lmrkmrcs commited on
Commit
e27ff7f
Β·
verified Β·
1 Parent(s): bba3b19

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # Alfred - The Gala Party Agent with Agentic RAG
3
+ # Unit 3 Use Case Project for Hugging Face Agents Course
4
+
5
+ import os
6
+ import gradio as gr
7
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
8
+
9
+ # Import our custom tools
10
+ from retriever import create_guest_retriever_tool
11
+ from tools import get_current_time, calculate_party_budget, suggest_seating_arrangement, dietary_check
12
+
13
+
14
+ # ============================================
15
+ # SETUP THE MODEL
16
+ # ============================================
17
+
18
+ model = InferenceClientModel(
19
+ model_id="Qwen/Qwen2.5-7B-Instruct", # Free tier compatible model
20
+ token=os.environ.get("HF_TOKEN"),
21
+ )
22
+
23
+
24
+ # ============================================
25
+ # CREATE THE GUEST RETRIEVER TOOL (RAG)
26
+ # ============================================
27
+
28
+ print("Loading guest database...")
29
+ guest_retriever_tool = create_guest_retriever_tool()
30
+ print("Guest database loaded successfully!")
31
+
32
+
33
+ # ============================================
34
+ # CREATE ALFRED - THE GALA AGENT
35
+ # ============================================
36
+
37
+ alfred = CodeAgent(
38
+ model=model,
39
+ tools=[
40
+ guest_retriever_tool, # RAG tool for guest information
41
+ DuckDuckGoSearchTool(), # Web search for general queries
42
+ get_current_time, # Time tool
43
+ calculate_party_budget, # Budget calculator
44
+ suggest_seating_arrangement, # Seating planner
45
+ dietary_check, # Dietary requirements helper
46
+ ],
47
+ max_steps=5,
48
+ verbosity_level=1,
49
+ )
50
+
51
+
52
+ # ============================================
53
+ # CHAT FUNCTION
54
+ # ============================================
55
+
56
+ def chat_with_alfred(message, history):
57
+ """
58
+ Process user message and return Alfred's response.
59
+ """
60
+ if not message.strip():
61
+ return "Good day! I am Alfred, your gala assistant. How may I help you with the party preparations?"
62
+
63
+ try:
64
+ # Add context to help Alfred understand his role
65
+ enhanced_message = f"""You are Alfred, the butler and gala party assistant.
66
+ You have access to a guest database that you can search using the guest_info_retriever tool.
67
+ You can also help with party planning, budgets, seating, and dietary requirements.
68
+
69
+ User's request: {message}
70
+
71
+ If the question is about a guest or guests, use the guest_info_retriever tool to find information.
72
+ Be helpful, polite, and thorough in your responses."""
73
+
74
+ response = alfred.run(enhanced_message)
75
+ return str(response)
76
+
77
+ except Exception as e:
78
+ error_msg = str(e)
79
+ if "404" in error_msg or "not found" in error_msg.lower():
80
+ return "I apologize, but I'm having trouble connecting to my systems. Please ensure the HF_TOKEN is properly configured."
81
+ elif "rate" in error_msg.lower() or "limit" in error_msg.lower():
82
+ return "I've reached my service limits momentarily. Please wait a moment and try again."
83
+ else:
84
+ return f"I encountered an unexpected issue: {error_msg}"
85
+
86
+
87
+ # ============================================
88
+ # GRADIO INTERFACE
89
+ # ============================================
90
+
91
+ with gr.Blocks(title="Alfred - Gala Party Agent") as demo:
92
+
93
+ gr.Markdown(
94
+ """
95
+ # 🎩 Alfred - Your Gala Party Assistant
96
+
97
+ Good evening! I am Alfred, your dedicated assistant for the grand gala.
98
+ I have access to the complete guest list and can help you with all party preparations.
99
+
100
+ ### What I Can Do:
101
+ - πŸ” **Guest Information** - Search and retrieve details about any guest
102
+ - 🌐 **Web Search** - Find information from the internet
103
+ - πŸ• **Current Time** - Tell you the current date and time
104
+ - πŸ’° **Budget Calculator** - Calculate party expenses
105
+ - πŸͺ‘ **Seating Planner** - Suggest seating arrangements
106
+ - πŸ₯— **Dietary Helper** - Get menu suggestions for dietary requirements
107
+
108
+ ---
109
+ """
110
+ )
111
+
112
+ chatbot = gr.ChatInterface(
113
+ fn=chat_with_alfred,
114
+ examples=[
115
+ "Who is Lady Galadriel and what are her interests?",
116
+ "Which guests are vegetarian?",
117
+ "Tell me about guests who like music",
118
+ "Calculate budget for 50 guests at $100 each, $5000 venue, $2000 entertainment",
119
+ "What's the current time?",
120
+ "Suggest seating for: John, Mary, Bob, Alice, Tom, Jane at tables of 3",
121
+ "What are vegan menu options?",
122
+ ],
123
+ )
124
+
125
+ gr.Markdown(
126
+ """
127
+ ---
128
+ *πŸŽ“ Unit 3 Agentic RAG Use Case | Hugging Face Agents Course*
129
+
130
+ This agent demonstrates **Agentic RAG** - combining an AI agent with document retrieval
131
+ to answer questions about specific data (guest information).
132
+ """
133
+ )
134
+
135
+
136
+ # ============================================
137
+ # LAUNCH
138
+ # ============================================
139
+
140
+ if __name__ == "__main__":
141
+ demo.launch()