Spaces:
Sleeping
Sleeping
File size: 4,132 Bytes
c27052d |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import gradio as gr
import requests
import datetime
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Backend endpoint - use environment variable or default to Hugging Face
BASE_URL = os.getenv("API_BASE_URL", "https://mishrabp-trip-advisor-api.hf.space")
def format_travel_plan(answer):
"""Format the travel plan response with markdown styling"""
return f"""### πΊοΈ AI-Generated Travel Plan
**Generated:** {datetime.datetime.now().strftime('%Y-%m-%d at %H:%M')}
**Created by:** Kube9t's Travel Agent
---
{answer}
---
π *Please double-check all travel details, costs, and dates before booking.*"""
def query_travel_agent(message, history):
"""Handle the travel agent query and format the response"""
try:
# Make API request
payload = {"question": message}
response = requests.post(f"{BASE_URL}/query", json=payload)
if response.status_code == 200:
answer = response.json().get("answer", "No answer returned.")
formatted_response = {
"role": "assistant",
"content": format_travel_plan(answer)
}
else:
formatted_response = {
"role": "assistant",
"content": f"β Bot failed to respond: {response.text}"
}
except Exception as e:
formatted_response = {
"role": "assistant",
"content": f"β οΈ Something went wrong: {str(e)}"
}
# Return updated history
return history + [
{"role": "user", "content": message},
formatted_response
]
def create_demo():
"""Create and configure the Gradio interface"""
with gr.Blocks(theme=gr.themes.Soft()) as demo:
# Header
gr.Markdown(
"""
# π Travel Planner Agentic Application
Let me help you design your next perfect trip β just tell me where you want to go!
"""
)
with gr.Row():
# Sidebar
with gr.Column(scale=1):
gr.Image(
"https://cdn-icons-png.flaticon.com/512/201/201623.png",
width=80,
show_label=False
)
gr.Markdown("### Navigation")
gr.Markdown("- π Home\n- π§³ My Trips\n- βοΈ Settings")
gr.Markdown("---")
gr.Markdown("*Powered by Kube9t's Travel Agent AI*")
# Main chat interface
with gr.Column(scale=3):
chatbot = gr.Chatbot(
height=400,
show_label=False,
avatar_images=["π€", "π€"],
type="messages" # Use new message format
)
msg = gr.Textbox(
label="Ask me something like: 'Plan a 7 days trip to Washington-DC, NewYork, and Niagra.'",
placeholder="Type your travel query here...",
lines=2
)
with gr.Row():
submit = gr.Button("Send", variant="primary")
clear = gr.Button("Clear")
# Event handlers
msg.submit(
fn=query_travel_agent,
inputs=[msg, chatbot],
outputs=chatbot
).then(
fn=lambda: "",
outputs=msg
)
submit.click(
fn=query_travel_agent,
inputs=[msg, chatbot],
outputs=chatbot
).then(
fn=lambda: "",
outputs=msg
)
clear.click(lambda: [], outputs=chatbot)
return demo
# Create and launch the app
app = create_demo()
if __name__ == "__main__":
# Determine if running locally
is_local = os.getenv("RUNNING_LOCAL", "false").lower() == "true"
# Launch with appropriate settings
if is_local:
app.launch(
server_name="127.0.0.1",
server_port=7860,
share=False
)
else:
app.launch() |