Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,9 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
import gradio as gr
|
4 |
-
from
|
5 |
-
from qwen_agent.agents import Agent
|
6 |
-
from qwen_agent.tools import Tool
|
7 |
|
8 |
-
#
|
9 |
-
HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
|
10 |
-
|
11 |
-
# Define a static weather tool
|
12 |
def get_current_weather(location, unit="fahrenheit"):
|
13 |
"""Get the current weather in a given location"""
|
14 |
if "tokyo" in location.lower():
|
@@ -20,56 +15,93 @@ def get_current_weather(location, unit="fahrenheit"):
|
|
20 |
else:
|
21 |
return json.dumps({"location": location, "temperature": "unknown"})
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
name
|
26 |
-
description
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
},
|
35 |
-
"unit": {
|
36 |
-
"type": "string",
|
37 |
-
"enum": ["celsius", "fahrenheit"],
|
38 |
-
"description": "The unit of temperature to use. Infer this from the user's location."
|
39 |
-
}
|
40 |
},
|
41 |
-
|
42 |
-
}
|
43 |
-
|
44 |
|
45 |
-
# Initialize the Qwen
|
46 |
-
def
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
tools=[weather_tool],
|
54 |
-
)
|
55 |
-
return agent
|
56 |
|
57 |
# Processing function for Gradio
|
58 |
def process_message(message, history):
|
59 |
-
# Initialize
|
60 |
-
if not hasattr(process_message, "
|
61 |
-
process_message.
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
#
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
|
69 |
# Set up the Gradio interface
|
70 |
-
with gr.Blocks(title="Qwen
|
71 |
-
gr.Markdown("# Qwen
|
72 |
-
gr.Markdown("This demo uses Qwen2.5-Coder-32B-Instruct with
|
73 |
gr.Markdown("### Example cities with data: Tokyo, San Francisco, Paris")
|
74 |
|
75 |
chatbot = gr.ChatInterface(
|
@@ -80,7 +112,7 @@ with gr.Blocks(title="Qwen Agent with Weather Tool") as demo:
|
|
80 |
"I'm planning a trip to Paris. How's the weather there?",
|
81 |
"What should I wear in Tokyo based on the weather?"
|
82 |
],
|
83 |
-
title="Chat with Qwen
|
84 |
)
|
85 |
|
86 |
# Launch the app
|
|
|
1 |
import os
|
2 |
import json
|
3 |
import gradio as gr
|
4 |
+
from qwen_agent.llm import get_chat_model
|
|
|
|
|
5 |
|
6 |
+
# Define a static weather tool function
|
|
|
|
|
|
|
7 |
def get_current_weather(location, unit="fahrenheit"):
|
8 |
"""Get the current weather in a given location"""
|
9 |
if "tokyo" in location.lower():
|
|
|
15 |
else:
|
16 |
return json.dumps({"location": location, "temperature": "unknown"})
|
17 |
|
18 |
+
# Set up the weather function definition
|
19 |
+
weather_function = {
|
20 |
+
'name': 'get_current_weather',
|
21 |
+
'description': 'Get the current weather in a given location',
|
22 |
+
'parameters': {
|
23 |
+
'type': 'object',
|
24 |
+
'properties': {
|
25 |
+
'location': {
|
26 |
+
'type': 'string',
|
27 |
+
'description': 'The city and state, e.g. San Francisco, CA',
|
28 |
+
},
|
29 |
+
'unit': {
|
30 |
+
'type': 'string',
|
31 |
+
'enum': ['celsius', 'fahrenheit'],
|
32 |
+
'description': 'The unit of temperature to use. Infer this from the user\'s location.'
|
33 |
},
|
|
|
|
|
|
|
|
|
|
|
34 |
},
|
35 |
+
'required': ['location'],
|
36 |
+
},
|
37 |
+
}
|
38 |
|
39 |
+
# Initialize the Qwen model
|
40 |
+
def init_model():
|
41 |
+
llm = get_chat_model({
|
42 |
+
'model': 'Qwen/Qwen2.5-Coder-32B-Instruct',
|
43 |
+
'endpoint_type': 'huggingface_hub',
|
44 |
+
'token': os.environ.get("HUGGINGFACE_TOKEN"),
|
45 |
+
})
|
46 |
+
return llm
|
|
|
|
|
|
|
47 |
|
48 |
# Processing function for Gradio
|
49 |
def process_message(message, history):
|
50 |
+
# Initialize model on first run
|
51 |
+
if not hasattr(process_message, "llm"):
|
52 |
+
process_message.llm = init_model()
|
53 |
+
|
54 |
+
# Set up the conversation
|
55 |
+
messages = [{'role': 'user', 'content': message}]
|
56 |
+
functions = [weather_function]
|
57 |
|
58 |
+
# Step 1: Get the initial response
|
59 |
+
try:
|
60 |
+
*_, response = process_message.llm.chat(
|
61 |
+
messages=messages,
|
62 |
+
functions=functions,
|
63 |
+
stream=True,
|
64 |
+
)
|
65 |
+
|
66 |
+
# Step 2: Check if the model wanted to call a function
|
67 |
+
if response.get('function_call', None):
|
68 |
+
# Step 3: Call the function
|
69 |
+
function_name = response['function_call']['name']
|
70 |
+
function_args = json.loads(response['function_call']['arguments'])
|
71 |
+
|
72 |
+
# Only process weather function calls
|
73 |
+
if function_name == 'get_current_weather':
|
74 |
+
function_response = get_current_weather(
|
75 |
+
location=function_args.get('location'),
|
76 |
+
unit=function_args.get('unit'),
|
77 |
+
)
|
78 |
+
|
79 |
+
# Step 4: Send the function result back to the model
|
80 |
+
messages.append(response) # Add the model's response with function call
|
81 |
+
messages.append({
|
82 |
+
'role': 'function',
|
83 |
+
'name': function_name,
|
84 |
+
'content': function_response,
|
85 |
+
})
|
86 |
+
|
87 |
+
# Get final response from the model
|
88 |
+
*_, final_response = process_message.llm.chat(
|
89 |
+
messages=messages,
|
90 |
+
functions=functions,
|
91 |
+
stream=False,
|
92 |
+
)
|
93 |
+
return final_response['content']
|
94 |
+
|
95 |
+
# If no function was called, return the initial response
|
96 |
+
return response['content']
|
97 |
|
98 |
+
except Exception as e:
|
99 |
+
return f"Error processing your request: {str(e)}"
|
100 |
|
101 |
# Set up the Gradio interface
|
102 |
+
with gr.Blocks(title="Qwen Weather Assistant") as demo:
|
103 |
+
gr.Markdown("# Qwen Weather Assistant")
|
104 |
+
gr.Markdown("This demo uses Qwen2.5-Coder-32B-Instruct with function calling. You can ask about the weather for any city!")
|
105 |
gr.Markdown("### Example cities with data: Tokyo, San Francisco, Paris")
|
106 |
|
107 |
chatbot = gr.ChatInterface(
|
|
|
112 |
"I'm planning a trip to Paris. How's the weather there?",
|
113 |
"What should I wear in Tokyo based on the weather?"
|
114 |
],
|
115 |
+
title="Chat with Qwen Weather Assistant"
|
116 |
)
|
117 |
|
118 |
# Launch the app
|