Wayne0102 commited on
Commit
e2f0e39
·
verified ·
1 Parent(s): f1f2114

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -68
app.py CHANGED
@@ -3,103 +3,107 @@ import gradio as gr
3
  import datetime
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
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
  MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
18
 
19
  # ==========================================
20
- # PART 1: LLAMAINDEX AGENT
21
  # ==========================================
22
  li_llm = HuggingFaceInferenceAPI(model_name=MODEL_ID, token=HF_TOKEN, provider="together")
23
 
24
- def get_tokyo_time() -> str:
25
- """Returns the current time in Tokyo, Japan."""
26
- tz = pytz.timezone('Asia/Tokyo')
27
- return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
28
-
29
  def get_weather(location: str) -> str:
30
- """
31
- Get the current weather for a specific location.
32
-
33
- Args:
34
- location: The city name to check weather for.
35
- """
36
- return f"LlamaIndex Tool: The weather in {location} is currently sunny and 22°C."
37
-
38
- li_tools = [
39
- FunctionTool.from_defaults(fn=get_tokyo_time),
40
- FunctionTool.from_defaults(fn=get_weather) # Added weather tool here
41
- ]
42
 
43
- li_agent = AgentWorkflow.from_tools_or_functions(li_tools, llm=li_llm)
 
 
 
44
 
45
- async def chat_llama(message, history):
46
- try:
47
- result = await li_agent.run(user_msg=message)
48
- return str(result)
49
- except Exception as e:
50
- return f"❌ LlamaIndex Error: {str(e)}"
51
 
52
  # ==========================================
53
- # PART 2: SMOLAGENTS
54
  # ==========================================
55
- smol_model = InferenceClientModel(model_id=MODEL_ID, token=HF_TOKEN, provider="together")
56
-
57
- @tool
58
- def weather_tool(location: str) -> str:
59
- """
60
- Get the current weather for a location.
61
-
62
- Args:
63
- location: The city and country, e.g., 'London, UK'.
64
- """
65
- return f"smolagents Tool: The weather in {location} is currently sunny and 22°C."
66
-
67
- smol_agent = CodeAgent(
68
- model=smol_model,
69
- tools=[weather_tool, DuckDuckGoSearchTool()],
70
- add_base_tools=True
71
- )
72
 
73
  def chat_smol(message, history):
 
 
 
 
 
 
 
 
 
74
  try:
75
  response = smol_agent.run(message)
76
- return str(response)
 
 
 
77
  except Exception as e:
78
- return f"❌ Smolagents Error: {str(e)}"
79
 
80
  # ==========================================
81
- # PART 3: GRADIO 6.0 UI
82
  # ==========================================
83
- with gr.Blocks() as demo:
84
- gr.Markdown("# 🤖 Consolidated AI Agent Space", elem_id="main-title")
85
- gr.Markdown(f"Currently using **{MODEL_ID}** via Together AI Provider.")
86
-
87
- with gr.Tabs():
88
- with gr.Tab("🏗️ LlamaIndex (Workflow)"):
89
- gr.ChatInterface(
90
- fn=chat_llama,
91
- examples=["What's the weather in Tokyo?", "What time is it in Japan?"]
92
- )
 
 
 
 
 
 
 
 
93
 
