YeeJun02 commited on
Commit
ca8cbb1
·
verified ·
1 Parent(s): 329f3a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -4,7 +4,7 @@ import datetime
4
  import pytz
5
  import asyncio
6
 
7
- # Framework 1: LlamaIndex (Updated to use Workflow for stability)
8
  from llama_index.core.agent.workflow import AgentWorkflow
9
  from llama_index.core.tools import FunctionTool
10
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
@@ -16,7 +16,7 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientMod
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
 
18
  # ==========================================
19
- # PART 1: LLAMAINDEX AGENT (Using Workflow)
20
  # ==========================================
21
  li_llm = HuggingFaceInferenceAPI(
22
  model_name="Qwen/Qwen2.5-7B-Instruct",
@@ -30,17 +30,18 @@ def get_tokyo_time() -> str:
30
 
31
  li_tools = [FunctionTool.from_defaults(fn=get_tokyo_time)]
32
 
33
- # The Workflow approach is much more stable against Pydantic errors
34
  li_agent = AgentWorkflow.from_tools_or_functions(
35
- tools=li_tools,
36
  llm=li_llm,
 
37
  )
38
 
39
- def chat_llama(message, history):
40
  try:
41
- # AgentWorkflow uses async .run()
42
- response = asyncio.run(li_agent.run(input=message))
43
- return str(response)
44
  except Exception as e:
45
  return f"LlamaIndex Error: {str(e)}"
46
 
@@ -67,21 +68,24 @@ smol_agent = CodeAgent(
67
 
68
  def chat_smol(message, history):
69
  try:
 
70
  response = smol_agent.run(message)
71
  return str(response)
72
  except Exception as e:
73
  return f"Smolagents Error: {str(e)}"
74
 
75
  # ==========================================
76
- # PART 3: GRADIO UI
77
  # ==========================================
78
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
79
- gr.Markdown("# 🤖 Dual-Agent Testing Space")
80
 
81
- with gr.Tab("LlamaIndex (AgentWorkflow)"):
 
82
  gr.ChatInterface(fn=chat_llama)
83
 
84
  with gr.Tab("smolagents (CodeAgent)"):
 
85
  gr.ChatInterface(fn=chat_smol)
86
 
87
  if __name__ == "__main__":
 
4
  import pytz
5
  import asyncio
6
 
7
+ # Framework 1: LlamaIndex
8
  from llama_index.core.agent.workflow import AgentWorkflow
9
  from llama_index.core.tools import FunctionTool
10
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
 
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",
 
30
 
31
  li_tools = [FunctionTool.from_defaults(fn=get_tokyo_time)]
32
 
33
+ # FIX: Passed li_tools as a positional argument (no 'tools=' key)
34
  li_agent = AgentWorkflow.from_tools_or_functions(
35
+ li_tools,
36
  llm=li_llm,
37
+ system_prompt="You are a helpful assistant with access to a Tokyo time tool."
38
  )
39
 
40
+ async def chat_llama(message, history):
41
  try:
42
+ # AgentWorkflow.run returns a handler; we await the final result
43
+ result = await li_agent.run(user_msg=message)
44
+ return str(result)
45
  except Exception as e:
46
  return f"LlamaIndex Error: {str(e)}"
47
 
 
68
 
69
  def chat_smol(message, history):
70
  try:
71
+ # smolagents .run() is synchronous by default
72
  response = smol_agent.run(message)
73
  return str(response)
74
  except Exception as e:
75
  return f"Smolagents Error: {str(e)}"
76
 
77
  # ==========================================
78
+ # PART 3: UNIFIED GRADIO UI
79
  # ==========================================
80
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
81
+ gr.Markdown("# 🤖 Dual-Agent Testing Space (2026 Edition)")
82
 
83
+ with gr.Tab("LlamaIndex (Workflow)"):
84
+ gr.Markdown("Uses an **Event-Driven Workflow** for reasoning.")
85
  gr.ChatInterface(fn=chat_llama)
86
 
87
  with gr.Tab("smolagents (CodeAgent)"):
88
+ gr.Markdown("Uses a **Code-Execution Loop** to solve tasks.")
89
  gr.ChatInterface(fn=chat_smol)
90
 
91
  if __name__ == "__main__":