jcleee commited on
Commit
8fa1deb
·
verified ·
1 Parent(s): 40a20d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -26
app.py CHANGED
@@ -7,7 +7,7 @@ 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 ===
@@ -22,7 +22,6 @@ def web_search(query: str) -> str:
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.
@@ -36,58 +35,65 @@ def get_current_time_in_timezone(timezone: str) -> str:
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,
@@ -98,5 +104,4 @@ agent = CodeAgent(
98
  )
99
 
100
  # === LAUNCH UI ===
101
-
102
  GradioUI(agent).launch()
 
7
 
8
  from langchain.text_splitter import CharacterTextSplitter
9
  from tools.final_answer import FinalAnswerTool
10
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
11
  from Gradio_UI import GradioUI
12
 
13
  # === TOOLS ===
 
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.
 
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 MANUALLY (SAFE FALLBACK) ===
73
+ model = HfApiModel(
74
+ model_id=model_config["model_id"],
75
+ token=os.getenv("HF_TOKEN"), # Injected securely from Space secret
76
+ temperature=model_config.get("temperature", 0.5),
77
+ max_tokens=model_config.get("max_tokens", 2048),
78
+ last_input_token_count=model_config.get("last_input_token_count", 0),
79
+ last_output_token_count=model_config.get("last_output_token_count", 0),
80
+ custom_role_conversions=model_config.get("custom_role_conversions", None),
81
+ )
82
 
83
+ # === IMPORT TOOL FROM HUB ===
84
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
85
 
86
+ # === BUILD AGENT ===
 
87
  agent = CodeAgent(
88
  model=model,
89
+ tools=[
90
+ final_answer,
91
+ web_search,
92
+ get_current_time_in_timezone,
93
+ visit_webpage,
94
+ text_splitter,
95
+ image_generation_tool
96
+ ],
97
  max_steps=6,
98
  verbosity_level=1,
99
  grammar=None,
 
104
  )
105
 
106
  # === LAUNCH UI ===
 
107
  GradioUI(agent).launch()