94
- with gr.Tab("💻 smolagents (CodeAgent)"):
95
- gr.ChatInterface(
96
- fn=chat_smol,
97
- examples=["Search for the latest AI news", "How is the weather in Paris?"]
98
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  if __name__ == "__main__":
101
- # Gradio 6.0: Move theme/css to launch()
102
  demo.launch(
103
- theme=gr.themes.Soft(),
104
- css="#main-title { text-align: center; margin-bottom: 20px; }"
105
  )
 
3
  import datetime
4
  import pytz
5
  import asyncio
6
+ from gradio import ChatMessage
7
 
8
+ # Frameworks
9
  from llama_index.core.agent.workflow import AgentWorkflow
10
  from llama_index.core.tools import FunctionTool
11
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
 
 
12
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
13
 
14
+ # 0. CONFIG
15
  HF_TOKEN = os.getenv("HF_TOKEN")
16
  MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
17
 
18
  # ==========================================
19
+ # AGENT SETUP
20
  # ==========================================
21
  li_llm = HuggingFaceInferenceAPI(model_name=MODEL_ID, token=HF_TOKEN, provider="together")
22
 
 
 
 
 
 
23
  def get_weather(location: str) -> str:
24
+ """Get the current weather. Args: location: city name."""
25
+ return f"The weather in {location} is currently sunny and 22°C."
 
 
 
 
 
 
 
 
 
 
26
 
27
+ li_agent = AgentWorkflow.from_tools_or_functions(
28
+ [FunctionTool.from_defaults(fn=get_weather)],
29
+ llm=li_llm
30
+ )
31
 
32
+ smol_model = InferenceClientModel(model_id=MODEL_ID, token=HF_TOKEN, provider="together")
33
+ smol_agent = CodeAgent(model=smol_model, tools=[DuckDuckGoSearchTool()])
 
 
 
 
34
 
35
  # ==========================================
36
+ # UI LOGIC
37
  # ==========================================
38
+ async def chat_llama(message, history):
39
+ result = await li_agent.run(user_msg=message)
40
+ return str(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def chat_smol(message, history):
43
+ # Yield a 'Thinking' status using ChatMessage metadata
44
+ yield [
45
+ ChatMessage(role="user", content=message),
46
+ ChatMessage(
47
+ role="assistant",
48
+ content="Agent is searching and executing Python code...",
49
+ metadata={"title": "🔍 Step 1: Thinking", "status": "pending"}
50
+ )
51
+ ]
52
  try:
53
  response = smol_agent.run(message)
54
+ yield [
55
+ ChatMessage(role="user", content=message),
56
+ ChatMessage(role="assistant", content=str(response))
57
+ ]
58
  except Exception as e:
59
+ yield [ChatMessage(role="assistant", content=f"Error: {str(e)}")]
60
 
61
  # ==========================================
62
+ # GRADIO 6.0 ENHANCED UI
63
  # ==========================================
64
+ with gr.Blocks(fill_height=True) as demo:
65
+ # Sidebar replaces global config area
66
+ with gr.Sidebar():
67
+ gr.Markdown("# 🛠️ Workspace Settings")
68
+ gr.Markdown("Adjust your agent behaviors and view tool status below.")
69
+
70
+ gr.HTML("<hr>") # This replaces gr.Divider()
71
+
72
+ gr.Markdown("### 🤖 Active Frameworks")
73
+ gr.CheckboxGroup(["LlamaIndex", "smolagents"], label="Enabled Agents", value=["LlamaIndex", "smolagents"])
74
+
75
+ gr.HTML("<hr>")
76
+
77
+ gr.Info("LlamaIndex is optimized for business logic. smolagents is built for raw tool execution.")
78
+
79
+ # Main Chat View
80
+ with gr.Column():
81
+ gr.Markdown(f"# 🤖 Multi-Agent Hub\nPowered by `{MODEL_ID}`")
82
 
83
+ with gr.Tabs():
84
+ with gr.Tab("🏗️ LlamaIndex (Workflow)"):
85
+ gr.ChatInterface(
86
+ fn=chat_llama,
87
+ type="messages",
88
+ examples=["How is the weather in Tokyo?"]
89
+ )
90
+
91
+ with gr.Tab("💻 smolagents (CodeAgent)"):
92
+ # Custom implementation for thought metadata
93
+ chatbot = gr.Chatbot(
94
+ label="Code Agent",
95
+ type="messages",
96
+ show_label=False,
97
+ avatar_images=(None, "https://raw.githubusercontent.com/huggingface/smolagents/main/docs/source/imgs/smolagents_logo.png")
98
+ )
99
+ msg = gr.Textbox(placeholder="Ask me to search or run code...", container=False)
100
+
101
+ msg.submit(chat_smol, [msg, chatbot], [chatbot])
102
+ msg.submit(lambda: "", None, [msg])
103
 
104
  if __name__ == "__main__":
105
+ # Custom CSS for the Title
106
  demo.launch(
107
+ theme=gr.themes.Soft(),
108
+ css=".gradio-container { max-width: 1200px !important; }"
109
  )