Update app.py
#571
by
muztechiee
- opened
app.py
CHANGED
|
@@ -1,23 +1,36 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool,
|
| 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 |
|
| 10 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -25,45 +38,58 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 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"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
|
| 42 |
-
model =
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 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=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
description=
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# --- 1. Islamic Info Tool ---
|
| 10 |
@tool
|
| 11 |
+
def get_islamic_prayer_times(city: str, country: str) -> str:
|
| 12 |
+
"""Fetches the Islamic prayer times for a specific city and country.
|
|
|
|
| 13 |
Args:
|
| 14 |
+
city: The name of the city (e.g., 'London').
|
| 15 |
+
country: The name of the country (e.g., 'UK').
|
| 16 |
"""
|
| 17 |
+
try:
|
| 18 |
+
# Using the Aladhan API to get timings
|
| 19 |
+
url = f"http://api.aladhan.com/v1/timingsByCity?city={city}&country={country}&method=2"
|
| 20 |
+
response = requests.get(url)
|
| 21 |
+
data = response.json()
|
| 22 |
+
if data['code'] == 200:
|
| 23 |
+
timings = data['data']['timings']
|
| 24 |
+
date = data['data']['date']['readable']
|
| 25 |
+
return (f"Prayer times for {city}, {country} on {date}:\n"
|
| 26 |
+
f"Fajr: {timings['Fajr']}, Dhuhr: {timings['Dhuhr']}, "
|
| 27 |
+
f"Asr: {timings['Asr']}, Maghrib: {timings['Maghrib']}, Isha: {timings['Isha']}")
|
| 28 |
+
else:
|
| 29 |
+
return f"Could not find prayer times for {city}. Please check the spelling."
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error fetching prayer times: {str(e)}"
|
| 32 |
|
| 33 |
+
# --- 2. Timezone Tool ---
|
| 34 |
@tool
|
| 35 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 36 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 38 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 39 |
"""
|
| 40 |
try:
|
|
|
|
| 41 |
tz = pytz.timezone(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 |
+
# --- 3. Placeholder Tool (Customizable) ---
|
| 48 |
+
@tool
|
| 49 |
+
def my_custom_tool(arg1:str, arg2:int)-> str:
|
| 50 |
+
"""A tool that currently returns a greeting.
|
| 51 |
+
Args:
|
| 52 |
+
arg1: A name or string.
|
| 53 |
+
arg2: An integer.
|
| 54 |
+
"""
|
| 55 |
+
return f"Hello {arg1}, your magic number is {arg2}!"
|
| 56 |
|
| 57 |
+
# --- 4. Setup Model & Tools ---
|
| 58 |
final_answer = FinalAnswerTool()
|
| 59 |
|
| 60 |
+
# Load the image generation tool from the Hub
|
| 61 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 62 |
|
| 63 |
+
model = InferenceClientModel(
|
| 64 |
+
max_tokens=2096,
|
| 65 |
+
temperature=0.5,
|
| 66 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 67 |
+
custom_role_conversions=None,
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Load system prompt
|
|
|
|
|
|
|
|
|
|
| 71 |
with open("prompts.yaml", 'r') as stream:
|
| 72 |
prompt_templates = yaml.safe_load(stream)
|
| 73 |
+
|
| 74 |
+
# --- 5. Initialize Agent ---
|
| 75 |
agent = CodeAgent(
|
| 76 |
model=model,
|
| 77 |
+
tools=[
|
| 78 |
+
final_answer,
|
| 79 |
+
get_current_time_in_timezone,
|
| 80 |
+
get_islamic_prayer_times,
|
| 81 |
+
image_generation_tool,
|
| 82 |
+
my_custom_tool
|
| 83 |
+
],
|
| 84 |
max_steps=6,
|
| 85 |
verbosity_level=1,
|
| 86 |
grammar=None,
|
| 87 |
planning_interval=None,
|
| 88 |
+
name="SmartAssistant",
|
| 89 |
+
description="An agent that can tell time, provide prayer schedules, and generate art.",
|
| 90 |
prompt_templates=prompt_templates
|
| 91 |
)
|
| 92 |
|
| 93 |
+
# --- 6. Launch UI ---
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
GradioUI(agent).launch()
|