PKU-ML commited on
Commit
c76a7c9
·
verified ·
1 Parent(s): 437d2bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -51
app.py CHANGED
@@ -1,64 +1,98 @@
 
 
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(model="PKU-ML/G1-7B")
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- # system_message,
14
- max_tokens,
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"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('pip install dashscope')
3
  import gradio as gr
4
+ from http import HTTPStatus
5
+ import dashscope
6
+ from dashscope import Generation
7
+ from dashscope.api_entities.dashscope_response import Role
8
+ from typing import List, Optional, Tuple, Dict
9
+ from urllib.error import HTTPError
10
+ default_system = 'You are a helpful assistant.'
11
 
12
+ YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
13
+ dashscope.api_key = YOUR_API_TOKEN
 
 
14
 
15
+ History = List[Tuple[str, str]]
16
+ Messages = List[Dict[str, str]]
17
 
18
+ def clear_session() -> History:
19
+ return '', []
 
 
 
 
 
 
 
20
 
21
+ def modify_system_session(system: str) -> str:
22
+ if system is None or len(system) == 0:
23
+ system = default_system
24
+ return system, system, []
 
25
 
26
+ def history_to_messages(history: History, system: str) -> Messages:
27
+ messages = [{'role': Role.SYSTEM, 'content': system}]
28
+ for h in history:
29
+ messages.append({'role': Role.USER, 'content': h[0]})
30
+ messages.append({'role': Role.ASSISTANT, 'content': h[1]})
31
+ return messages
32
 
 
33
 
34
+ def messages_to_history(messages: Messages) -> Tuple[str, History]:
35
+ assert messages[0]['role'] == Role.SYSTEM
36
+ system = messages[0]['content']
37
+ history = []
38
+ for q, r in zip(messages[1::2], messages[2::2]):
39
+ history.append([q['content'], r['content']])
40
+ return system, history
 
41
 
 
 
42
 
43
+ def model_chat(query: Optional[str], history: Optional[History], system: str
44
+ ) -> Tuple[str, str, History]:
45
+ if query is None:
46
+ query = ''
47
+ if history is None:
48
+ history = []
49
+ messages = history_to_messages(history, system)
50
+ messages.append({'role': Role.USER, 'content': query})
51
+ gen = Generation.call(
52
+ model = "G1-7B",
53
+ messages=messages,
54
+ result_format='message',
55
+ stream=True
56
+ )
57
+ for response in gen:
58
+ if response.status_code == HTTPStatus.OK:
59
+ role = response.output.choices[0].message.role
60
+ response = response.output.choices[0].message.content
61
+ system, history = messages_to_history(messages + [{'role': role, 'content': response}])
62
+ yield '', history, system
63
+ else:
64
+ raise HTTPError('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
65
+ response.request_id, response.status_code,
66
+ response.code, response.message
67
+ ))
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ with gr.Blocks() as demo:
71
+ gr.Markdown("""<p align="center"><img src="https://modelscope.cn/api/v1/models/qwen/Qwen-VL-Chat/repo?Revision=master&FilePath=assets/logo.jpg&View=true" style="height: 80px"/><p>""")
72
+ gr.Markdown("""<center><font size=8>Qwen-1.8B-Chat Bot👾</center>""")
73
+ gr.Markdown("""<center><font size=4>通义千问-1.8B(Qwen-1.8B) 是阿里云研发的通义千问大模型系列的18亿参数规模的模型。</center>""")
74
 
75
+ with gr.Row():
76
+ with gr.Column(scale=3):
77
+ system_input = gr.Textbox(value=default_system, lines=1, label='System')
78
+ with gr.Column(scale=1):
79
+ modify_system = gr.Button("🛠️ 设置system并清除历史对话", scale=2)
80
+ system_state = gr.Textbox(value=default_system, visible=False)
81
+ chatbot = gr.Chatbot(label='G1-7B')
82
+ textbox = gr.Textbox(lines=2, label='Input')
83
+
84
+ with gr.Row():
85
+ clear_history = gr.Button("🧹 清除历史对话")
86
+ sumbit = gr.Button("🚀 发送")
87
+
88
+ sumbit.click(model_chat,
89
+ inputs=[textbox, chatbot, system_state],
90
+ outputs=[textbox, chatbot, system_input])
91
+ clear_history.click(fn=clear_session,
92
+ inputs=[],
93
+ outputs=[textbox, chatbot])
94
+ modify_system.click(fn=modify_system_session,
95
+ inputs=[system_input],
96
+ outputs=[system_state, system_input, chatbot])
97
+
98
+ demo.queue(api_open=False).launch(max_threads=10,height=800, share=False)