Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,11 +39,9 @@ def get_location_from_ip():
|
|
39 |
'longitude': response.get('longitude')
|
40 |
}
|
41 |
|
42 |
-
|
43 |
class WeatherInput(BaseModel):
|
44 |
city: str = Field(default=None, description="The city to get the weather for.")
|
45 |
|
46 |
-
|
47 |
@tool("get_weather_by_location", args_schema=WeatherInput, return_direct=True)
|
48 |
def get_weather_by_location(city: str = None):
|
49 |
"""Get the weather based on the user's location if no city is specified."""
|
@@ -68,29 +66,27 @@ def get_weather_by_location(city: str = None):
|
|
68 |
}
|
69 |
|
70 |
response = requests.post(url, json=payload, headers=headers).json()
|
71 |
-
|
72 |
return format_weather_response(response, city)
|
73 |
|
74 |
-
|
75 |
def format_weather_response(weather_data, city):
|
76 |
"""Format the weather data into a readable string."""
|
77 |
intervals = weather_data['data']['timelines'][0]['intervals']
|
78 |
response = f"Weather forecast for {city}:\n\n"
|
79 |
-
|
80 |
for interval in intervals:
|
81 |
date = datetime.fromisoformat(interval['startTime']).strftime("%A, %B %d")
|
82 |
temp = round(interval['values']['temperature'], 1)
|
83 |
humidity = round(interval['values']['humidity'], 1)
|
84 |
wind_speed = round(interval['values']['windSpeed'], 1)
|
85 |
-
|
86 |
response += f"{date}:\n"
|
87 |
response += f" Temperature: {temp}°C\n"
|
88 |
response += f" Humidity: {humidity}%\n"
|
89 |
response += f" Wind Speed: {wind_speed} km/h\n\n"
|
90 |
-
|
91 |
return response
|
92 |
|
93 |
-
|
94 |
get_weather_tool = Tool(
|
95 |
name="get_weather_by_location",
|
96 |
func=get_weather_by_location,
|
@@ -134,18 +130,18 @@ Remember to be friendly and informative in your responses, and focus on providin
|
|
134 |
])
|
135 |
|
136 |
|
137 |
-
# Initialize tools and agent
|
138 |
-
tools = [get_weather_by_location]
|
139 |
|
140 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
141 |
|
|
|
|
|
142 |
agent = create_tool_calling_agent(llm, tools, prompt=prompt)
|
143 |
|
144 |
agent_executor = AgentExecutor(
|
145 |
agent=agent,
|
146 |
tools=tools,
|
147 |
memory=memory,
|
148 |
-
output_parser=
|
149 |
)
|
150 |
|
151 |
|
@@ -160,11 +156,11 @@ with gr.Blocks() as demo:
|
|
160 |
chatbot = gr.Chatbot()
|
161 |
with gr.Row():
|
162 |
txt = gr.Textbox(
|
163 |
-
show_label=False,
|
164 |
-
placeholder="Ask about the weather...",
|
165 |
-
lines=1,
|
166 |
container=False
|
167 |
)
|
168 |
txt.submit(gradio_interface, inputs=txt, outputs=chatbot)
|
169 |
|
170 |
-
demo.launch()
|
|
|
39 |
'longitude': response.get('longitude')
|
40 |
}
|
41 |
|
|
|
42 |
class WeatherInput(BaseModel):
|
43 |
city: str = Field(default=None, description="The city to get the weather for.")
|
44 |
|
|
|
45 |
@tool("get_weather_by_location", args_schema=WeatherInput, return_direct=True)
|
46 |
def get_weather_by_location(city: str = None):
|
47 |
"""Get the weather based on the user's location if no city is specified."""
|
|
|
66 |
}
|
67 |
|
68 |
response = requests.post(url, json=payload, headers=headers).json()
|
69 |
+
|
70 |
return format_weather_response(response, city)
|
71 |
|
|
|
72 |
def format_weather_response(weather_data, city):
|
73 |
"""Format the weather data into a readable string."""
|
74 |
intervals = weather_data['data']['timelines'][0]['intervals']
|
75 |
response = f"Weather forecast for {city}:\n\n"
|
76 |
+
|
77 |
for interval in intervals:
|
78 |
date = datetime.fromisoformat(interval['startTime']).strftime("%A, %B %d")
|
79 |
temp = round(interval['values']['temperature'], 1)
|
80 |
humidity = round(interval['values']['humidity'], 1)
|
81 |
wind_speed = round(interval['values']['windSpeed'], 1)
|
82 |
+
|
83 |
response += f"{date}:\n"
|
84 |
response += f" Temperature: {temp}°C\n"
|
85 |
response += f" Humidity: {humidity}%\n"
|
86 |
response += f" Wind Speed: {wind_speed} km/h\n\n"
|
87 |
+
|
88 |
return response
|
89 |
|
|
|
90 |
get_weather_tool = Tool(
|
91 |
name="get_weather_by_location",
|
92 |
func=get_weather_by_location,
|
|
|
130 |
])
|
131 |
|
132 |
|
|
|
|
|
133 |
|
134 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
135 |
|
136 |
+
tools = [get_weather_tool]
|
137 |
+
|
138 |
agent = create_tool_calling_agent(llm, tools, prompt=prompt)
|
139 |
|
140 |
agent_executor = AgentExecutor(
|
141 |
agent=agent,
|
142 |
tools=tools,
|
143 |
memory=memory,
|
144 |
+
output_parser=parser
|
145 |
)
|
146 |
|
147 |
|
|
|
156 |
chatbot = gr.Chatbot()
|
157 |
with gr.Row():
|
158 |
txt = gr.Textbox(
|
159 |
+
show_label=False,
|
160 |
+
placeholder="Ask about the weather...",
|
161 |
+
lines=1,
|
162 |
container=False
|
163 |
)
|
164 |
txt.submit(gradio_interface, inputs=txt, outputs=chatbot)
|
165 |
|
166 |
+
demo.launch(share=True)
|