Updated app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
|
| 4 |
# # Occasio - Event Management Assistant
|
| 5 |
|
| 6 |
-
# In[
|
| 7 |
|
| 8 |
|
| 9 |
# imports
|
|
@@ -61,19 +61,36 @@ genai.configure()
|
|
| 61 |
GOOGLE_MODEL = "gemini-2.0-flash"
|
| 62 |
|
| 63 |
|
| 64 |
-
# In[
|
| 65 |
|
| 66 |
|
| 67 |
-
system_message =
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
list
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
# Some imports for handling images
|
|
@@ -83,7 +100,7 @@ from io import BytesIO
|
|
| 83 |
from PIL import Image
|
| 84 |
|
| 85 |
|
| 86 |
-
# In[
|
| 87 |
|
| 88 |
|
| 89 |
def artist(event_text):
|
|
@@ -99,7 +116,7 @@ def artist(event_text):
|
|
| 99 |
return Image.open(BytesIO(image_data))
|
| 100 |
|
| 101 |
|
| 102 |
-
# In[
|
| 103 |
|
| 104 |
|
| 105 |
import base64
|
|
@@ -119,15 +136,75 @@ def talker(message):
|
|
| 119 |
with open(output_filename, "wb") as f:
|
| 120 |
f.write(audio_stream.read())
|
| 121 |
return output_filename
|
| 122 |
-
#Commented below just for Huggingface deployment
|
| 123 |
-
# # Play the generated audio
|
| 124 |
-
# display(Audio(output_filename, autoplay=True))
|
| 125 |
|
| 126 |
|
| 127 |
-
# In[
|
| 128 |
|
| 129 |
|
| 130 |
school_events = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
{
|
| 132 |
"event_id": "pta",
|
| 133 |
"name": "Parent Teachers Meeting (PTA/PTM)",
|
|
@@ -180,7 +257,7 @@ school_events = [
|
|
| 180 |
]
|
| 181 |
|
| 182 |
|
| 183 |
-
# In[
|
| 184 |
|
| 185 |
|
| 186 |
def get_event_details(query):
|
|
@@ -202,7 +279,7 @@ def get_event_details(query):
|
|
| 202 |
#
|
| 203 |
# Well, kinda.
|
| 204 |
|
| 205 |
-
# In[
|
| 206 |
|
| 207 |
|
| 208 |
# for claude
|
|
@@ -225,7 +302,7 @@ tools_claude = [
|
|
| 225 |
]
|
| 226 |
|
| 227 |
|
| 228 |
-
# In[
|
| 229 |
|
| 230 |
|
| 231 |
# For GPT
|
|
@@ -247,14 +324,14 @@ events_function_gpt = {
|
|
| 247 |
}
|
| 248 |
|
| 249 |
|
| 250 |
-
# In[
|
| 251 |
|
| 252 |
|
| 253 |
# And this is included in a list of tools:
|
| 254 |
tools_gpt = [{"type": "function", "function": events_function_gpt}]
|
| 255 |
|
| 256 |
|
| 257 |
-
# In[
|
| 258 |
|
| 259 |
|
| 260 |
#Gemini function declaration structure
|
|
@@ -289,7 +366,7 @@ gemini_event_details = [{
|
|
| 289 |
]
|
| 290 |
|
| 291 |
|
| 292 |
-
# In[
|
| 293 |
|
| 294 |
|
| 295 |
def chat_claude(history):
|
|
@@ -301,7 +378,6 @@ def chat_claude(history):
|
|
| 301 |
message = claude.messages.create(
|
| 302 |
model=ANTHROPIC_MODEL,
|
| 303 |
max_tokens=1000,
|
| 304 |
-
temperature=0.7,
|
| 305 |
system=system_message,
|
| 306 |
messages=history_claude,
|
| 307 |
tools=tools_claude
|
|
@@ -373,7 +449,7 @@ def chat_claude(history):
|
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
-
# In[
|
| 377 |
|
| 378 |
|
| 379 |
def chat_gpt(history):
|
|
@@ -422,7 +498,7 @@ def chat_gpt(history):
|
|
| 422 |
yield error_message, None
|
| 423 |
|
| 424 |
|
| 425 |
-
# In[
|
| 426 |
|
| 427 |
|
| 428 |
def chat_gemini(history):
|
|
@@ -485,7 +561,7 @@ def chat_gemini(history):
|
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
-
# In[
|
| 489 |
|
| 490 |
|
| 491 |
def call_and_process_model_responses(fn_name, chatbot):#, response, image):
|
|
@@ -501,7 +577,7 @@ def call_and_process_model_responses(fn_name, chatbot):#, response, image):
|
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
-
# In[
|
| 505 |
|
| 506 |
|
| 507 |
def handle_tool_call(event_text):
|
|
@@ -517,7 +593,7 @@ def handle_tool_call(event_text):
|
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
-
# In[
|
| 521 |
|
| 522 |
|
| 523 |
def process_chosen_model(chatbot, model):
|
|
@@ -534,7 +610,7 @@ def process_chosen_model(chatbot, model):
|
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
-
# In[
|
| 538 |
|
| 539 |
|
| 540 |
# More involved Gradio code as we're not using the preset Chat interface!
|
|
@@ -549,9 +625,6 @@ with gr.Blocks(css="""
|
|
| 549 |
with gr.Row():
|
| 550 |
gr.HTML("<h1 style='text-align: center; color: #4CAF50;'>Occasio! An Event Management Assistant</h1>") # Added title
|
| 551 |
with gr.Row():
|
| 552 |
-
# with gr.Column(scale=3): #Acts as a spacer on the left
|
| 553 |
-
# pass
|
| 554 |
-
|
| 555 |
with gr.Column(scale=0):
|
| 556 |
model = gr.Dropdown(
|
| 557 |
choices=["GPT", "Claude", "Gemini"],
|
|
@@ -560,9 +633,6 @@ with gr.Blocks(css="""
|
|
| 560 |
interactive=True,
|
| 561 |
container=True # Applying the CSS class
|
| 562 |
)
|
| 563 |
-
# with gr.Column(scale=-54, min_width=200):
|
| 564 |
-
# gr.HTML("<h1 style='text-align: center; color: #4CAF50;'>Occasio</h1>") # Added title
|
| 565 |
-
# pass #Acts as a spacer on the right
|
| 566 |
with gr.Row():
|
| 567 |
chatbot = gr.Chatbot(height=500, type="messages")
|
| 568 |
image_output = gr.Image(height=500)
|
|
@@ -576,16 +646,17 @@ with gr.Blocks(css="""
|
|
| 576 |
history += [{"role":"user", "content":message}]
|
| 577 |
return "", history
|
| 578 |
|
|
|
|
| 579 |
entry.submit(do_entry, inputs=[entry, chatbot], outputs=[entry, chatbot]).then(
|
| 580 |
process_chosen_model, inputs=[chatbot, model], outputs=[chatbot, image_output]
|
| 581 |
).then(#added this specifically for Hugging face spaces deployment
|
| 582 |
-
lambda chat: talker(chat[-1]["content"]), inputs=[chatbot], outputs=gr.Audio()
|
| 583 |
)
|
| 584 |
|
| 585 |
clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)
|
| 586 |
|
| 587 |
|
| 588 |
-
# In[
|
| 589 |
|
| 590 |
|
| 591 |
ui.launch(inbrowser=True)
|
|
|
|
| 3 |
|
| 4 |
# # Occasio - Event Management Assistant
|
| 5 |
|
| 6 |
+
# In[3]:
|
| 7 |
|
| 8 |
|
| 9 |
# imports
|
|
|
|
| 61 |
GOOGLE_MODEL = "gemini-2.0-flash"
|
| 62 |
|
| 63 |
|
| 64 |
+
# In[173]:
|
| 65 |
|
| 66 |
|
| 67 |
+
system_message = (
|
| 68 |
+
'You are called "EventAI," a virtual assistant for an Elementary school called Eagle Elementary School. '
|
| 69 |
+
"You will help users by providing details of upcoming school events such as event name, description, and location. "
|
| 70 |
+
"You MUST always call the appropriate tool function to fetch event details and NEVER generate information yourself. "
|
| 71 |
+
"If you do not have the required details, simply state that you do not have that information. "
|
| 72 |
+
"Responses should be short and courteous, no more than 2 sentences. "
|
| 73 |
+
"When asked to list user questions, summarize based on conversation history."
|
| 74 |
|
| 75 |
+
"\n\nExample:\n"
|
| 76 |
+
"User: What is read aloud?\n"
|
| 77 |
+
"Assistant: [calls tool function to fetch read aloud event]\n"
|
| 78 |
+
)
|
| 79 |
|
| 80 |
+
|
| 81 |
+
# In[147]:
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# system_message = "You are called \"EventAI\", a virtual assistant for an Elementary school called Eagle Elementary School. You will help users by giving \
|
| 85 |
+
# them details of upcoming school events like event name, description, location etc. by calling a tools function"
|
| 86 |
+
# #system_message += "Introduce yourself with a warm welcome message on your first response ONLY."
|
| 87 |
+
# system_message += "Give short, courteous answers, no more than 2 sentences. "
|
| 88 |
+
# system_message += "Do not make up your own event details information"
|
| 89 |
+
# system_message += "You might be asked to list the questions asked by the user so far. In that situation, based on the conversation history provided to you, \
|
| 90 |
+
# list the questions and respond"
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# In[148]:
|
| 94 |
|
| 95 |
|
| 96 |
# Some imports for handling images
|
|
|
|
| 100 |
from PIL import Image
|
| 101 |
|
| 102 |
|
| 103 |
+
# In[149]:
|
| 104 |
|
| 105 |
|
| 106 |
def artist(event_text):
|
|
|
|
| 116 |
return Image.open(BytesIO(image_data))
|
| 117 |
|
| 118 |
|
| 119 |
+
# In[150]:
|
| 120 |
|
| 121 |
|
| 122 |
import base64
|
|
|
|
| 136 |
with open(output_filename, "wb") as f:
|
| 137 |
f.write(audio_stream.read())
|
| 138 |
return output_filename
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
|
| 141 |
+
# In[151]:
|
| 142 |
|
| 143 |
|
| 144 |
school_events = [
|
| 145 |
+
{
|
| 146 |
+
"event_id": "science_fair",
|
| 147 |
+
"name": "Annual Science Fair",
|
| 148 |
+
"description": "Students showcase their science projects and experiments.",
|
| 149 |
+
"date_time": "May 25th, 2025 10 AM",
|
| 150 |
+
"location": "School Auditorium"
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"event_id": "sports_day",
|
| 154 |
+
"name": "Sports Day",
|
| 155 |
+
"description": "A day full of fun and competitive sports activities for students.",
|
| 156 |
+
"date_time": "Jun 10th, 2025 9 AM",
|
| 157 |
+
"location": "School Playground"
|
| 158 |
+
},
|
| 159 |
+
{
|
| 160 |
+
"event_id": "art_exhibition",
|
| 161 |
+
"name": "Art Exhibition",
|
| 162 |
+
"description": "Students display their creative artwork for parents and teachers to admire.",
|
| 163 |
+
"date_time": "Jun 20th, 2025 3 PM",
|
| 164 |
+
"location": "School Art Hall"
|
| 165 |
+
},
|
| 166 |
+
{
|
| 167 |
+
"event_id": "music_festival",
|
| 168 |
+
"name": "Annual Music Festival",
|
| 169 |
+
"description": "A musical event where students perform solo and group performances.",
|
| 170 |
+
"date_time": "Jul 5th, 2025 6 PM",
|
| 171 |
+
"location": "School Auditorium"
|
| 172 |
+
},
|
| 173 |
+
{
|
| 174 |
+
"event_id": "career_day",
|
| 175 |
+
"name": "Career Day",
|
| 176 |
+
"description": "Professionals from various fields share their experiences with students.",
|
| 177 |
+
"date_time": "Aug 12th, 2025 10 AM",
|
| 178 |
+
"location": "Conference Hall"
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"event_id": "math_olympiad",
|
| 182 |
+
"name": "Math Olympiad",
|
| 183 |
+
"description": "A competitive math event to challenge students' problem-solving skills.",
|
| 184 |
+
"date_time": "Sep 1st, 2025 2 PM",
|
| 185 |
+
"location": "Math Lab"
|
| 186 |
+
},
|
| 187 |
+
{
|
| 188 |
+
"event_id": "book_fair",
|
| 189 |
+
"name": "Book Fair",
|
| 190 |
+
"description": "A school-wide book fair to encourage reading and literacy.",
|
| 191 |
+
"date_time": "Sep 20th, 2025 11 AM",
|
| 192 |
+
"location": "Library"
|
| 193 |
+
},
|
| 194 |
+
{
|
| 195 |
+
"event_id": "halloween_parade",
|
| 196 |
+
"name": "Halloween Parade",
|
| 197 |
+
"description": "Students dress up in costumes and parade around the school.",
|
| 198 |
+
"date_time": "Oct 31st, 2025 1 PM",
|
| 199 |
+
"location": "School Courtyard"
|
| 200 |
+
},
|
| 201 |
+
{
|
| 202 |
+
"event_id": "winter_concert",
|
| 203 |
+
"name": "Winter Concert",
|
| 204 |
+
"description": "A special musical performance to celebrate the winter season.",
|
| 205 |
+
"date_time": "Dec 15th, 2025 5 PM",
|
| 206 |
+
"location": "School Auditorium"
|
| 207 |
+
},
|
| 208 |
{
|
| 209 |
"event_id": "pta",
|
| 210 |
"name": "Parent Teachers Meeting (PTA/PTM)",
|
|
|
|
| 257 |
]
|
| 258 |
|
| 259 |
|
| 260 |
+
# In[152]:
|
| 261 |
|
| 262 |
|
| 263 |
def get_event_details(query):
|
|
|
|
| 279 |
#
|
| 280 |
# Well, kinda.
|
| 281 |
|
| 282 |
+
# In[153]:
|
| 283 |
|
| 284 |
|
| 285 |
# for claude
|
|
|
|
| 302 |
]
|
| 303 |
|
| 304 |
|
| 305 |
+
# In[154]:
|
| 306 |
|
| 307 |
|
| 308 |
# For GPT
|
|
|
|
| 324 |
}
|
| 325 |
|
| 326 |
|
| 327 |
+
# In[155]:
|
| 328 |
|
| 329 |
|
| 330 |
# And this is included in a list of tools:
|
| 331 |
tools_gpt = [{"type": "function", "function": events_function_gpt}]
|
| 332 |
|
| 333 |
|
| 334 |
+
# In[156]:
|
| 335 |
|
| 336 |
|
| 337 |
#Gemini function declaration structure
|
|
|
|
| 366 |
]
|
| 367 |
|
| 368 |
|
| 369 |
+
# In[178]:
|
| 370 |
|
| 371 |
|
| 372 |
def chat_claude(history):
|
|
|
|
| 378 |
message = claude.messages.create(
|
| 379 |
model=ANTHROPIC_MODEL,
|
| 380 |
max_tokens=1000,
|
|
|
|
| 381 |
system=system_message,
|
| 382 |
messages=history_claude,
|
| 383 |
tools=tools_claude
|
|
|
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
+
# In[177]:
|
| 453 |
|
| 454 |
|
| 455 |
def chat_gpt(history):
|
|
|
|
| 498 |
yield error_message, None
|
| 499 |
|
| 500 |
|
| 501 |
+
# In[176]:
|
| 502 |
|
| 503 |
|
| 504 |
def chat_gemini(history):
|
|
|
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
+
# In[168]:
|
| 565 |
|
| 566 |
|
| 567 |
def call_and_process_model_responses(fn_name, chatbot):#, response, image):
|
|
|
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
+
# In[169]:
|
| 581 |
|
| 582 |
|
| 583 |
def handle_tool_call(event_text):
|
|
|
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
+
# In[170]:
|
| 597 |
|
| 598 |
|
| 599 |
def process_chosen_model(chatbot, model):
|
|
|
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
+
# In[174]:
|
| 614 |
|
| 615 |
|
| 616 |
# More involved Gradio code as we're not using the preset Chat interface!
|
|
|
|
| 625 |
with gr.Row():
|
| 626 |
gr.HTML("<h1 style='text-align: center; color: #4CAF50;'>Occasio! An Event Management Assistant</h1>") # Added title
|
| 627 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 628 |
with gr.Column(scale=0):
|
| 629 |
model = gr.Dropdown(
|
| 630 |
choices=["GPT", "Claude", "Gemini"],
|
|
|
|
| 633 |
interactive=True,
|
| 634 |
container=True # Applying the CSS class
|
| 635 |
)
|
|
|
|
|
|
|
|
|
|
| 636 |
with gr.Row():
|
| 637 |
chatbot = gr.Chatbot(height=500, type="messages")
|
| 638 |
image_output = gr.Image(height=500)
|
|
|
|
| 646 |
history += [{"role":"user", "content":message}]
|
| 647 |
return "", history
|
| 648 |
|
| 649 |
+
|
| 650 |
entry.submit(do_entry, inputs=[entry, chatbot], outputs=[entry, chatbot]).then(
|
| 651 |
process_chosen_model, inputs=[chatbot, model], outputs=[chatbot, image_output]
|
| 652 |
).then(#added this specifically for Hugging face spaces deployment
|
| 653 |
+
lambda chat: talker(chat[-1]["content"]), inputs=[chatbot], outputs=gr.Audio(autoplay=True, visible=False)
|
| 654 |
)
|
| 655 |
|
| 656 |
clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)
|
| 657 |
|
| 658 |
|
| 659 |
+
# In[175]:
|
| 660 |
|
| 661 |
|
| 662 |
ui.launch(inbrowser=True)
|