wwpop commited on
Commit
d801608
·
verified ·
1 Parent(s): b797dc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -47
app.py CHANGED
@@ -1,69 +1,73 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
- import json
5
 
6
- api_key = os.environ.get('qwen_API_KEY')
7
  """
8
- 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
9
  """
10
- client = InferenceClient("Qwen/Qwen2.5-72B-Instruct", token=api_key)
11
 
12
  def respond(
13
- message,
14
- history: list[tuple[str, str]],
15
- system_message,
16
- max_tokens,
17
- temperature,
18
- top_p
19
  ):
20
- messages = [{"role": "system", "content": system_message}]
21
 
22
- for val in history:
23
- if val[0]:
24
- messages.append({"role": "user", "content": val[0]})
25
- if val[1]:
26
- messages.append({"role": "assistant", "content": val[1]})
27
 
28
- messages.append({"role": "user", "content": message})
 
 
 
 
29
 
30
- response = ""
31
 
32
- try:
33
- async for message in client.chat_completion(
34
- messages,
35
- max_tokens=max_tokens,
36
- stream=True,
37
- temperature=temperature,
38
- top_p=top_p
39
- ):
40
- token = message.choices[0].delta.content
41
- response += token
42
- yield response
43
- except Exception as e:
44
- yield f"An error occurred: {e}"
45
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  example_prompts = [
47
  ["How to cook Kung Pao chicken the tastiest?"],
48
- ["Help me create an email expressing my greetings to an old friend."],
49
  ["写一篇关于青春的五言绝句"],
50
- ["你是谁?"]
 
 
 
 
 
51
  ]
52
 
53
  demo = gr.ChatInterface(
54
- respond,
55
- examples=example_prompts,
56
- title="千问2.5-72B",
57
- description="千问2.5-72B聊天机器人",
58
- additional_inputs=[
59
- gr.Textbox(value="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.", label="System message"),
60
- gr.Slider(minimum=1, maximum=8888, value=2048, step=1, label="Max new tokens"),
61
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
62
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
63
- ],
64
- chatbot=gr.Chatbot(show_label=True, show_copy_button=True)
 
65
  )
66
 
67
- if __name__ == "__main__":
68
- demo.queue(default_concurrency_limit=40)
69
- demo.launch(max_threads=40)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
 
4
 
5
+ api_key=os.environ.get('qwen_API_KEY')
6
  """
7
+ 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
8
  """
9
+ client = InferenceClient("Qwen/Qwen2.5-72B-Instruct",token=api_key)
10
 
11
  def respond(
12
+ message,
13
+ history: list[tuple[str, str]],
14
+ system_message,
15
+ max_tokens,
16
+ temperature,
17
+ top_p
18
  ):
19
+ messages = [{"role": "system", "content": system_message}]
20
 
 
 
 
 
 
21
 
22
+ for val in history:
23
+ if val[0]:
24
+ messages.append({"role": "user", "content": val[0]})
25
+ if val[1]:
26
+ messages.append({"role": "assistant", "content": val[1]})
27
 
28
+ messages.append({"role": "user", "content": message})
29
 
30
+ response = ""
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ for message in client.chat_completion(
33
+ messages,
34
+ max_tokens=max_tokens,
35
+ stream=True,
36
+ temperature=temperature,
37
+ top_p=top_p
38
+ ):
39
+ token = message.choices[0].delta.content
40
+
41
+ response += token
42
+ yield response
43
+
44
  example_prompts = [
45
  ["How to cook Kung Pao chicken the tastiest?"],
46
+ ["你是谁开发的?"],
47
  ["写一篇关于青春的五言绝句"],
48
+ ["你是谁?"]
49
+ ]
50
+ latex_delimiters = [
51
+ {"left": "$$", "right": "$$", "display": True},
52
+ {"left": "\\[", "right": "\\]", "display": True},{"left": "$", "right": "$", "display": False},
53
+ {"left": "\\(", "right": "\\)", "display": False}
54
  ]
55
 
56
  demo = gr.ChatInterface(
57
+ respond,
58
+ examples=example_prompts,
59
+ title="千问2.5-72B",
60
+ description="千问2.5-72B聊天机器人",
61
+ additional_inputs=[
62
+ gr.Textbox(value="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.", label="System message"),
63
+ gr.Slider(minimum=1, maximum=8888, value=2048, step=1, label="Max new tokens"),
64
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
65
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
66
+ ],
67
+ chatbot=gr.Chatbot(show_label=True,latex_delimiters=latex_delimiters, show_copy_button=True)
68
+
69
  )
70
 
71
+ if name == "main":
72
+ demo.queue(default_concurrency_limit=40)
73
+ demo.launch(max_threads=40)