Khaled Jamal
commited on
Commit
·
59f70d4
1
Parent(s):
ae7a494
app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,115 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
with
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
# Import the necessary classes and functions from smolagents
|
| 4 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 5 |
+
|
| 6 |
+
# Standard library imports
|
| 7 |
+
import datetime
|
| 8 |
+
import pytz
|
| 9 |
+
import yaml
|
| 10 |
+
|
| 11 |
+
# External imports
|
| 12 |
+
# TODO: uncomment the import statements
|
| 13 |
+
#import torch
|
| 14 |
+
#from transformers import pipeline
|
| 15 |
+
|
| 16 |
+
# Import custom final answer tool and Gradio UI
|
| 17 |
+
from tools.final_answer import FinalAnswerTool
|
| 18 |
+
from Gradio_UI import GradioUI
|
| 19 |
+
|
| 20 |
+
# Initialize the Transformer-based sentiment analysis pipeline
|
| 21 |
+
#TODO: uncomment when testing using the real transformers
|
| 22 |
+
#sentiment_pipeline = pipeline("sentiment-analysis")
|
| 23 |
+
|
| 24 |
+
@tool
|
| 25 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 26 |
+
"""A tool that does nothing yet
|
| 27 |
+
Args:
|
| 28 |
+
arg1: the first argument
|
| 29 |
+
arg2: the second argument
|
| 30 |
+
"""
|
| 31 |
+
return "What magic will you build?"
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 35 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 36 |
+
Args:
|
| 37 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 38 |
+
"""
|
| 39 |
+
try:
|
| 40 |
+
tz = pytz.timezone(timezone)
|
| 41 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 42 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 45 |
+
|
| 46 |
+
@tool
|
| 47 |
+
def advanced_sentiment_tool(text: str) -> str:
|
| 48 |
+
"""A tool that uses a pre-trained transformer model to do sentiment analysis.
|
| 49 |
+
Args:
|
| 50 |
+
text: The text to analyze for sentiment.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
# TODO: uncomment later. for now test with hardcoded value first. later test using the real model
|
| 54 |
+
# TODO: also uncomment the import statements
|
| 55 |
+
#analysis = sentiment_pipeline(text)
|
| 56 |
+
#label = analysis[0]['label']
|
| 57 |
+
#score = analysis[0]['score']
|
| 58 |
+
|
| 59 |
+
label = "positive"
|
| 60 |
+
score = "0.99"
|
| 61 |
+
|
| 62 |
+
return f"Sentiment: {label} (confidence: {score:.4f})"
|
| 63 |
+
|
| 64 |
+
@tool
|
| 65 |
+
def simple_sentiment_tool(text: str) -> str:
|
| 66 |
+
"""A tool that uses a pre-trained transformer model to do sentiment analysis.
|
| 67 |
+
Args:
|
| 68 |
+
text: The text to analyze for sentiment.
|
| 69 |
+
"""
|
| 70 |
+
text = text.lower()
|
| 71 |
+
|
| 72 |
+
if "happy" in text:
|
| 73 |
+
return "Sentiment: Joyful (confidence: 1.00)"
|
| 74 |
+
elif "sad" in text:
|
| 75 |
+
return "Sentiment: Sorrowful (confidence: 1.00)"
|
| 76 |
+
|
| 77 |
+
label = "positive"
|
| 78 |
+
score = "0.99"
|
| 79 |
+
|
| 80 |
+
return f"Sentiment: {label} (confidence: {score:.4f})"
|
| 81 |
+
|
| 82 |
+
# Final answer tool
|
| 83 |
+
final_answer = FinalAnswerTool()
|
| 84 |
+
|
| 85 |
+
# Initialize the model
|
| 86 |
+
model = HfApiModel(
|
| 87 |
+
max_tokens=2096,
|
| 88 |
+
temperature=0.5,
|
| 89 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 90 |
+
custom_role_conversions=None,
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Load an image generation tool (unrelated, just for demonstration)
|
| 94 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 95 |
+
|
| 96 |
+
# Load prompt templates
|
| 97 |
+
with open("prompts.yaml", 'r') as stream:
|
| 98 |
+
prompt_templates = yaml.safe_load(stream)
|
| 99 |
+
|
| 100 |
+
# Initialize the agent, including the sentiment analysis tool
|
| 101 |
+
agent = CodeAgent(
|
| 102 |
+
model=model,
|
| 103 |
+
# TODO: use advanced_sentiment_tool later after testing using the simpler tool is done
|
| 104 |
+
tools=[final_answer, simple_sentiment_tool],
|
| 105 |
+
max_steps=6,
|
| 106 |
+
verbosity_level=1,
|
| 107 |
+
grammar=None,
|
| 108 |
+
planning_interval=None,
|
| 109 |
+
name=None,
|
| 110 |
+
description=None,
|
| 111 |
+
prompt_templates=prompt_templates
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Launch the Gradio UI
|
| 115 |
+
GradioUI(agent).launch()
|