Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,93 @@
|
|
| 1 |
-
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 8 |
from typing import List
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
| 12 |
-
#
|
|
|
|
| 13 |
@tool
|
| 14 |
-
def web_search(query:str) -> str:
|
| 15 |
-
"""Allows search through DuckDuckGo
|
| 16 |
Args:
|
| 17 |
query: what you want to search
|
| 18 |
"""
|
| 19 |
search_tool = DuckDuckGoSearchTool()
|
| 20 |
-
results = search_tool(query)
|
| 21 |
return "\n".join(results)
|
| 22 |
|
|
|
|
| 23 |
@tool
|
| 24 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
-
"""
|
| 26 |
Args:
|
| 27 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
-
# Create timezone object
|
| 31 |
tz = pytz.timezone(timezone)
|
| 32 |
-
# Get current time in that timezone
|
| 33 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
return f"The current local time in {timezone} is: {local_time}"
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 37 |
|
|
|
|
| 38 |
@tool
|
| 39 |
def visit_webpage(url: str) -> str:
|
| 40 |
"""Fetches raw HTML content of a web page.
|
| 41 |
Args:
|
| 42 |
-
url: The url of the webpage
|
|
|
|
| 43 |
try:
|
| 44 |
response = requests.get(url, timeout=5)
|
| 45 |
-
return response.text[:5000]
|
| 46 |
except Exception as e:
|
| 47 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 48 |
|
|
|
|
| 49 |
@tool
|
| 50 |
def text_splitter(text: str) -> List[str]:
|
| 51 |
-
"""
|
| 52 |
Args:
|
| 53 |
-
text: A string of text to
|
| 54 |
"""
|
| 55 |
-
c_splitter = CharacterTextSplitter(
|
| 56 |
-
|
| 57 |
-
chunk_overlap=10
|
| 58 |
-
)
|
| 59 |
-
split_text = c_splitter.split_text(text)
|
| 60 |
-
return split_text
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
|
|
|
|
| 64 |
|
| 65 |
final_answer = FinalAnswerTool()
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
token=os.getenv("HF_TOKEN"),
|
| 73 |
-
max_tokens=2096,
|
| 74 |
-
temperature=0.5,
|
| 75 |
-
last_input_token_count=0,
|
| 76 |
-
last_output_token_count=0,
|
| 77 |
-
)
|
| 78 |
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
with open("prompts.yaml", 'r') as stream:
|
| 84 |
prompt_templates = yaml.safe_load(stream)
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
agent = CodeAgent(
|
| 87 |
model=model,
|
| 88 |
-
tools=[final_answer, web_search, text_splitter, image_generation_tool],
|
| 89 |
max_steps=6,
|
| 90 |
verbosity_level=1,
|
| 91 |
grammar=None,
|
|
@@ -95,5 +97,6 @@ agent = CodeAgent(
|
|
| 95 |
prompt_templates=prompt_templates
|
| 96 |
)
|
| 97 |
|
|
|
|
| 98 |
|
| 99 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
| 6 |
from typing import List
|
| 7 |
+
|
| 8 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 9 |
+
from tools.final_answer import FinalAnswerTool
|
| 10 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool, from_config
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
| 13 |
+
# === TOOLS ===
|
| 14 |
+
|
| 15 |
@tool
|
| 16 |
+
def web_search(query: str) -> str:
|
| 17 |
+
"""Allows search through DuckDuckGo.
|
| 18 |
Args:
|
| 19 |
query: what you want to search
|
| 20 |
"""
|
| 21 |
search_tool = DuckDuckGoSearchTool()
|
| 22 |
+
results = search_tool(query)
|
| 23 |
return "\n".join(results)
|
| 24 |
|
| 25 |
+
|
| 26 |
@tool
|
| 27 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 28 |
+
"""Fetches the current local time in a specified timezone.
|
| 29 |
Args:
|
| 30 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 31 |
"""
|
| 32 |
try:
|
|
|
|
| 33 |
tz = pytz.timezone(timezone)
|
|
|
|
| 34 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 35 |
return f"The current local time in {timezone} is: {local_time}"
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 38 |
|
| 39 |
+
|
| 40 |
@tool
|
| 41 |
def visit_webpage(url: str) -> str:
|
| 42 |
"""Fetches raw HTML content of a web page.
|
| 43 |
Args:
|
| 44 |
+
url: The url of the webpage
|
| 45 |
+
"""
|
| 46 |
try:
|
| 47 |
response = requests.get(url, timeout=5)
|
| 48 |
+
return response.text[:5000]
|
| 49 |
except Exception as e:
|
| 50 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 51 |
|
| 52 |
+
|
| 53 |
@tool
|
| 54 |
def text_splitter(text: str) -> List[str]:
|
| 55 |
+
"""Splits text into chunks using LangChain's CharacterTextSplitter.
|
| 56 |
Args:
|
| 57 |
+
text: A string of text to split.
|
| 58 |
"""
|
| 59 |
+
c_splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
|
| 60 |
+
return c_splitter.split_text(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# === FINAL ANSWER TOOL ===
|
| 63 |
|
| 64 |
final_answer = FinalAnswerTool()
|
| 65 |
|
| 66 |
+
# === LOAD MODEL FROM agent.json + INJECT TOKEN SECURELY ===
|
|
|
|
| 67 |
|
| 68 |
+
with open("agent.json", "r") as f:
|
| 69 |
+
agent_config = yaml.safe_load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# Inject HF token securely
|
| 72 |
+
agent_config["model"]["data"]["token"] = os.getenv("HF_TOKEN")
|
| 73 |
|
| 74 |
+
# Construct the model safely
|
| 75 |
+
model = from_config(agent_config["model"])
|
| 76 |
+
|
| 77 |
+
# === IMPORT PROMPT TEMPLATES ===
|
| 78 |
|
| 79 |
with open("prompts.yaml", 'r') as stream:
|
| 80 |
prompt_templates = yaml.safe_load(stream)
|
| 81 |
+
|
| 82 |
+
# === IMPORT EXTRA TOOL FROM HUB ===
|
| 83 |
+
|
| 84 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 85 |
+
|
| 86 |
+
# === AGENT ===
|
| 87 |
+
|
| 88 |
agent = CodeAgent(
|
| 89 |
model=model,
|
| 90 |
+
tools=[final_answer, web_search, get_current_time_in_timezone, visit_webpage, text_splitter, image_generation_tool],
|
| 91 |
max_steps=6,
|
| 92 |
verbosity_level=1,
|
| 93 |
grammar=None,
|
|
|
|
| 97 |
prompt_templates=prompt_templates
|
| 98 |
)
|
| 99 |
|
| 100 |
+
# === LAUNCH UI ===
|
| 101 |
|
| 102 |
+
GradioUI(agent).launch()
|