YeeJun02 commited on
Commit
e34cf5b
·
verified ·
1 Parent(s): 5147ad1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -2,23 +2,21 @@ import os
2
  import gradio as gr
3
  import datetime
4
  import pytz
5
- import math
6
  import requests
7
- from deep_translator import GoogleTranslator
8
 
9
- # Framework 1: LlamaIndex (Core components for Agent Construction)
10
- from llama_index.core.agent import AgentRunner, ReActAgentWorker
11
  from llama_index.core.tools import FunctionTool
12
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
13
 
14
- # Framework 2: smolagents (Updated Model Class)
15
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
16
 
17
  # 0. SHARED CONFIG
18
  HF_TOKEN = os.getenv("HF_TOKEN")
19
 
20
  # ==========================================
21
- # PART 1: LLAMAINDEX AGENT (Fixed Initialization)
22
  # ==========================================
23
  li_llm = HuggingFaceInferenceAPI(
24
  model_name="Qwen/Qwen2.5-7B-Instruct",
@@ -27,20 +25,18 @@ li_llm = HuggingFaceInferenceAPI(
27
  )
28
 
29
  def get_tokyo_time() -> str:
30
- """Returns the current time in Tokyo."""
31
  tz = pytz.timezone('Asia/Tokyo')
32
- return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
33
 
34
  li_tools = [FunctionTool.from_defaults(fn=get_tokyo_time)]
35
 
36
- # FIX: Using ReActAgentWorker + AgentRunner instead of ReActAgent.from_tools
37
- # This bypasses the Pydantic AttributeError issue
38
- worker = ReActAgentWorker.from_tools(
39
- tools=li_tools,
40
- llm=li_llm,
41
  verbose=True
42
  )
43
- li_agent = AgentRunner(worker)
44
 
45
  def chat_llama(message, history):
46
  try:
@@ -63,12 +59,11 @@ def weather_tool(location: str) -> str:
63
  Args:
64
  location: The city name.
65
  """
66
- return f"The weather in {location} is sunny and 25°C."
67
 
68
  smol_agent = CodeAgent(
69
  model=smol_model,
70
- tools=[weather_tool, DuckDuckGoSearchTool()],
71
- additional_authorized_imports=['math', 'requests', 'pytz', 'datetime']
72
  )
73
 
74
  def chat_smol(message, history):
@@ -82,13 +77,12 @@ def chat_smol(message, history):
82
  # PART 3: UNIFIED GRADIO UI
83
  # ==========================================
84
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
85
- gr.Markdown("# 🤖 Multi-Framework Agent Space")
86
- gr.Markdown("Compare the two leading agent frameworks side-by-side.")
87
 
88
- with gr.Tab("LlamaIndex (ReAct Agent)"):
89
  gr.ChatInterface(fn=chat_llama)
90
 
91
- with gr.Tab("smolagents (Code Agent)"):
92
  gr.ChatInterface(fn=chat_smol)
93
 
94
  if __name__ == "__main__":
 
2
  import gradio as gr
3
  import datetime
4
  import pytz
 
5
  import requests
 
6
 
7
+ # Framework 1: LlamaIndex - Using the most direct, stable imports
8
+ from llama_index.core.agent import ReActAgent
9
  from llama_index.core.tools import FunctionTool
10
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
11
 
12
+ # Framework 2: smolagents
13
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
14
 
15
  # 0. SHARED CONFIG
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
 
18
  # ==========================================
19
+ # PART 1: LLAMAINDEX AGENT
20
  # ==========================================
21
  li_llm = HuggingFaceInferenceAPI(
22
  model_name="Qwen/Qwen2.5-7B-Instruct",
 
25
  )
26
 
27
  def get_tokyo_time() -> str:
28
+ """Returns the current time in Tokyo, Japan."""
29
  tz = pytz.timezone('Asia/Tokyo')
30
+ return f"The current time is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
31
 
32
  li_tools = [FunctionTool.from_defaults(fn=get_tokyo_time)]
33
 
34
+ # Using the class-based initialization which is now corrected in latest 0.10+ versions
35
+ li_agent = ReActAgent.from_tools(
36
+ tools=li_tools,
37
+ llm=li_llm,
 
38
  verbose=True
39
  )
 
40
 
41
  def chat_llama(message, history):
42
  try:
 
59
  Args:
60
  location: The city name.
61
  """
62
+ return f"The weather in {location} is currently sunny and 22°C."
63
 
64
  smol_agent = CodeAgent(
65
  model=smol_model,
66
+ tools=[weather_tool, DuckDuckGoSearchTool()]
 
67
  )
68
 
69
  def chat_smol(message, history):
 
77
  # PART 3: UNIFIED GRADIO UI
78
  # ==========================================
79
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
80
+ gr.Markdown("# 🤖 Multi-Framework AI Space")
 
81
 
82
+ with gr.Tab("LlamaIndex (ReAct)"):
83
  gr.ChatInterface(fn=chat_llama)
84
 
85
+ with gr.Tab("smolagents (Code)"):
86
  gr.ChatInterface(fn=chat_smol)
87
 
88
  if __name__ == "__main__":