Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -55,24 +55,24 @@ def model_chat(query: Optional[str], history: Optional[History], system: str) ->
|
|
55 |
query = ''
|
56 |
if history is None:
|
57 |
history = []
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
# Convert history to a list of messages
|
60 |
messages = history_to_messages(history, system)
|
61 |
messages.append({'role': 'user', 'content': query})
|
62 |
|
63 |
-
# Prepare the payload for Hugging Face Inference API
|
64 |
payload = {"inputs": query, "parameters": {"max_new_tokens": 150}, "history": messages}
|
65 |
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
66 |
|
67 |
try:
|
68 |
-
# Request generation with Hugging Face Inference API
|
69 |
response = requests.post(f"https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct",
|
70 |
json=payload, headers=headers)
|
71 |
|
72 |
if response.status_code == 200:
|
73 |
response_data = response.json()
|
74 |
-
|
75 |
-
# Handle the response as a list or dict
|
76 |
if isinstance(response_data, list):
|
77 |
response_text = response_data[0].get('generated_text', '')
|
78 |
else:
|
@@ -83,20 +83,18 @@ def model_chat(query: Optional[str], history: Optional[History], system: str) ->
|
|
83 |
|
84 |
# Update history with the new assistant response and return it
|
85 |
history.append([query, response_text])
|
86 |
-
system, history = messages_to_history(messages + [{'role': 'assistant', 'content': response_text}])
|
87 |
return response_text, history, system
|
88 |
else:
|
89 |
-
# Log error message to file and display it in Gradio
|
90 |
error_message = f"Error {response.status_code}: {response.json().get('error', response.text)}"
|
91 |
log_history_to_file(query, error_message)
|
92 |
return error_message, history, system
|
93 |
except Exception as e:
|
94 |
-
# Log any unexpected exceptions
|
95 |
error_message = f"Exception: {str(e)}"
|
96 |
log_history_to_file(query, error_message)
|
97 |
return error_message, history, system
|
98 |
|
99 |
|
|
|
100 |
# Gradio Interface Setup
|
101 |
with gr.Blocks() as demo:
|
102 |
gr.Markdown("<center><font size=8>Qwen2.5-72B-Instruct👾</center>")
|
|
|
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:
|
|
|
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>")
|