Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,15 +10,11 @@ from llama_index.core.agent import ReActAgent
|
|
| 10 |
from llama_index.core.tools import FunctionTool
|
| 11 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 12 |
|
| 13 |
-
# ============ 1. SMOLAGENTS
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def time_tool(timezone: str = "UTC") -> str:
|
| 17 |
-
"""
|
| 18 |
-
Get current time in a specific timezone.
|
| 19 |
-
Args:
|
| 20 |
-
timezone: The timezone to check (e.g., 'Asia/Tokyo').
|
| 21 |
-
"""
|
| 22 |
try:
|
| 23 |
tz = pytz.timezone(timezone)
|
| 24 |
now = datetime.datetime.now(tz)
|
|
@@ -28,12 +24,7 @@ def time_tool(timezone: str = "UTC") -> str:
|
|
| 28 |
|
| 29 |
@tool
|
| 30 |
def translator_tool(text: str, target_language: str) -> str:
|
| 31 |
-
"""
|
| 32 |
-
Translates text into a specified language.
|
| 33 |
-
Args:
|
| 34 |
-
text: The text or phrase to translate.
|
| 35 |
-
target_language: The destination language (e.g., 'french', 'german').
|
| 36 |
-
"""
|
| 37 |
try:
|
| 38 |
translation = GoogleTranslator(source='auto', target=target_language).translate(text)
|
| 39 |
return f"Translated to {target_language.title()}: {translation}"
|
|
@@ -42,11 +33,7 @@ def translator_tool(text: str, target_language: str) -> str:
|
|
| 42 |
|
| 43 |
@tool
|
| 44 |
def weather_tool(location: str) -> str:
|
| 45 |
-
"""
|
| 46 |
-
Get the current weather for any location worldwide.
|
| 47 |
-
Args:
|
| 48 |
-
location: The name of the city or place (e.g., 'London' or 'Tokyo').
|
| 49 |
-
"""
|
| 50 |
try:
|
| 51 |
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1&language=en&format=json"
|
| 52 |
geo_res = requests.get(geo_url).json()
|
|
@@ -58,9 +45,7 @@ def weather_tool(location: str) -> str:
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"Error: {str(e)}"
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 64 |
smol_model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", token=HF_TOKEN)
|
| 65 |
|
| 66 |
smol_agent = CodeAgent(
|
|
@@ -69,17 +54,34 @@ smol_agent = CodeAgent(
|
|
| 69 |
max_steps=5
|
| 70 |
)
|
| 71 |
|
| 72 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
llama_llm = HuggingFaceInferenceAPI(
|
| 74 |
model_name="Qwen/Qwen2.5-7B-Instruct",
|
| 75 |
token=HF_TOKEN,
|
| 76 |
-
task="
|
| 77 |
)
|
| 78 |
|
| 79 |
llama_agent = ReActAgent.from_tools(
|
| 80 |
[
|
| 81 |
-
FunctionTool.from_defaults(fn=
|
| 82 |
-
FunctionTool.from_defaults(fn=
|
| 83 |
],
|
| 84 |
llm=llama_llm,
|
| 85 |
verbose=True
|
|
@@ -88,17 +90,28 @@ llama_agent = ReActAgent.from_tools(
|
|
| 88 |
# ============ GRADIO UI ============
|
| 89 |
|
| 90 |
def chat_smol(message, history):
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
def chat_llama(message, history):
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 98 |
gr.Markdown("# 🤖 Multi-Framework AI Agent Space")
|
|
|
|
| 99 |
with gr.Tab("Smolagents (Alfred)"):
|
|
|
|
| 100 |
gr.ChatInterface(chat_smol)
|
|
|
|
| 101 |
with gr.Tab("LlamaIndex (ReAct)"):
|
|
|
|
| 102 |
gr.ChatInterface(chat_llama)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
|
|
|
| 10 |
from llama_index.core.tools import FunctionTool
|
| 11 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 12 |
|
| 13 |
+
# ============ 1. SMOLAGENTS SETUP (Already Working) ============
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def time_tool(timezone: str = "UTC") -> str:
|
| 17 |
+
"""Get current time in a specific timezone."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
tz = pytz.timezone(timezone)
|
| 20 |
now = datetime.datetime.now(tz)
|
|
|
|
| 24 |
|
| 25 |
@tool
|
| 26 |
def translator_tool(text: str, target_language: str) -> str:
|
| 27 |
+
"""Translates text into a specified language."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
translation = GoogleTranslator(source='auto', target=target_language).translate(text)
|
| 30 |
return f"Translated to {target_language.title()}: {translation}"
|
|
|
|
| 33 |
|
| 34 |
@tool
|
| 35 |
def weather_tool(location: str) -> str:
|
| 36 |
+
"""Get the current weather for any location worldwide."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1&language=en&format=json"
|
| 39 |
geo_res = requests.get(geo_url).json()
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error: {str(e)}"
|
| 47 |
|
| 48 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
| 49 |
smol_model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", token=HF_TOKEN)
|
| 50 |
|
| 51 |
smol_agent = CodeAgent(
|
|
|
|
| 54 |
max_steps=5
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# ============ 2. LLAMAINDEX SETUP (Fixed) ============
|
| 58 |
+
|
| 59 |
+
# FIX 1: Define real functions with docstrings so the Agent knows what they do
|
| 60 |
+
def get_tokyo_time() -> str:
|
| 61 |
+
"""
|
| 62 |
+
Returns the current time in Tokyo, Japan.
|
| 63 |
+
Use this tool when the user asks for the time in Tokyo.
|
| 64 |
+
"""
|
| 65 |
+
return f"Tokyo: {datetime.datetime.now(pytz.timezone('Asia/Tokyo'))}"
|
| 66 |
+
|
| 67 |
+
def multiply(a: float, b: float) -> float:
|
| 68 |
+
"""
|
| 69 |
+
Multiplies two numbers a and b.
|
| 70 |
+
Use this tool for calculations involving multiplication.
|
| 71 |
+
"""
|
| 72 |
+
return a * b
|
| 73 |
+
|
| 74 |
+
# FIX 2: Change task to 'text-generation'
|
| 75 |
llama_llm = HuggingFaceInferenceAPI(
|
| 76 |
model_name="Qwen/Qwen2.5-7B-Instruct",
|
| 77 |
token=HF_TOKEN,
|
| 78 |
+
task="text-generation"
|
| 79 |
)
|
| 80 |
|
| 81 |
llama_agent = ReActAgent.from_tools(
|
| 82 |
[
|
| 83 |
+
FunctionTool.from_defaults(fn=get_tokyo_time),
|
| 84 |
+
FunctionTool.from_defaults(fn=multiply)
|
| 85 |
],
|
| 86 |
llm=llama_llm,
|
| 87 |
verbose=True
|
|
|
|
| 90 |
# ============ GRADIO UI ============
|
| 91 |
|
| 92 |
def chat_smol(message, history):
|
| 93 |
+
try:
|
| 94 |
+
return str(smol_agent.run(message))
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return f"SmolAgent Error: {str(e)}"
|
| 97 |
|
| 98 |
def chat_llama(message, history):
|
| 99 |
+
try:
|
| 100 |
+
# LlamaIndex keeps its own history, so we just pass the message
|
| 101 |
+
response = llama_agent.chat(message)
|
| 102 |
+
return str(response)
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"LlamaIndex Error: {str(e)}"
|
| 105 |
|
| 106 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 107 |
gr.Markdown("# 🤖 Multi-Framework AI Agent Space")
|
| 108 |
+
|
| 109 |
with gr.Tab("Smolagents (Alfred)"):
|
| 110 |
+
gr.Markdown("Uses `Qwen2.5-Coder-32B`")
|
| 111 |
gr.ChatInterface(chat_smol)
|
| 112 |
+
|
| 113 |
with gr.Tab("LlamaIndex (ReAct)"):
|
| 114 |
+
gr.Markdown("Uses `Qwen2.5-7B`")
|
| 115 |
gr.ChatInterface(chat_llama)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|