Xiaoqing WANG commited on
Commit
810169e
·
1 Parent(s): 2e0cd9a

Update app.py with latest changes

Browse files
Files changed (1) hide show
  1. app.py +27 -48
app.py CHANGED
@@ -1,71 +1,50 @@
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
  import os
8
  from huggingface_hub import InferenceClient
9
  import gradio as gr
10
 
 
11
  print("DEBUG TOKEN:", os.getenv("mytokenvivi"))
12
 
13
-
14
- token = token = os.getenv("mytokenvivi")
15
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)
16
 
17
- def respond(
18
- message,
19
- history: list[tuple[str, str]],
20
- system_message,
21
- max_tokens,
22
- temperature,
23
- top_p,
24
- ):
25
  messages = [{"role": "system", "content": system_message}]
26
 
27
- for val in history:
28
- if val[0]:
29
- messages.append({"role": "user", "content": val[0]})
30
- if val[1]:
31
- messages.append({"role": "assistant", "content": val[1]})
32
 
33
  messages.append({"role": "user", "content": message})
34
 
35
  response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- for message in client.chat_completion(
38
- messages,
39
- max_tokens=max_tokens,
40
- stream=True,
41
- temperature=temperature,
42
- top_p=top_p,
43
- ):
44
- token = message.choices[0].delta.content
45
-
46
- response += token
47
- yield response
48
-
49
-
50
- """
51
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
52
- """
53
  demo = gr.ChatInterface(
54
  respond,
55
  additional_inputs=[
56
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
57
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
58
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
59
- gr.Slider(
60
- minimum=0.2,
61
- maximum=1.0,
62
- value=0.95,
63
- step=0.05,
64
- label="Top-p (nucleus sampling)",
65
- ),
66
  ],
67
  )
68
 
69
-
70
  if __name__ == "__main__":
71
  demo.launch()
 
 
 
 
 
 
 
1
  import os
2
  from huggingface_hub import InferenceClient
3
  import gradio as gr
4
 
5
+ # Debug token in logs (remove later!)
6
  print("DEBUG TOKEN:", os.getenv("mytokenvivi"))
7
 
8
+ # Load token from env variable (must be set as a Secret in Hugging Face Space)
9
+ token = os.getenv("mytokenvivi")
10
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)
11
 
12
+ def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
 
 
 
 
 
 
 
13
  messages = [{"role": "system", "content": system_message}]
14
 
15
+ for user, assistant in history:
16
+ if user:
17
+ messages.append({"role": "user", "content": user})
18
+ if assistant:
19
+ messages.append({"role": "assistant", "content": assistant})
20
 
21
  messages.append({"role": "user", "content": message})
22
 
23
  response = ""
24
+ try:
25
+ for msg in client.chat_completion(
26
+ messages=messages,
27
+ max_tokens=max_tokens,
28
+ stream=True,
29
+ temperature=temperature,
30
+ top_p=top_p,
31
+ ):
32
+ token = msg.choices[0].delta.content
33
+ if token:
34
+ response += token
35
+ yield response
36
+ except Exception as e:
37
+ yield f"[ERROR] {str(e)}"
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  demo = gr.ChatInterface(
40
  respond,
41
  additional_inputs=[
42
+ gr.Textbox(value="You are a friendly chatbot.", label="System message"),
43
+ gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
44
+ gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
45
+ gr.Slider(0.2, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
46
  ],
47
  )
48
 
 
49
  if __name__ == "__main__":
50
  demo.launch()