Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,8 @@ import time
|
|
9 |
import openai
|
10 |
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
11 |
import random
|
|
|
|
|
12 |
|
13 |
# (Keep Constants as is)
|
14 |
# --- Constants ---
|
@@ -16,6 +18,95 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
16 |
|
17 |
# --- Basic Agent Definition ---
|
18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# --- Retry Helper for Agent Call ---
|
20 |
def safe_agent_call(agent, question, retries=5, wait_time=20):
|
21 |
"""
|
@@ -43,6 +134,9 @@ class BasicAgent:
|
|
43 |
tools=[
|
44 |
DuckDuckGoSearchTool(),
|
45 |
WikipediaSearchTool(),
|
|
|
|
|
|
|
46 |
],
|
47 |
add_base_tools=True,
|
48 |
)
|
|
|
9 |
import openai
|
10 |
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
11 |
import random
|
12 |
+
import re
|
13 |
+
from collections import Counter
|
14 |
|
15 |
# (Keep Constants as is)
|
16 |
# --- Constants ---
|
|
|
18 |
|
19 |
# --- Basic Agent Definition ---
|
20 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
21 |
+
|
22 |
+
|
23 |
+
class TextSummarizationTool(Tool):
|
24 |
+
"""Summarizes a long text into a concise version by extracting leading sentences."""
|
25 |
+
|
26 |
+
name = "text_summarization"
|
27 |
+
description = "Summarizes a long input text into a short paragraph."
|
28 |
+
|
29 |
+
inputs = {
|
30 |
+
"text": {
|
31 |
+
"type": "string",
|
32 |
+
"description": "The long text that needs to be summarized.",
|
33 |
+
}
|
34 |
+
}
|
35 |
+
output_type = "string"
|
36 |
+
|
37 |
+
def forward(self, text: str) -> str:
|
38 |
+
try:
|
39 |
+
sentences = text.split('. ')
|
40 |
+
if len(sentences) <= 3:
|
41 |
+
return text
|
42 |
+
return '. '.join(sentences[:3]) + '.'
|
43 |
+
except Exception as e:
|
44 |
+
return f"Error summarizing text: {e}"
|
45 |
+
|
46 |
+
class KeywordExtractorTool(Tool):
|
47 |
+
"""Extracts top keywords from a given block of text based on frequency."""
|
48 |
+
|
49 |
+
name = "keyword_extractor"
|
50 |
+
description = "Extracts the most frequent keywords from the provided text."
|
51 |
+
|
52 |
+
inputs = {
|
53 |
+
"text": {
|
54 |
+
"type": "string",
|
55 |
+
"description": "The text to analyze for keywords.",
|
56 |
+
}
|
57 |
+
}
|
58 |
+
output_type = "string"
|
59 |
+
|
60 |
+
def forward(self, text: str) -> str:
|
61 |
+
try:
|
62 |
+
words = re.findall(r'\b\w+\b', text.lower())
|
63 |
+
stop_words = {'the', 'and', 'is', 'in', 'it', 'of', 'to', 'a'}
|
64 |
+
filtered_words = [w for w in words if w not in stop_words]
|
65 |
+
word_counts = Counter(filtered_words)
|
66 |
+
keywords = ', '.join(word for word, _ in word_counts.most_common(5))
|
67 |
+
return keywords
|
68 |
+
except Exception as e:
|
69 |
+
return f"Error extracting keywords: {e}"
|
70 |
+
|
71 |
+
class TextTranslationTool(Tool):
|
72 |
+
"""Translates simple words from source to target language using a dictionary lookup."""
|
73 |
+
|
74 |
+
name = "text_translation"
|
75 |
+
description = "Translates simple words from English to Spanish using a fixed dictionary."
|
76 |
+
|
77 |
+
inputs = {
|
78 |
+
"text": {
|
79 |
+
"type": "string",
|
80 |
+
"description": "The text to translate word-by-word.",
|
81 |
+
},
|
82 |
+
"source_lang": {
|
83 |
+
"type": "string",
|
84 |
+
"description": "Source language code (e.g., 'en').",
|
85 |
+
},
|
86 |
+
"target_lang": {
|
87 |
+
"type": "string",
|
88 |
+
"description": "Target language code (e.g., 'es').",
|
89 |
+
}
|
90 |
+
}
|
91 |
+
output_type = "string"
|
92 |
+
|
93 |
+
def __init__(self):
|
94 |
+
self.translation_dict = {
|
95 |
+
'hello': 'hola',
|
96 |
+
'world': 'mundo',
|
97 |
+
'goodbye': 'adiós',
|
98 |
+
'thank': 'gracias',
|
99 |
+
'you': 'tú'
|
100 |
+
}
|
101 |
+
|
102 |
+
def forward(self, text: str, source_lang: str, target_lang: str) -> str:
|
103 |
+
try:
|
104 |
+
words = text.split()
|
105 |
+
translated_words = [self.translation_dict.get(word.lower(), word) for word in words]
|
106 |
+
return ' '.join(translated_words)
|
107 |
+
except Exception as e:
|
108 |
+
return f"Error translating text: {e}"
|
109 |
+
|
110 |
# --- Retry Helper for Agent Call ---
|
111 |
def safe_agent_call(agent, question, retries=5, wait_time=20):
|
112 |
"""
|
|
|
134 |
tools=[
|
135 |
DuckDuckGoSearchTool(),
|
136 |
WikipediaSearchTool(),
|
137 |
+
KeywordExtractorTool(),
|
138 |
+
TextSummarizationTool(),
|
139 |
+
TextTranslationTool()
|
140 |
],
|
141 |
add_base_tools=True,
|
142 |
)
|