Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import pandas as pd
|
| 4 |
import datasets
|
| 5 |
-
import logging
|
| 6 |
|
| 7 |
from smolagents import CodeAgent, OpenAIServerModel
|
| 8 |
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool, NewsSearchTool
|
|
@@ -11,16 +10,12 @@ from retriever import load_guest_dataset
|
|
| 11 |
# Constants
|
| 12 |
SAMPLE_FILE = "sample_guests.csv"
|
| 13 |
|
| 14 |
-
# Set up logging
|
| 15 |
-
logging.basicConfig(level=logging.INFO)
|
| 16 |
-
|
| 17 |
# Generate sample dataset if not already present
|
| 18 |
def generate_sample_guest_csv():
|
| 19 |
if not os.path.exists(SAMPLE_FILE):
|
| 20 |
guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
|
| 21 |
df = pd.DataFrame(guest_dataset)
|
| 22 |
df.to_csv(SAMPLE_FILE, index=False)
|
| 23 |
-
logging.info(f"Sample dataset saved as {SAMPLE_FILE}")
|
| 24 |
|
| 25 |
generate_sample_guest_csv()
|
| 26 |
|
|
@@ -76,28 +71,21 @@ with gr.Blocks() as demo:
|
|
| 76 |
|
| 77 |
def run_query(prompt, file):
|
| 78 |
global agent_instance
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
result =
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
result = "\n\n".join(f"**{k}**: {v}" for k, v in result.items())
|
| 89 |
-
elif isinstance(result, list):
|
| 90 |
-
if all(isinstance(item, dict) and "name" in item and "starter" in item for item in result):
|
| 91 |
-
result = "\n\n".join(f"{item['name']}: {item['starter']}" for item in result)
|
| 92 |
-
else:
|
| 93 |
-
result = str(result)
|
| 94 |
else:
|
| 95 |
result = str(result)
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
logging.error(f"Error running agent: {e}")
|
| 99 |
-
result = f"⚠️ An error occurred: {str(e)}"
|
| 100 |
-
|
| 101 |
return [
|
| 102 |
{"role": "user", "content": prompt},
|
| 103 |
{"role": "assistant", "content": result}
|
|
|
|
| 2 |
import os
|
| 3 |
import pandas as pd
|
| 4 |
import datasets
|
|
|
|
| 5 |
|
| 6 |
from smolagents import CodeAgent, OpenAIServerModel
|
| 7 |
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool, NewsSearchTool
|
|
|
|
| 10 |
# Constants
|
| 11 |
SAMPLE_FILE = "sample_guests.csv"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# Generate sample dataset if not already present
|
| 14 |
def generate_sample_guest_csv():
|
| 15 |
if not os.path.exists(SAMPLE_FILE):
|
| 16 |
guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
|
| 17 |
df = pd.DataFrame(guest_dataset)
|
| 18 |
df.to_csv(SAMPLE_FILE, index=False)
|
|
|
|
| 19 |
|
| 20 |
generate_sample_guest_csv()
|
| 21 |
|
|
|
|
| 71 |
|
| 72 |
def run_query(prompt, file):
|
| 73 |
global agent_instance
|
| 74 |
+
agent_instance = build_agent(file_path=file)
|
| 75 |
+
result = agent_instance.run(prompt)
|
| 76 |
+
|
| 77 |
+
# Handle different result types to convert to string for chatbot output
|
| 78 |
+
if isinstance(result, dict):
|
| 79 |
+
result = "\n\n".join(f"**{k}**: {v}" for k, v in result.items())
|
| 80 |
+
elif isinstance(result, list):
|
| 81 |
+
if all(isinstance(item, dict) and "name" in item and "starter" in item for item in result):
|
| 82 |
+
result = "\n\n".join(f"{item['name']}: {item['starter']}" for item in result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
else:
|
| 84 |
result = str(result)
|
| 85 |
+
else:
|
| 86 |
+
result = str(result)
|
| 87 |
|
| 88 |
+
# Return as list of message dicts for Gradio chatbot type="messages"
|
|
|
|
|
|
|
|
|
|
| 89 |
return [
|
| 90 |
{"role": "user", "content": prompt},
|
| 91 |
{"role": "assistant", "content": result}
|