mimifuel2018 commited on
Commit
6436fcc
1 Parent(s): 747d900

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -36
app.py CHANGED
@@ -1,37 +1,108 @@
1
- import gradio as gr
2
- import requests
3
  import os
4
- from datetime import datetime
5
-
6
- # Define the Hugging Face API endpoint
7
- api_url = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct"
8
-
9
- # Retrieve the token from the environment
10
- headers = {"Authorization": f"Bearer {os.getenv('HF_Token')}"}
11
-
12
- # Define a function to send prompts to the model and get responses
13
- def query(prompt):
14
- response = requests.post(api_url, headers=headers, json={"inputs": prompt})
15
- if response.status_code == 200:
16
- result = response.json()[0]["generated_text"]
17
-
18
- # Log interaction to logs.txt
19
- with open("logs.txt", "a") as log_file:
20
- log_entry = f"{datetime.now()} - Prompt: {prompt}\nResponse: {result}\n\n"
21
- log_file.write(log_entry)
22
-
23
- return result
24
- else:
25
- return f"Error {response.status_code}: {response.text}"
26
-
27
- # Create a Gradio interface with text input and output
28
- demo = gr.Interface(
29
- fn=query,
30
- inputs="text",
31
- outputs="text",
32
- title="Qwen-2.5 72B Interaction",
33
- description="Ask complex mathematical or pattern-related questions and get responses from Qwen-2.5 72B."
34
- )
35
-
36
- # Launch the interface
37
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ from http import HTTPStatus
4
+ import dashscope
5
+ from dashscope import Generation
6
+ from dashscope.api_entities.dashscope_response import Role
7
+ from typing import List, Optional, Tuple, Dict
8
+
9
+ default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
10
+
11
+ dashscope.api_key = os.getenv('YOUR_API_TOKEN') # Ensure this token is managed securely
12
+
13
+ History = List[Tuple[str, str]]
14
+ Messages = List[Dict[str, str]]
15
+
16
+ def clear_session() -> History:
17
+ return '', []
18
+
19
+ def modify_system_session(system: str) -> str:
20
+ if system is None or len(system) == 0:
21
+ system = default_system
22
+ return system, system, []
23
+
24
+ def history_to_messages(history: History, system: str) -> Messages:
25
+ messages = [{'role': Role.SYSTEM, 'content': system}]
26
+ for h in history:
27
+ messages.append({'role': Role.USER, 'content': h[0]})
28
+ messages.append({'role': Role.ASSISTANT, 'content': h[1]})
29
+ return messages
30
+
31
+ def messages_to_history(messages: Messages) -> Tuple[str, History]:
32
+ assert messages[0]['role'] == Role.SYSTEM
33
+ system = messages[0]['content']
34
+ history = []
35
+ for q, r in zip(messages[1::2], messages[2::2]):
36
+ history.append([q['content'], r['content']])
37
+ return system, history
38
+
39
+ def model_chat(query: Optional[str], history: Optional[History], system: str
40
+ ) -> Tuple[str, str, History]:
41
+ if query is None:
42
+ query = ''
43
+ if history is None:
44
+ history = []
45
+ messages = history_to_messages(history, system)
46
+ messages.append({'role': Role.USER, 'content': query})
47
+
48
+ # Request generation with controlled parameters
49
+ gen = Generation.call(
50
+ model='qwen2.5-72b-instruct',
51
+ messages=messages,
52
+ result_format='message',
53
+ stream=True,
54
+ max_new_tokens=150 # Set response length limit
55
+ )
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 ValueError('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
+ # Gradio Interface Setup
70
+ with gr.Blocks() as demo:
71
+ gr.Markdown("""<center><font size=8>Qwen2.5-72B-Instruct👾</center>""")
72
+
73
+ with gr.Row():
74
+ with gr.Column(scale=3):
75
+ system_input = gr.Textbox(value=default_system, lines=1, label='System')
76
+ with gr.Column(scale=1):
77
+ modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2)
78
+ system_state = gr.Textbox(value=default_system, visible=False)
79
+ chatbot = gr.Chatbot(label='Qwen2.5-72B-Instruct')
80
+ textbox = gr.Textbox(lines=1, label='Input')
81
+
82
+ with gr.Row():
83
+ clear_history = gr.Button("🧹 Clear history")
84
+ submit = gr.Button("🚀 Send")
85
+
86
+ textbox.submit(model_chat,
87
+ inputs=[textbox, chatbot, system_state],
88
+ outputs=[textbox, chatbot, system_input],
89
+ concurrency_limit=5) # Reduced for free plan
90
+
91
+ submit.click(model_chat,
92
+ inputs=[textbox, chatbot, system_state],
93
+ outputs=[textbox, chatbot, system_input],
94
+ concurrency_limit=5)
95
+
96
+ clear_history.click(fn=clear_session,
97
+ inputs=[],
98
+ outputs=[textbox, chatbot],
99
+ concurrency_limit=5)
100
+
101
+ modify_system.click(fn=modify_system_session,
102
+ inputs=[system_input],
103
+ outputs=[system_state, system_input, chatbot],
104
+ concurrency_limit=5)
105
+
106
+ # Launching with reduced threads for free plan
107
+ demo.queue(api_open=False)
108
+ demo.launch(max_threads=10)