cas_task_1 / app.py
AndreaBagno's picture
feature
504e9d7
Raw
History Blame Contribute Delete
3.56 kB
import gradio as gr
import os
from agent import GenerativeAIAgent
# Initialize the agent
agent = GenerativeAIAgent()
def chat_with_agent(user_input, history):
"""Chat function that maintains conversation history"""
if not user_input.strip():
return history, history
response = agent.generate_response(user_input)
history.append((user_input, response))
return history, history
# Create fancy Gradio interface with custom theme
with gr.Blocks(
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="indigo",
neutral_hue="slate",
),
css="""
.gradio-container {
font-family: 'Inter', sans-serif;
}
.chat-container {
border-radius: 15px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
footer {
display: none !important;
}
"""
) as demo:
gr.Markdown(
"""
# πŸ€– AI Assistant with Tool Integration
**Powered by GPT-5** β€’ Ask anything and I'll use Wikipedia or web search to find accurate answers!
---
""",
elem_classes="header"
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown(
"""
### πŸ› οΈ Available Tools
- πŸ“š **Wikipedia**: For factual information
- 🌐 **Tavily Search**: For latest news & web content
### πŸ’‘ Try asking:
- "What's the latest news about AI?"
- "Tell me about quantum computing"
- "Recent developments in climate change"
""",
elem_classes="sidebar"
)
with gr.Column(scale=3):
chatbot = gr.Chatbot(
label="Conversation",
height=500,
bubble_full_width=False,
avatar_images=(None, "assistant_avatar.png"),
elem_classes="chat-container"
)
with gr.Row():
user_input = gr.Textbox(
label="Your Message",
placeholder="Type your question here... πŸ’¬",
lines=2,
scale=4
)
submit_btn = gr.Button("Send πŸš€", variant="primary", scale=1)
with gr.Row():
clear_btn = gr.Button("Clear Chat πŸ—‘οΈ", variant="secondary")
# Store conversation history
state = gr.State([])
# Event handlers
submit_btn.click(
fn=chat_with_agent,
inputs=[user_input, state],
outputs=[chatbot, state]
).then(
lambda: "",
outputs=[user_input]
)
user_input.submit(
fn=chat_with_agent,
inputs=[user_input, state],
outputs=[chatbot, state]
).then(
lambda: "",
outputs=[user_input]
)
clear_btn.click(
lambda: ([], []),
outputs=[chatbot, state]
)
gr.Markdown(
"""
---
πŸ”’ **Privacy**: Your conversations are not stored β€’ ⚑ **Fast**: Powered by OpenAI's latest models
""",
elem_classes="footer"
)
# Launch the app
demo.launch(share=False)