jcleee commited on
Commit
c6b256a
·
verified ·
1 Parent(s): cb925ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -103
app.py CHANGED
@@ -1,106 +1,14 @@
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, LiteLLMModel, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
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
- @tool
26
- def get_current_time_in_timezone(timezone: str) -> str:
27
- """Fetches the current local time in a specified timezone.
28
- Args:
29
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
30
- """
31
- try:
32
- tz = pytz.timezone(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
- """
44
- try:
45
- response = requests.get(url, timeout=5)
46
- return response.text[:5000] # Limit length
47
- except Exception as e:
48
- return f"[ERROR fetching {url}]: {str(e)}"
49
-
50
- @tool
51
- def text_splitter(text: str) -> List[str]:
52
- """Splits text into chunks using LangChain's CharacterTextSplitter.
53
- Args:
54
- text: A string of text to split.
55
- """
56
- splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
57
- return splitter.split_text(text)
58
-
59
- # === FINAL ANSWER TOOL ===
60
- final_answer = FinalAnswerTool()
61
-
62
- # === LOAD PROMPT TEMPLATES ===
63
- with open("prompts.yaml", "r") as stream:
64
- prompt_templates = yaml.safe_load(stream)
65
-
66
- # === LOAD agent.json CONFIG ===
67
- with open("agent.json", "r") as f:
68
- agent_config = yaml.safe_load(f)
69
-
70
- model_config = agent_config["model"]["data"]
71
-
72
- # === BUILD MODEL ===
73
- model = LiteLLMModel(
74
- model_id="gemini/gemini-2.0-flash-lite",
75
- api_key=os.getenv("GEMINI_API_KEY"),
76
- temperature=0.5,
77
- max_tokens=1024,
78
- )
79
-
80
-
81
-
82
- # === IMPORT TOOL FROM HUB ===
83
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
84
-
85
- # === BUILD AGENT ===
86
- agent = CodeAgent(
87
- model=model,
88
- tools=[
89
- final_answer,
90
- web_search,
91
- get_current_time_in_timezone,
92
- visit_webpage,
93
- text_splitter,
94
- image_generation_tool
95
- ],
96
- max_steps=6,
97
- verbosity_level=1,
98
- grammar=None,
99
- planning_interval=None,
100
- name=None,
101
- description=None,
102
- prompt_templates=prompt_templates
103
- )
104
 
105
- # === LAUNCH UI ===
106
- GradioUI(agent).launch()
 
1
+ import gradio as gr
2
+ from agent import agent # Import your refactored agent
 
 
 
 
 
 
 
 
3
  from Gradio_UI import GradioUI
4
 
5
+ # Optional: Uncomment to test agent endpoint separately
6
+ # def run_agent(question):
7
+ # try:
8
+ # result = agent(question)
9
+ # return [str(result)]
10
+ # except Exception as e:
11
+ # return [f"Error: {e}"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ if __name__ == "__main__":
14
+ GradioUI(agent).launch(debug=True, share=True)