Khaled Jamal
commited on
Commit
·
0c10235
1
Parent(s):
59f70d4
added a tool to that uses a pre-trained transformer model to do sentiment analysis
Browse files- .gitignore +2 -0
- app.py +23 -31
- requirements.txt +2 -1
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
etc/
|
2 |
+
myenv/
|
app.py
CHANGED
@@ -9,17 +9,15 @@ import pytz
|
|
9 |
import yaml
|
10 |
|
11 |
# External imports
|
12 |
-
|
13 |
-
|
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 |
-
|
22 |
-
#sentiment_pipeline = pipeline("sentiment-analysis")
|
23 |
|
24 |
@tool
|
25 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
@@ -30,34 +28,19 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
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 |
-
#
|
54 |
-
#
|
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 |
|
@@ -79,6 +62,19 @@ def simple_sentiment_tool(text: str) -> str:
|
|
79 |
|
80 |
return f"Sentiment: {label} (confidence: {score:.4f})"
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
# Final answer tool
|
83 |
final_answer = FinalAnswerTool()
|
84 |
|
@@ -90,9 +86,6 @@ model = HfApiModel(
|
|
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)
|
@@ -100,8 +93,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
100 |
# Initialize the agent, including the sentiment analysis tool
|
101 |
agent = CodeAgent(
|
102 |
model=model,
|
103 |
-
|
104 |
-
tools=[final_answer, simple_sentiment_tool],
|
105 |
max_steps=6,
|
106 |
verbosity_level=1,
|
107 |
grammar=None,
|
|
|
9 |
import yaml
|
10 |
|
11 |
# External imports
|
12 |
+
import torch
|
13 |
+
from transformers import pipeline
|
|
|
14 |
|
15 |
# Import custom final answer tool and Gradio UI
|
16 |
from tools.final_answer import FinalAnswerTool
|
17 |
from Gradio_UI import GradioUI
|
18 |
|
19 |
# Initialize the Transformer-based sentiment analysis pipeline
|
20 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
|
|
21 |
|
22 |
@tool
|
23 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
28 |
"""
|
29 |
return "What magic will you build?"
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
@tool
|
32 |
def advanced_sentiment_tool(text: str) -> str:
|
33 |
"""A tool that uses a pre-trained transformer model to do sentiment analysis.
|
34 |
Args:
|
35 |
text: The text to analyze for sentiment.
|
36 |
"""
|
37 |
+
|
38 |
+
analysis = sentiment_pipeline(text)
|
39 |
+
label = analysis[0]['label']
|
40 |
+
score = analysis[0]['score']
|
41 |
|
42 |
+
#label = "positive"
|
43 |
+
#score = "0.99"
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
return f"Sentiment: {label} (confidence: {score:.4f})"
|
46 |
|
|
|
62 |
|
63 |
return f"Sentiment: {label} (confidence: {score:.4f})"
|
64 |
|
65 |
+
@tool
|
66 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
67 |
+
"""A tool that fetches the current local time in a specified timezone.
|
68 |
+
Args:
|
69 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
70 |
+
"""
|
71 |
+
try:
|
72 |
+
tz = pytz.timezone(timezone)
|
73 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
74 |
+
return f"The current local time in {timezone} is: {local_time}"
|
75 |
+
except Exception as e:
|
76 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
77 |
+
|
78 |
# Final answer tool
|
79 |
final_answer = FinalAnswerTool()
|
80 |
|
|
|
86 |
custom_role_conversions=None,
|
87 |
)
|
88 |
|
|
|
|
|
|
|
89 |
# Load prompt templates
|
90 |
with open("prompts.yaml", 'r') as stream:
|
91 |
prompt_templates = yaml.safe_load(stream)
|
|
|
93 |
# Initialize the agent, including the sentiment analysis tool
|
94 |
agent = CodeAgent(
|
95 |
model=model,
|
96 |
+
tools=[final_answer, advanced_sentiment_tool],
|
|
|
97 |
max_steps=6,
|
98 |
verbosity_level=1,
|
99 |
grammar=None,
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
markdownify
|
2 |
smolagents
|
3 |
requests
|
4 |
-
duckduckgo_search
|
5 |
pandas
|
|
|
|
|
|
1 |
markdownify
|
2 |
smolagents
|
3 |
requests
|
|
|
4 |
pandas
|
5 |
+
torch
|
6 |
+
transformers
|