Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
| 9 |
@tool
|
| 10 |
def solve_code_problem_with_explanation(problem_statement: str) -> str:
|
| 11 |
"""A tool that solves basic Python coding problems and explains the solution using the Qwen model.
|
|
@@ -25,48 +25,54 @@ Return:
|
|
| 25 |
|
| 26 |
Format your response like this:
|
| 27 |
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
@tool
|
| 33 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 34 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 35 |
Args:
|
| 36 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 37 |
"""
|
| 38 |
try:
|
| 39 |
-
# Create timezone object
|
| 40 |
tz = pytz.timezone(timezone)
|
| 41 |
-
# Get current time in that timezone
|
| 42 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 43 |
return f"The current local time in {timezone} is: {local_time}"
|
| 44 |
except Exception as e:
|
| 45 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 46 |
-
"""
|
| 47 |
|
|
|
|
| 48 |
final_answer = FinalAnswerTool()
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 52 |
-
|
| 53 |
model = HfApiModel(
|
| 54 |
-
max_tokens=2096,
|
| 55 |
-
temperature=0.5,
|
| 56 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 57 |
-
custom_role_conversions=None,
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# Import tool from Hub
|
| 62 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 63 |
|
|
|
|
| 64 |
with open("prompts.yaml", 'r') as stream:
|
| 65 |
prompt_templates = yaml.safe_load(stream)
|
| 66 |
-
|
|
|
|
| 67 |
agent = CodeAgent(
|
| 68 |
model=model,
|
| 69 |
-
tools=[final_answer, solve_code_problem_with_explanation],
|
| 70 |
max_steps=6,
|
| 71 |
verbosity_level=1,
|
| 72 |
grammar=None,
|
|
@@ -76,5 +82,5 @@ agent = CodeAgent(
|
|
| 76 |
prompt_templates=prompt_templates
|
| 77 |
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
+
|
| 8 |
+
# Tool 1: Solve Python coding problems and explain the solution
|
| 9 |
@tool
|
| 10 |
def solve_code_problem_with_explanation(problem_statement: str) -> str:
|
| 11 |
"""A tool that solves basic Python coding problems and explains the solution using the Qwen model.
|
|
|
|
| 25 |
|
| 26 |
Format your response like this:
|
| 27 |
|
| 28 |
+
# Code
|
| 29 |
+
<your function here>
|
| 30 |
|
| 31 |
+
# Explanation
|
| 32 |
+
<your explanation here>
|
| 33 |
+
"""
|
| 34 |
+
response = model.run(prompt)
|
| 35 |
+
return response
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error generating solution: {str(e)}"
|
| 38 |
|
| 39 |
+
# Tool 2: Get current time in a timezone
|
|
|
|
| 40 |
@tool
|
| 41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 42 |
"""A tool that fetches the current local time in a specified timezone.
|
| 43 |
+
|
| 44 |
Args:
|
| 45 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 46 |
"""
|
| 47 |
try:
|
|
|
|
| 48 |
tz = pytz.timezone(timezone)
|
|
|
|
| 49 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 50 |
return f"The current local time in {timezone} is: {local_time}"
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
|
|
|
| 53 |
|
| 54 |
+
# Final Answer Tool
|
| 55 |
final_answer = FinalAnswerTool()
|
| 56 |
|
| 57 |
+
# Load model (Qwen2.5 Coder)
|
|
|
|
|
|
|
| 58 |
model = HfApiModel(
|
| 59 |
+
max_tokens=2096,
|
| 60 |
+
temperature=0.5,
|
| 61 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # This must be available in the Space environment
|
| 62 |
+
custom_role_conversions=None,
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Optional: Import tool from remote tool hub
|
|
|
|
| 66 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 67 |
|
| 68 |
+
# Load prompt templates
|
| 69 |
with open("prompts.yaml", 'r') as stream:
|
| 70 |
prompt_templates = yaml.safe_load(stream)
|
| 71 |
+
|
| 72 |
+
# Define the agent
|
| 73 |
agent = CodeAgent(
|
| 74 |
model=model,
|
| 75 |
+
tools=[final_answer, solve_code_problem_with_explanation, get_current_time_in_timezone],
|
| 76 |
max_steps=6,
|
| 77 |
verbosity_level=1,
|
| 78 |
grammar=None,
|
|
|
|
| 82 |
prompt_templates=prompt_templates
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# Launch the Gradio UI
|
| 86 |
+
GradioUI(agent).launch()
|