ndwdgda commited on
Commit
7736908
·
verified ·
1 Parent(s): 3feea3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -24
app.py CHANGED
@@ -1,24 +1,17 @@
1
-
2
  !pip install huggingface_hub
3
  !pip install transformers
4
  import gradio as gr
5
  from huggingface_hub import InferenceClient
 
6
 
 
7
 
8
- from transformers import pipeline
 
 
 
 
9
 
10
- messages = [
11
- {"role": "user", "content": "Who are you?"},
12
- ]
13
- pipe = pipeline("text-generation", model="ibm-granite/granite-8b-code-instruct")
14
- pipe(messages)
15
- message,
16
- history: list[tuple[str, str]],
17
- system_message,
18
- max_tokens,
19
- temperature,
20
- top_p,
21
- ):
22
  messages = [{"role": "system", "content": system_message}]
23
 
24
  for val in history:
@@ -31,21 +24,19 @@ pipe(messages)
31
 
32
  response = ""
33
 
 
 
34
  for message in client.chat_completion(
35
- messages,
36
- max_tokens=max_tokens,
37
- stream=True,
38
- temperature=temperature,
39
- top_p=top_p,
40
  ):
41
  token = message.choices[0].delta.content
42
-
43
  response += token
44
  yield response
45
 
46
- """
47
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
48
- """
49
  demo = gr.ChatInterface(
50
  respond,
51
  additional_inputs=[
@@ -62,6 +53,5 @@ demo = gr.ChatInterface(
62
  ],
63
  )
64
 
65
-
66
  if __name__ == "__main__":
67
  demo.launch()
 
 
1
  !pip install huggingface_hub
2
  !pip install transformers
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
+ from transformers import pipeline
6
 
7
+ system_message = "You are a friendly chatbot."
8
 
9
+ def respond(message, history=None, system_message=system_message, max_tokens=512, temperature=0.7, top_p=0.95):
10
+ if history is None:
11
+ history = []
12
+ if isinstance(history, str):
13
+ history = json.loads(history)
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  messages = [{"role": "system", "content": system_message}]
16
 
17
  for val in history:
 
24
 
25
  response = ""
26
 
27
+ client = InferenceClient(pipeline("text-generation", model="ibm-granite/granite-8b-code-instruct"))
28
+
29
  for message in client.chat_completion(
30
+ messages,
31
+ max_tokens=max_tokens,
32
+ stream=True,
33
+ temperature=temperature,
34
+ top_p=top_p,
35
  ):
36
  token = message.choices[0].delta.content
 
37
  response += token
38
  yield response
39
 
 
 
 
40
  demo = gr.ChatInterface(
41
  respond,
42
  additional_inputs=[
 
53
  ],
54
  )
55
 
 
56
  if __name__ == "__main__":
57
  demo.launch()