Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,16 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
|
|
3 |
from typing import List, Optional, Tuple, Dict
|
4 |
import dashscope
|
5 |
from dashscope import Generation
|
6 |
from dashscope.api_entities.dashscope_response import Role
|
7 |
-
|
8 |
-
# Configuration
|
9 |
-
default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
|
10 |
-
dashscope.api_key = os.getenv('HF_TOKEN') # Check if this is loading correctly
|
11 |
-
|
12 |
-
# **Test Token Retrieval** (remove after testing)
|
13 |
-
print("Token:", dashscope.api_key)
|
14 |
-
|
15 |
-
# Typing definitions
|
16 |
-
History = List[Tuple[str, str]]
|
17 |
-
Messages = List[Dict[str, str]]
|
18 |
|
19 |
|
20 |
-
import os
|
21 |
-
import gradio as gr
|
22 |
-
from typing import List, Optional, Tuple, Dict
|
23 |
-
import dashscope
|
24 |
-
from dashscope import Generation
|
25 |
-
from dashscope.api_entities.dashscope_response import Role
|
26 |
-
|
27 |
# Configuration
|
28 |
default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
|
29 |
-
dashscope.api_key = os.getenv('HF_TOKEN')
|
30 |
|
31 |
# Typing definitions
|
32 |
History = List[Tuple[str, str]]
|
@@ -71,37 +55,46 @@ def model_chat(query: Optional[str], history: Optional[History], system: str) ->
|
|
71 |
query = ''
|
72 |
if history is None:
|
73 |
history = []
|
|
|
|
|
|
|
|
|
74 |
|
75 |
messages = history_to_messages(history, system)
|
76 |
-
messages.append({'role':
|
|
|
|
|
|
|
77 |
|
78 |
try:
|
79 |
-
|
80 |
-
|
81 |
-
messages=messages,
|
82 |
-
result_format='message',
|
83 |
-
stream=True
|
84 |
-
)
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
log_history_to_file(query, response_text)
|
92 |
-
|
93 |
-
# Update history with the new assistant response
|
94 |
-
history.append((query, response_text))
|
95 |
-
return response_text, history, system
|
96 |
else:
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
except Exception as e:
|
101 |
error_message = f"Exception: {str(e)}"
|
102 |
log_history_to_file(query, error_message)
|
103 |
return error_message, history, system
|
104 |
|
|
|
|
|
105 |
# Gradio Interface Setup
|
106 |
with gr.Blocks() as demo:
|
107 |
gr.Markdown("<center><font size=8>Qwen2.5-72B-Instruct👾</center>")
|
@@ -120,6 +113,7 @@ with gr.Blocks() as demo:
|
|
120 |
clear_history = gr.Button("🧹 Clear history")
|
121 |
submit = gr.Button("🚀 Send")
|
122 |
|
|
|
123 |
textbox.submit(model_chat,
|
124 |
inputs=[textbox, chatbot, system_state],
|
125 |
outputs=[textbox, chatbot, system_input],
|
@@ -142,4 +136,4 @@ with gr.Blocks() as demo:
|
|
142 |
|
143 |
# Launching Gradio Interface with reduced threads for free plan
|
144 |
demo.queue(api_open=False)
|
145 |
-
demo.launch(max_threads=10)
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from http import HTTPStatus
|
4 |
from typing import List, Optional, Tuple, Dict
|
5 |
import dashscope
|
6 |
from dashscope import Generation
|
7 |
from dashscope.api_entities.dashscope_response import Role
|
8 |
+
import requests # <-- Add this line to import the requests library
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Configuration
|
12 |
default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
|
13 |
+
dashscope.api_key = os.getenv('HF_TOKEN') # Replace 'YOUR_API_TOKEN' with your actual API token.
|
14 |
|
15 |
# Typing definitions
|
16 |
History = List[Tuple[str, str]]
|
|
|
55 |
query = ''
|
56 |
if history is None:
|
57 |
history = []
|
58 |
+
|
59 |
+
# Ensure the query is clearly asking for numbers
|
60 |
+
if 'next numbers' in query or 'give me numbers after' in query:
|
61 |
+
query = "Please give me the next 10 numbers after 10, starting from 11."
|
62 |
|
63 |
messages = history_to_messages(history, system)
|
64 |
+
messages.append({'role': 'user', 'content': query})
|
65 |
+
|
66 |
+
payload = {"inputs": query, "parameters": {"max_new_tokens": 150}, "history": messages}
|
67 |
+
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
68 |
|
69 |
try:
|
70 |
+
response = requests.post(f"https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct",
|
71 |
+
json=payload, headers=headers)
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
if response.status_code == 200:
|
74 |
+
response_data = response.json()
|
75 |
+
|
76 |
+
if isinstance(response_data, list):
|
77 |
+
response_text = response_data[0].get('generated_text', '')
|
|
|
|
|
|
|
|
|
|
|
78 |
else:
|
79 |
+
response_text = response_data.get('generated_text', '')
|
80 |
+
|
81 |
+
# Log the chat to file
|
82 |
+
log_history_to_file(query, response_text)
|
83 |
+
|
84 |
+
# Update history with the new assistant response and return it
|
85 |
+
history.append([query, response_text])
|
86 |
+
return response_text, history, system
|
87 |
+
else:
|
88 |
+
error_message = f"Error {response.status_code}: {response.json().get('error', response.text)}"
|
89 |
+
log_history_to_file(query, error_message)
|
90 |
+
return error_message, history, system
|
91 |
except Exception as e:
|
92 |
error_message = f"Exception: {str(e)}"
|
93 |
log_history_to_file(query, error_message)
|
94 |
return error_message, history, system
|
95 |
|
96 |
+
|
97 |
+
|
98 |
# Gradio Interface Setup
|
99 |
with gr.Blocks() as demo:
|
100 |
gr.Markdown("<center><font size=8>Qwen2.5-72B-Instruct👾</center>")
|
|
|
113 |
clear_history = gr.Button("🧹 Clear history")
|
114 |
submit = gr.Button("🚀 Send")
|
115 |
|
116 |
+
# Link buttons to functions
|
117 |
textbox.submit(model_chat,
|
118 |
inputs=[textbox, chatbot, system_state],
|
119 |
outputs=[textbox, chatbot, system_input],
|
|
|
136 |
|
137 |
# Launching Gradio Interface with reduced threads for free plan
|
138 |
demo.queue(api_open=False)
|
139 |
+
demo.launch(max_threads=10)"
|