OnurKerimoglu commited on
Commit
de0c472
·
1 Parent(s): dad9632

app.py: refactored and fixed warning

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -1,13 +1,8 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
  message,
12
  history: list[tuple[str, str]],
13
  system_message,
@@ -15,36 +10,57 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
19
 
20
  for val in history:
21
  if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
 
 
 
 
 
23
  if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
27
 
28
- response = ""
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
  max_tokens=max_tokens,
33
  stream=True,
34
  temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
 
 
 
39
  response += token
40
  yield response
41
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
  additional_inputs=[
49
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -61,4 +77,4 @@ demo = gr.ChatInterface(
61
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ import dotenv
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
+ def respond_chat_completion(
 
 
 
 
 
 
6
  message,
7
  history: list[tuple[str, str]],
8
  system_message,
 
10
  temperature,
11
  top_p,
12
  ):
13
+ messages_in = [
14
+ {
15
+ "role": "system",
16
+ "content": system_message
17
+ }
18
+ ]
19
 
20
  for val in history:
21
  if val[0]:
22
+ messages_in.append(
23
+ {
24
+ "role": "user",
25
+ "content": val[0]
26
+ }
27
+ )
28
  if val[1]:
29
+ messages_in.append(
30
+ {
31
+ "role": "assistant",
32
+ "content": val[1]
33
+ }
34
+ )
35
 
36
+ messages_in.append(
37
+ {
38
+ "role": "user",
39
+ "content": message
40
+ }
41
+ )
42
 
43
+ dotenv.load_dotenv(dotenv.find_dotenv()) # Load env. variable HF_TOKEN
44
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
45
 
46
+ messages_out = client.chat_completion(
47
+ messages_in,
48
  max_tokens=max_tokens,
49
  stream=True,
50
  temperature=temperature,
51
+ top_p=top_p
52
+ )
 
53
 
54
+ response = ""
55
+ for message in messages_out:
56
+ token = message.choices[0].delta.content
57
  response += token
58
  yield response
59
 
60
 
61
+ chatbot = gr.ChatInterface(
62
+ respond_chat_completion,
63
+ type='messages',
 
 
64
  additional_inputs=[
65
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
66
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
77
 
78
 
79
  if __name__ == "__main__":
80
+ chatbot.launch()