File size: 3,776 Bytes
3ee26eb
a07aeef
8d088e9
 
3ee26eb
8d088e9
3ee26eb
 
 
8d088e9
 
 
ad9bb76
8d088e9
 
 
 
 
 
 
 
 
3ee26eb
 
8d088e9
3ee26eb
 
 
a07aeef
8d088e9
a07aeef
 
 
8d088e9
 
 
 
 
 
 
a07aeef
 
 
8d088e9
 
 
a07aeef
 
 
 
 
 
 
 
 
f6335a8
1bb2e42
a07aeef
 
 
 
 
 
 
 
 
 
 
 
 
bd35e0b
3a4977e
a07aeef
 
2b751c4
 
 
 
 
 
 
 
 
bd35e0b
 
2b751c4
 
bd35e0b
2b751c4
3a4977e
 
 
 
 
a07aeef
3ee26eb
 
8d088e9
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
import gradio as gr
import os
import pandas as pd
import datasets

from smolagents import CodeAgent, OpenAIServerModel
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool, NewsSearchTool
from retriever import load_guest_dataset

# Constants
SAMPLE_FILE = "sample_guests.csv"

# Generate sample dataset if not already present
def generate_sample_guest_csv():
    if not os.path.exists(SAMPLE_FILE):
        guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
        df = pd.DataFrame(guest_dataset)
        df.to_csv(SAMPLE_FILE, index=False)

generate_sample_guest_csv()

# Set up model
model = OpenAIServerModel(model_id="gpt-4o")

# Initialize tools
search_tool = DuckDuckGoSearchTool()
weather_info_tool = WeatherInfoTool()
hub_stats_tool = HubStatsTool()
news_tool = NewsSearchTool(api_key=os.getenv("CONTEXTUALWEB_API_KEY"))

# Dynamically create agent with selected guest file
def build_agent(file_path=None):
    guest_info_tool = load_guest_dataset(file_path=file_path)
    return CodeAgent(
        tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool, news_tool],
        model=model,
        add_base_tools=True,
        planning_interval=3
    )

# Agent instance placeholder
agent_instance = None

# Gradio UI
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("## Agent interface")
            gr.Markdown("This web UI allows you to interact with a `smolagents` agent that can use tools and execute steps to complete tasks.")
            gr.File(value=SAMPLE_FILE, label="πŸ“₯ Download sample_guests.csv", interactive=False)
            guest_file = gr.File(label="πŸ“€ Upload guest CSV/JSON", type="filepath", file_types=[".csv", ".json"])
            prompt = gr.Textbox(label="Your request")
            submit = gr.Button("Submit")

            example_prompts = [
                ["List guests"],
                ["Give some examples of conversation starters based on each guest's interests?"],
                ["What's the weather like in Amsterdam tonight? Will it be suitable for our fireworks display?"],
                ["One of our guests is from Qwen. What can you tell me about their most popular model?"],
                ["I need to speak with Dr. Nikola Tesla about recent advancements in wireless energy. Can you help me prepare for this conversation?"],
            ]

            gr.Examples(
                examples=example_prompts,
                inputs=[prompt],
                label="πŸ’‘ Example Prompts sample_guests.csv",
            )
            gr.Markdown("Powered by **smolagents**")

        with gr.Column(scale=3):
            output = gr.Chatbot(label="Agent", type="messages")

    def run_query(prompt, file):
        global agent_instance
        agent_instance = build_agent(file_path=file)
        result = agent_instance.run(prompt)

        # Handle different result types to convert to string for chatbot output
        if isinstance(result, dict):
            result = "\n\n".join(f"**{k}**: {v}" for k, v in result.items())
        elif isinstance(result, list):
            if all(isinstance(item, dict) and "name" in item and "starter" in item for item in result):
                result = "\n\n".join(f"{item['name']}: {item['starter']}" for item in result)
            else:
                result = str(result)
        else:
            result = str(result)

        # Return as list of message dicts for Gradio chatbot type="messages"
        return [
            {"role": "user", "content": prompt},
            {"role": "assistant", "content": result}
        ]

    submit.click(fn=run_query, inputs=[prompt, guest_file], outputs=output)

if __name__ == "__main__":
    demo.launch()