Wayne0102 commited on
Commit
e72cc60
·
verified ·
1 Parent(s): cc9d817

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -33
app.py CHANGED
@@ -12,14 +12,12 @@ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
12
  # Framework 2: smolagents
13
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
14
 
15
- # 0. CONFIG & ASSETS
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
  MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
18
 
19
- # CUSTOM CSS for better styling
20
  custom_css = """
21
  .agent-header { text-align: center; margin-bottom: 20px; }
22
- .tool-tag { background-color: #f0f2f6; border-radius: 5px; padding: 2px 8px; font-size: 0.8em; }
23
  """
24
 
25
  # ==========================================
@@ -39,7 +37,6 @@ li_agent = AgentWorkflow.from_tools_or_functions(
39
 
40
  async def chat_llama(message, history):
41
  try:
42
- # LlamaIndex Workflows are async-native
43
  result = await li_agent.run(user_msg=message)
44
  return str(result)
45
  except Exception as e:
@@ -60,47 +57,42 @@ def weather_tool(location: str) -> str:
60
  """
61
  return f"The weather in {location} is currently sunny and 22°C."
62
 
63
- # smolagents can capture logs in a buffer for the UI
64
  smol_agent = CodeAgent(
65
  model=smol_model,
66
  tools=[weather_tool, DuckDuckGoSearchTool()],
67
- add_base_tools=True # Adds useful defaults like line_plot
68
  )
69
 
70
  def chat_smol(message, history):
71
  try:
72
- # We wrap the run to return a clean string
73
  response = smol_agent.run(message)
74
  return str(response)
75
  except Exception as e:
76
  return f"❌ Smolagents Error: {str(e)}"
77
 
78
  # ==========================================
79
- # PART 3: IMPROVED GRADIO UI
80
  # ==========================================
81
- with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
82
 
83
- with gr.Row(elem_classes="agent-header"):
84
- gr.Markdown(f"""
85
- # 🤖 Multi-Framework Agent Playground
86
- **Model:** `{MODEL_ID}` | **Provider:** `Together AI`
87
- """)
88
 
89
  with gr.Tabs():
90
  # TAB 1: LLAMAINDEX
91
  with gr.Tab("🏗️ LlamaIndex Workflow"):
92
  with gr.Row():
93
  with gr.Column(scale=4):
 
94
  gr.ChatInterface(
95
  fn=chat_llama,
96
- examples=["What time is it in Tokyo?", "Explain how workflows work."],
97
- type="messages"
98
  )
99
  with gr.Column(scale=1):
100
  gr.Markdown("### Agent Specs")
101
- gr.Markdown("**Type:** Workflow-based Agent")
102
- gr.Markdown("**Tools:** `get_tokyo_time`")
103
- gr.Info("LlamaIndex Workflows use an event-driven architecture, ideal for complex, multi-step logic.")
104
 
105
  # TAB 2: SMOLAGENTS
106
  with gr.Tab("💻 smolagents CodeAgent"):
@@ -108,22 +100,12 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
108
  with gr.Column(scale=4):
109
  gr.ChatInterface(
110
  fn=chat_smol,
111
- examples=["What is the weather in Paris?", "Search for the latest AI news."],
112
- type="messages"
113
  )
114
  with gr.Column(scale=1):
115
  gr.Markdown("### Agent Specs")
116
- gr.Markdown("**Type:** Code Executor")
117
- gr.Markdown("**Tools:** `weather_tool`, `DuckDuckGo`")
118
- gr.Warning("smolagents writes and executes Python code to solve tasks.")
119
-
120
- with gr.Accordion("⚙️ System Configuration", open=False):
121
- gr.JSON(value={
122
- "model": MODEL_ID,
123
- "status": "Connected",
124
- "timezone": "UTC",
125
- "frameworks": ["LlamaIndex 0.11+", "smolagents 1.0+"]
126
- })
127
 
128
  if __name__ == "__main__":
129
- demo.launch()
 
 
12
  # Framework 2: smolagents
13
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
14
 
15
+ # 0. CONFIG
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
  MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
18
 
 
19
  custom_css = """
20
  .agent-header { text-align: center; margin-bottom: 20px; }
 
21
  """
22
 
23
  # ==========================================
 
37
 
38
  async def chat_llama(message, history):
39
  try:
 
40
  result = await li_agent.run(user_msg=message)
41
  return str(result)
42
  except Exception as e:
 
57
  """
58
  return f"The weather in {location} is currently sunny and 22°C."
59
 
 
60
  smol_agent = CodeAgent(
61
  model=smol_model,
62
  tools=[weather_tool, DuckDuckGoSearchTool()],
63
+ add_base_tools=True
64
  )
65
 
66
  def chat_smol(message, history):
67
  try:
 
68
  response = smol_agent.run(message)
69
  return str(response)
70
  except Exception as e:
71
  return f"❌ Smolagents Error: {str(e)}"
72
 
73
  # ==========================================
74
+ # PART 3: REVISED GRADIO UI (v6.0 Compatible)
75
  # ==========================================
76
+ with gr.Blocks() as demo:
77
 
78
+ gr.Markdown(f"""
79
+ # 🤖 Multi-Framework Agent Playground
80
+ **Model:** `{MODEL_ID}` | **Provider:** `Together AI`
81
+ """, elem_classes="agent-header")
 
82
 
83
  with gr.Tabs():
84
  # TAB 1: LLAMAINDEX
85
  with gr.Tab("🏗️ LlamaIndex Workflow"):
86
  with gr.Row():
87
  with gr.Column(scale=4):
88
+ # Removed 'type' argument for compatibility
89
  gr.ChatInterface(
90
  fn=chat_llama,
91
+ examples=["What time is it in Tokyo?"]
 
92
  )
93
  with gr.Column(scale=1):
94
  gr.Markdown("### Agent Specs")
95
+ gr.Info("LlamaIndex handles complex multi-step logic via event-driven workflows.")
 
 
96
 
97
  # TAB 2: SMOLAGENTS
98
  with gr.Tab("💻 smolagents CodeAgent"):
 
100
  with gr.Column(scale=4):
101
  gr.ChatInterface(
102
  fn=chat_smol,
103
+ examples=["What is the weather in Paris?"]
 
104
  )
105
  with gr.Column(scale=1):
106
  gr.Markdown("### Agent Specs")
107
+ gr.Warning("smolagents writes and executes Python code locally.")
 
 
 
 
 
 
 
 
 
 
108
 
109
  if __name__ == "__main__":
110
+ # Moved theme and css here for Gradio 6.0
111
+ demo.launch(theme=gr.themes.Soft(), css=custom_css)