dlaima commited on
Commit
8d088e9
·
verified ·
1 Parent(s): 1a790ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -26
app.py CHANGED
@@ -1,41 +1,64 @@
1
  import gradio as gr
2
- import random
3
- #from smolagents import GradioUI, CodeAgent, HfApiModel
4
- from smolagents import GradioUI, CodeAgent, OpenAIServerModel
5
 
6
-
7
- # Import our custom tools from their modules
8
- #from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
9
  from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool, NewsSearchTool
10
  from retriever import load_guest_dataset
11
 
12
- # Initialize the Hugging Face model
13
- #model = HfApiModel()
 
 
 
 
 
 
 
 
 
 
 
14
  model = OpenAIServerModel(model_id="gpt-4o")
15
 
16
- # Initialize the web search tool
17
  search_tool = DuckDuckGoSearchTool()
18
-
19
- # Initialize the weather tool
20
  weather_info_tool = WeatherInfoTool()
21
-
22
- # Initialize the Hub stats tool
23
  hub_stats_tool = HubStatsTool()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Initialize the news tool
26
- news_tool = NewsSearchTool(api_key="CONTEXTUALWEB_API_KEY")
27
 
28
- # Load the guest dataset and initialize the guest info tool
29
- guest_info_tool = load_guest_dataset()
 
30
 
31
- # Create Alfred with all the tools
32
- alfred = CodeAgent(
33
- #tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
34
- tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool, news_tool],
35
- model=model,
36
- add_base_tools=True, # Add any additional base tools
37
- planning_interval=3 # Enable planning every 3 steps
38
- )
39
 
 
40
  if __name__ == "__main__":
41
- GradioUI(alfred).launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import datasets
4
+ import os
5
 
6
+ from smolagents import CodeAgent, OpenAIServerModel
 
 
7
  from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool, NewsSearchTool
8
  from retriever import load_guest_dataset
9
 
10
+ # Constants
11
+ SAMPLE_FILE = "sample_guests.csv"
12
+
13
+ # Generate sample dataset if not 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
+
22
+ # Set up model
23
  model = OpenAIServerModel(model_id="gpt-4o")
24
 
25
+ # Initialize tools
26
  search_tool = DuckDuckGoSearchTool()
 
 
27
  weather_info_tool = WeatherInfoTool()
 
 
28
  hub_stats_tool = HubStatsTool()
29
+ news_tool = NewsSearchTool(api_key=os.getenv("CONTEXTUALWEB_API_KEY")) # replace env var or set directly
30
+
31
+ # Wrapper to load guest info tool dynamically (from uploaded or default file)
32
+ def build_agent_with_guest_file(file=None):
33
+ path = file.name if file else None
34
+ guest_info_tool = load_guest_dataset(file_path=path)
35
+ return CodeAgent(
36
+ tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool, news_tool],
37
+ model=model,
38
+ add_base_tools=True,
39
+ planning_interval=3
40
+ )
41
+
42
+ # Gradio UI
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("## 🧑‍💼 Gala Guest Assistant")
45
+ gr.Markdown("Upload a guest dataset or use the default below.")
46
+
47
+ with gr.Row():
48
+ gr.File(value=SAMPLE_FILE, label="📥 Download sample_guests.csv", interactive=False)
49
+ guest_file = gr.File(label="📤 Upload your custom .csv or .json guest file", file_types=[".csv", ".json"], type="file")
50
+
51
+ query_input = gr.Textbox(label="Ask about a guest (name or relation)")
52
+ output = gr.Textbox(label="Result", lines=10)
53
 
54
+ run_button = gr.Button("Run")
 
55
 
56
+ def query_guest_info(query, file):
57
+ agent = build_agent_with_guest_file(file)
58
+ return agent.run(query)
59
 
60
+ run_button.click(fn=query_guest_info, inputs=[query_input, guest_file], outputs=output)
 
 
 
 
 
 
 
61
 
62
+ # Launch
63
  if __name__ == "__main__":
64
+ demo.launch()