Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,301 +12,113 @@ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
|
| 12 |
# Framework 2: smolagents
|
| 13 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
|
| 14 |
|
| 15 |
-
# 0.
|
| 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(
|
| 23 |
-
model_name=MODEL_ID,
|
| 24 |
-
token=HF_TOKEN,
|
| 25 |
-
provider="together"
|
| 26 |
-
)
|
| 27 |
|
| 28 |
def get_tokyo_time() -> str:
|
| 29 |
"""Returns the current time in Tokyo, Japan."""
|
| 30 |
tz = pytz.timezone('Asia/Tokyo')
|
| 31 |
return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
|
| 32 |
|
| 33 |
-
li_tools = [FunctionTool.from_defaults(fn=get_tokyo_time)]
|
| 34 |
-
|
| 35 |
li_agent = AgentWorkflow.from_tools_or_functions(
|
| 36 |
-
|
| 37 |
llm=li_llm,
|
| 38 |
)
|
| 39 |
|
| 40 |
async def chat_llama(message, history):
|
| 41 |
try:
|
|
|
|
| 42 |
result = await li_agent.run(user_msg=message)
|
| 43 |
return str(result)
|
| 44 |
except Exception as e:
|
| 45 |
-
return f"
|
| 46 |
|
| 47 |
# ==========================================
|
| 48 |
# PART 2: SMOLAGENTS
|
| 49 |
# ==========================================
|
| 50 |
-
smol_model = InferenceClientModel(
|
| 51 |
-
model_id=MODEL_ID,
|
| 52 |
-
token=HF_TOKEN,
|
| 53 |
-
provider="together"
|
| 54 |
-
)
|
| 55 |
|
| 56 |
@tool
|
| 57 |
def weather_tool(location: str) -> str:
|
| 58 |
-
"""Get the current weather for a location.
|
| 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):
|
| 70 |
try:
|
|
|
|
| 71 |
response = smol_agent.run(message)
|
| 72 |
return str(response)
|
| 73 |
except Exception as e:
|
| 74 |
-
return f"
|
| 75 |
|
| 76 |
# ==========================================
|
| 77 |
-
# PART 3:
|
| 78 |
# ==========================================
|
| 79 |
-
|
| 80 |
-
# Custom CSS for better styling
|
| 81 |
-
custom_css = """
|
| 82 |
-
.header-container {
|
| 83 |
-
text-align: center;
|
| 84 |
-
padding: 20px;
|
| 85 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 86 |
-
border-radius: 10px;
|
| 87 |
-
margin-bottom: 20px;
|
| 88 |
-
color: white;
|
| 89 |
-
}
|
| 90 |
-
.info-box {
|
| 91 |
-
background-color: #f0f4ff;
|
| 92 |
-
padding: 15px;
|
| 93 |
-
border-radius: 8px;
|
| 94 |
-
border-left: 4px solid #667eea;
|
| 95 |
-
margin-bottom: 15px;
|
| 96 |
-
}
|
| 97 |
-
.feature-card {
|
| 98 |
-
background: white;
|
| 99 |
-
padding: 20px;
|
| 100 |
-
border-radius: 10px;
|
| 101 |
-
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 102 |
-
margin-bottom: 15px;
|
| 103 |
-
}
|
| 104 |
-
.tool-badge {
|
| 105 |
-
display: inline-block;
|
| 106 |
-
background: #e0e7ff;
|
| 107 |
-
color: #4338ca;
|
| 108 |
-
padding: 8px 15px;
|
| 109 |
-
border-radius: 20px;
|
| 110 |
-
margin: 5px;
|
| 111 |
-
font-size: 0.9em;
|
| 112 |
-
font-weight: 500;
|
| 113 |
-
}
|
| 114 |
-
"""
|
| 115 |
-
|
| 116 |
-
with gr.Blocks(title="AI Agent Comparison") as demo:
|
| 117 |
|
| 118 |
-
|
| 119 |
-
with gr.Row(elem_classes="header-container"):
|
| 120 |
-
gr.Markdown("""
|
| 121 |
-
# 🤖 AI Agent Framework Comparison
|
| 122 |
-
### Compare LlamaIndex and smolagents side-by-side
|
| 123 |
-
""")
|
| 124 |
-
|
| 125 |
-
# Model Info Bar
|
| 126 |
-
with gr.Row():
|
| 127 |
gr.Markdown(f"""
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
</div>
|
| 131 |
""")
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
# Tab 1: LlamaIndex
|
| 137 |
-
with gr.Tab("🦙 LlamaIndex Workflow"):
|
| 138 |
with gr.Row():
|
| 139 |
-
with gr.Column(scale=
|
| 140 |
-
gr.Markdown("""
|
| 141 |
-
<div class="feature-card">
|
| 142 |
-
|
| 143 |
-
### About LlamaIndex
|
| 144 |
-
**Framework Type:** Workflow-based Agent
|
| 145 |
-
**Strengths:** Structured reasoning, enterprise-grade
|
| 146 |
-
|
| 147 |
-
**Available Tools:**
|
| 148 |
-
|
| 149 |
-
<span class="tool-badge">🕐 Tokyo Time</span>
|
| 150 |
-
|
| 151 |
-
**Best For:**
|
| 152 |
-
- Time-based queries
|
| 153 |
-
- Structured data retrieval
|
| 154 |
-
- Enterprise applications
|
| 155 |
-
|
| 156 |
-
</div>
|
| 157 |
-
""")
|
| 158 |
-
|
| 159 |
-
gr.Markdown("""
|
| 160 |
-
### 💡 Try These Examples
|
| 161 |
-
Click to copy and paste:
|
| 162 |
-
|
| 163 |
-
```
|
| 164 |
-
What time is it in Tokyo?
|
| 165 |
-
```
|
| 166 |
-
|
| 167 |
-
```
|
| 168 |
-
Can you tell me the current Tokyo time?
|
| 169 |
-
```
|
| 170 |
-
|
| 171 |
-
```
|
| 172 |
-
What's the time right now in Japan?
|
| 173 |
-
```
|
| 174 |
-
""")
|
| 175 |
-
|
| 176 |
-
with gr.Column(scale=2):
|
| 177 |
gr.ChatInterface(
|
| 178 |
fn=chat_llama,
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
retry_btn="🔄 Retry",
|
| 182 |
-
undo_btn="↩️ Undo",
|
| 183 |
-
clear_btn="🗑️ Clear",
|
| 184 |
)
|
| 185 |
-
|
| 186 |
-
# Tab 2: smolagents
|
| 187 |
-
with gr.Tab("🔧 smolagents CodeAgent"):
|
| 188 |
-
with gr.Row():
|
| 189 |
with gr.Column(scale=1):
|
| 190 |
-
gr.Markdown(""
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
<span class="tool-badge">🌤️ Weather</span>
|
| 200 |
-
<span class="tool-badge">🔍 Web Search</span>
|
| 201 |
-
|
| 202 |
-
**Best For:**
|
| 203 |
-
- General queries
|
| 204 |
-
- Web research
|
| 205 |
-
- Weather information
|
| 206 |
-
|
| 207 |
-
</div>
|
| 208 |
-
""")
|
| 209 |
-
|
| 210 |
-
gr.Markdown("""
|
| 211 |
-
### 💡 Try These Examples
|
| 212 |
-
Click to copy and paste:
|
| 213 |
-
|
| 214 |
-
```
|
| 215 |
-
What's the weather in Paris?
|
| 216 |
-
```
|
| 217 |
-
|
| 218 |
-
```
|
| 219 |
-
Search for latest AI news
|
| 220 |
-
```
|
| 221 |
-
|
| 222 |
-
```
|
| 223 |
-
Tell me the weather in London
|
| 224 |
-
```
|
| 225 |
-
|
| 226 |
-
```
|
| 227 |
-
Find information about Claude AI
|
| 228 |
-
```
|
| 229 |
-
""")
|
| 230 |
-
|
| 231 |
-
with gr.Column(scale=2):
|
| 232 |
gr.ChatInterface(
|
| 233 |
fn=chat_smol,
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
retry_btn="🔄 Retry",
|
| 237 |
-
undo_btn="↩️ Undo",
|
| 238 |
-
clear_btn="🗑️ Clear",
|
| 239 |
)
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
| **Best Use Case** | Enterprise, Structured | General, Dynamic |
|
| 254 |
-
|
| 255 |
-
---
|
| 256 |
-
|
| 257 |
-
## How to Use This Space
|
| 258 |
-
|
| 259 |
-
1. **Choose a Framework Tab** - Pick either LlamaIndex or smolagents
|
| 260 |
-
2. **Copy the Examples** - Use the example queries shown in each tab
|
| 261 |
-
3. **Ask Your Questions** - Type your own questions in the chat
|
| 262 |
-
4. **Compare Results** - Try the same question in both frameworks
|
| 263 |
-
|
| 264 |
-
---
|
| 265 |
-
|
| 266 |
-
## Tips for Best Results
|
| 267 |
-
|
| 268 |
-
- ✅ **Be specific** - "What time is it in Tokyo?" works better than "time?"
|
| 269 |
-
- ✅ **Use natural language** - Ask questions as you would to a person
|
| 270 |
-
- ✅ **Start simple** - Try the examples first to understand capabilities
|
| 271 |
-
- ❌ **Avoid complex multi-step queries** - Keep questions focused
|
| 272 |
-
|
| 273 |
-
---
|
| 274 |
-
|
| 275 |
-
## Technical Details
|
| 276 |
-
|
| 277 |
-
**Model:** `Qwen/Qwen2.5-7B-Instruct`
|
| 278 |
-
**Provider:** Together AI Inference API
|
| 279 |
-
**Frameworks:** LlamaIndex v0.12+ & smolagents v1.0+
|
| 280 |
-
**Deployment:** Hugging Face Spaces (Gradio 6.0)
|
| 281 |
-
|
| 282 |
-
---
|
| 283 |
-
|
| 284 |
-
## Framework Strengths
|
| 285 |
-
|
| 286 |
-
### LlamaIndex 🦙
|
| 287 |
-
- Excellent for enterprise applications
|
| 288 |
-
- Built-in support for RAG (Retrieval Augmented Generation)
|
| 289 |
-
- Strong workflow orchestration
|
| 290 |
-
- Great for structured data operations
|
| 291 |
-
|
| 292 |
-
### smolagents 🔧
|
| 293 |
-
- Lightweight and fast
|
| 294 |
-
- Dynamic code execution
|
| 295 |
-
- Great for web search and general queries
|
| 296 |
-
- Simple to set up and use
|
| 297 |
-
""")
|
| 298 |
-
|
| 299 |
-
# Footer
|
| 300 |
-
gr.Markdown("""
|
| 301 |
-
---
|
| 302 |
-
<div style="text-align: center; color: #666; padding: 20px;">
|
| 303 |
-
<p>Built with ❤️ using Gradio 6.0 | Powered by Together AI | Framework Comparison Demo</p>
|
| 304 |
-
<p style="font-size: 0.9em;">⚠️ This is a demonstration space. Responses may vary based on API availability.</p>
|
| 305 |
-
</div>
|
| 306 |
-
""")
|
| 307 |
|
| 308 |
if __name__ == "__main__":
|
| 309 |
-
demo.launch(
|
| 310 |
-
theme=gr.themes.Soft(),
|
| 311 |
-
css=custom_css
|
| 312 |
-
)
|
|
|
|
| 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 |
# ==========================================
|
| 26 |
# PART 1: LLAMAINDEX AGENT
|
| 27 |
# ==========================================
|
| 28 |
+
li_llm = HuggingFaceInferenceAPI(model_name=MODEL_ID, token=HF_TOKEN, provider="together")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def get_tokyo_time() -> str:
|
| 31 |
"""Returns the current time in Tokyo, Japan."""
|
| 32 |
tz = pytz.timezone('Asia/Tokyo')
|
| 33 |
return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
|
| 34 |
|
|
|
|
|
|
|
| 35 |
li_agent = AgentWorkflow.from_tools_or_functions(
|
| 36 |
+
[FunctionTool.from_defaults(fn=get_tokyo_time)],
|
| 37 |
llm=li_llm,
|
| 38 |
)
|
| 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:
|
| 46 |
+
return f"❌ LlamaIndex Error: {str(e)}"
|
| 47 |
|
| 48 |
# ==========================================
|
| 49 |
# PART 2: SMOLAGENTS
|
| 50 |
# ==========================================
|
| 51 |
+
smol_model = InferenceClientModel(model_id=MODEL_ID, token=HF_TOKEN, provider="together")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
@tool
|
| 54 |
def weather_tool(location: str) -> str:
|
| 55 |
+
"""Get the current weather for a location."""
|
|
|
|
|
|
|
|
|
|
| 56 |
return f"The weather in {location} is currently sunny and 22°C."
|
| 57 |
|
| 58 |
+
# smolagents can capture logs in a buffer for the UI
|
| 59 |
smol_agent = CodeAgent(
|
| 60 |
model=smol_model,
|
| 61 |
+
tools=[weather_tool, DuckDuckGoSearchTool()],
|
| 62 |
+
add_base_tools=True # Adds useful defaults like line_plot
|
| 63 |
)
|
| 64 |
|
| 65 |
def chat_smol(message, history):
|
| 66 |
try:
|
| 67 |
+
# We wrap the run to return a clean string
|
| 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: IMPROVED GRADIO UI
|
| 75 |
# ==========================================
|
| 76 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
with gr.Row(elem_classes="agent-header"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
gr.Markdown(f"""
|
| 80 |
+
# 🤖 Multi-Framework Agent Playground
|
| 81 |
+
**Model:** `{MODEL_ID}` | **Provider:** `Together AI`
|
|
|
|
| 82 |
""")
|
| 83 |
+
|
| 84 |
+
with gr.Tabs():
|
| 85 |
+
# TAB 1: LLAMAINDEX
|
| 86 |
+
with gr.Tab("🏗️ LlamaIndex Workflow"):
|
|
|
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
+
with gr.Column(scale=4):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
gr.ChatInterface(
|
| 90 |
fn=chat_llama,
|
| 91 |
+
examples=["What time is it in Tokyo?", "Explain how workflows work."],
|
| 92 |
+
type="messages"
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
+
gr.Markdown("### Agent Specs")
|
| 96 |
+
gr.Markdown("**Type:** Workflow-based Agent")
|
| 97 |
+
gr.Markdown("**Tools:** `get_tokyo_time`")
|
| 98 |
+
gr.Info("LlamaIndex Workflows use an event-driven architecture, ideal for complex, multi-step logic.")
|
| 99 |
+
|
| 100 |
+
# TAB 2: SMOLAGENTS
|
| 101 |
+
with gr.Tab("💻 smolagents CodeAgent"):
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column(scale=4):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
gr.ChatInterface(
|
| 105 |
fn=chat_smol,
|
| 106 |
+
examples=["What is the weather in Paris?", "Search for the latest AI news."],
|
| 107 |
+
type="messages"
|
|
|
|
|
|
|
|
|
|
| 108 |
)
|
| 109 |
+
with gr.Column(scale=1):
|
| 110 |
+
gr.Markdown("### Agent Specs")
|
| 111 |
+
gr.Markdown("**Type:** Code Executor")
|
| 112 |
+
gr.Markdown("**Tools:** `weather_tool`, `DuckDuckGo`")
|
| 113 |
+
gr.Warning("smolagents writes and executes Python code to solve tasks.")
|
| 114 |
+
|
| 115 |
+
with gr.Accordion("⚙️ System Configuration", open=False):
|
| 116 |
+
gr.JSON(value={
|
| 117 |
+
"model": MODEL_ID,
|
| 118 |
+
"status": "Connected",
|
| 119 |
+
"timezone": "UTC",
|
| 120 |
+
"frameworks": ["LlamaIndex 0.11+", "smolagents 1.0+"]
|
| 121 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|
|
|
|
|
|
|
|
|