Khaled Jamal commited on
Commit
59f70d4
·
1 Parent(s): ae7a494
Files changed (1) hide show
  1. app.py +115 -69
app.py CHANGED
@@ -1,69 +1,115 @@
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
-
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
- """A tool that fetches the current local time in a specified timezone.
24
- Args:
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
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
-
42
- model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
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
- # 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=[final_answer], ## add your tools here (don't remove final answer)
59
- max_steps=6,
60
- verbosity_level=1,
61
- grammar=None,
62
- planning_interval=None,
63
- name=None,
64
- description=None,
65
- prompt_templates=prompt_templates
66
- )
67
-
68
-
69
- GradioUI(agent).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()