Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,86 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
-
import
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
-
from Gradio_UI import GradioUI
|
| 9 |
-
|
| 10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
-
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the first argument
|
| 17 |
-
arg2: the second argument
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
-
"""
|
| 24 |
Args:
|
| 25 |
-
timezone: A string representing a valid timezone (e.g., '
|
| 26 |
"""
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
-
return f"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
with open("prompts.yaml", 'r') as stream:
|
| 54 |
-
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer],
|
| 59 |
-
max_steps=
|
| 60 |
-
verbosity_level=
|
| 61 |
-
|
| 62 |
-
planning_interval=None,
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 3 |
import datetime
|
|
|
|
| 4 |
import pytz
|
| 5 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@tool
|
| 8 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 9 |
+
"""Tool to fetch the current local time in a specified timezone.
|
| 10 |
Args:
|
| 11 |
+
timezone: A string representing a valid timezone (e.g., 'Europe/Moscow').
|
| 12 |
"""
|
| 13 |
+
if not isinstance(timezone, str):
|
| 14 |
+
return f"Error: Expected a string for timezone, got: {type(timezone)}"
|
| 15 |
try:
|
|
|
|
| 16 |
tz = pytz.timezone(timezone)
|
|
|
|
| 17 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 18 |
+
return f"Current time in {timezone}: {local_time}"
|
| 19 |
except Exception as e:
|
| 20 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 21 |
|
| 22 |
+
@tool
|
| 23 |
+
def final_answer(answer: str) -> str:
|
| 24 |
+
"""Returns the final answer.
|
| 25 |
+
Args:
|
| 26 |
+
answer: The final answer as a string.
|
| 27 |
+
"""
|
| 28 |
+
return answer
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
model = HfApiModel(
|
| 32 |
+
max_tokens=1000,
|
| 33 |
+
temperature=0.5,
|
| 34 |
+
model_id='mistralai/Mixtral-8x7B-Instruct-v0.1',
|
| 35 |
+
)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Error initializing model: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
prompt_templates = {
|
| 40 |
+
"system_prompt": (
|
| 41 |
+
"You are a helpful assistant that responds in the same language as the user's query. "
|
| 42 |
+
"Detect the language of the query and respond accordingly, ensuring clarity and accuracy. "
|
| 43 |
+
"For time-related questions (e.g., 'What time is it in [place]?'), use the `get_current_time_in_timezone` tool "
|
| 44 |
+
"with the appropriate timezone (e.g., 'Europe/Moscow' for Moscow, 'Asia/Yekaterinburg' for Birsk). "
|
| 45 |
+
"NEVER redefine tools—use only the provided ones. "
|
| 46 |
+
"Always return the response via `final_answer()`, not through print or variables. "
|
| 47 |
+
"If the query is unclear, ask for clarification in the same language as the query."
|
| 48 |
+
),
|
| 49 |
+
"default": "Response: {{question}}",
|
| 50 |
+
"planning": {
|
| 51 |
+
"initial_plan": (
|
| 52 |
+
"Analyze the query: {{question}}. Determine if a tool is needed. "
|
| 53 |
+
"If the query is about time, use `get_current_time_in_timezone` with the correct timezone (e.g., 'Asia/Yekaterinburg' for Birsk). "
|
| 54 |
+
"Describe the steps and write code. Always return the result via `final_answer()`."
|
| 55 |
+
)
|
| 56 |
+
},
|
| 57 |
+
"managed_agent": (
|
| 58 |
+
"Execute the task: {{question}}. Use tools as specified in the plan. "
|
| 59 |
+
"Ensure the code is correct and uses only provided tools. "
|
| 60 |
+
"Return the result via `final_answer()`."
|
| 61 |
+
),
|
| 62 |
+
"final_answer": "Final response: {{answer}}"
|
| 63 |
+
}
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
+
tools=[final_answer, get_current_time_in_timezone],
|
| 68 |
+
max_steps=2,
|
| 69 |
+
verbosity_level=2,
|
| 70 |
+
prompt_templates=prompt_templates,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
+
def process_input(user_input):
|
| 74 |
+
try:
|
| 75 |
+
response = agent.run(user_input)
|
| 76 |
+
return response
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return f"Error: {str(e)}"
|
| 79 |
|
| 80 |
+
gr.Interface(
|
| 81 |
+
fn=process_input,
|
| 82 |
+
inputs="text",
|
| 83 |
+
outputs="text",
|
| 84 |
+
title="Helpful Assistant",
|
| 85 |
+
description="Hello! What city or town are you from? I can tell you the time, weather, air quality, and more!"
|
| 86 |
+
).launch()
|