Spaces:
Runtime error
Runtime error
File size: 3,444 Bytes
88234d0 133f74a 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 88234d0 8902182 133f74a 8902182 133f74a 8902182 88234d0 8902182 88234d0 8902182 88234d0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
from smolagents.models import LiteLLMModel # ✨ ADD THIS
import gradio as gr
import datetime
import pytz
# ==================== YOUR TOOLS ====================
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Get current time in any timezone.
Args:
timezone: Timezone like 'America/New_York' or 'Asia/Tokyo'
"""
try:
tz = pytz.timezone(timezone)
time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"⏰ Time in {timezone}: {time}"
except:
return "Invalid timezone. Try: America/New_York, Europe/London, Asia/Tokyo"
@tool
def calculate_tip(bill_amount: float, tip_percent: float) -> str:
"""Calculate restaurant tip.
Args:
bill_amount: Bill total in dollars
tip_percent: Tip percentage (e.g., 15 for 15%)
"""
tip = bill_amount * (tip_percent / 100)
total = bill_amount + tip
return f"💰 Tip: ${tip:.2f}\n💵 Total: ${total:.2f}\n📊 {tip_percent}% of ${bill_amount}"
@tool
def word_counter(text: str) -> str:
"""Count words and characters.
Args:
text: Text to analyze
"""
words = len(text.split())
chars = len(text)
return f"📝 Words: {words}\n🔤 Characters: {chars}"
@tool
def simple_calculator(expression: str) -> str:
"""Do basic math calculations.
Args:
expression: Math like '2 + 2' or '10 * 5 / 2'
"""
try:
# Safe calculation
allowed = {'+', '-', '*', '/', '(', ')', '.', ' '}
if all(c.isdigit() or c in allowed for c in expression):
result = eval(expression)
return f"🧮 {expression} = {result}"
return "❌ Only basic math allowed: + - * / ( )"
except:
return "❌ Invalid math expression"
# ==================== CREATE MODEL ====================
# ✨ ADD THIS: Create a model object
model = LiteLLMModel(
model="gpt-3.5-turbo", # Free model that works
api_base="https://api.openai.com/v1", # OpenAI endpoint
temperature=0.5
)
# ==================== CREATE AGENT ====================
agent = CodeAgent(
model=model, # ✨ ADD THIS: Pass the model object
tools=[
DuckDuckGoSearchTool(), # Web search
get_current_time_in_timezone, # Time tool
calculate_tip, # Tip calculator
word_counter, # Word counter
simple_calculator # Math calculator
],
max_steps=8, # Increased for complex tasks
verbosity_level=1 # Show thinking process
)
# ==================== GRADIO INTERFACE ====================
def chat_with_agent(message, history):
"""Handle chat messages"""
response = agent.run(message)
return response
# Create chat interface
demo = gr.ChatInterface(
fn=chat_with_agent,
title="🤖 Super Assistant Pro",
description="I can: 🔍 Search web • ⏰ Tell time • 💰 Calculate tips • 📝 Count words • 🧮 Do math",
examples=[
"What time is it in Tokyo and calculate 15% tip on ¥5000?",
"Count words in 'Hello world' and search for current news",
"Calculate 25 * 4 + 10 and tell time in London",
"What's the weather in Paris? Also calculate 18% tip on €75"
],
theme="soft"
)
# Launch the app
if __name__ == "__main__":
demo.launch() |