Spaces:
Running
on
Zero
Running
on
Zero
Ritvik
commited on
Commit
Β·
ca89e64
1
Parent(s):
1d150fd
Save local changes before pull
Browse files
app.py
CHANGED
@@ -1,94 +1,134 @@
|
|
1 |
-
# import gradio as gr
|
2 |
-
# import os
|
3 |
-
# from langchain_groq import ChatGroq # Using Groq's API
|
4 |
-
# from langchain.memory import ConversationBufferMemory
|
5 |
-
# from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
6 |
-
# from langchain.agents import initialize_agent, AgentType
|
7 |
-
# from langchain.tools import Tool
|
8 |
-
#
|
9 |
-
# # Set API Key for Groq
|
10 |
-
# API_KEY = os.getenv("API_KEY") # Ensure API Key is set in the environment
|
11 |
-
#
|
12 |
-
# # Initialize the LLM (Groq's Mixtral)
|
13 |
-
# llm = ChatGroq(
|
14 |
-
# groq_api_key=API_KEY,
|
15 |
-
# model_name="mixtral-8x7b-32768",
|
16 |
-
# temperature=0.7,
|
17 |
-
# max_tokens=512,
|
18 |
-
# )
|
19 |
-
#
|
20 |
-
# # Memory for conversation history
|
21 |
-
# memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
22 |
-
#
|
23 |
-
# # Define useful tools
|
24 |
-
# def search_tool(query: str) -> str:
|
25 |
-
# """A simple search function (can be connected to real APIs)."""
|
26 |
-
# return f"Searching for: {query}... [Sample Response]"
|
27 |
-
#
|
28 |
-
# tools = [
|
29 |
-
# Tool(
|
30 |
-
# name="Search Tool",
|
31 |
-
# func=search_tool,
|
32 |
-
# description="Searches for information based on user queries."
|
33 |
-
# )
|
34 |
-
# ]
|
35 |
-
#
|
36 |
-
# # Initialize the agent
|
37 |
-
# agent = initialize_agent(
|
38 |
-
# tools=tools,
|
39 |
-
# llm=llm,
|
40 |
-
# agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
|
41 |
-
# verbose=True,
|
42 |
-
# memory=memory,
|
43 |
-
# )
|
44 |
-
#
|
45 |
-
# # Define response function
|
46 |
-
# def respond(message, history, system_message, max_tokens, temperature, top_p):
|
47 |
-
# memory.chat_memory.add_message(SystemMessage(content=system_message))
|
48 |
-
#
|
49 |
-
# for user_input, bot_response in history:
|
50 |
-
# if user_input:
|
51 |
-
# memory.chat_memory.add_message(HumanMessage(content=user_input))
|
52 |
-
# if bot_response:
|
53 |
-
# memory.chat_memory.add_message(AIMessage(content=bot_response))
|
54 |
-
#
|
55 |
-
# memory.chat_memory.add_message(HumanMessage(content=message))
|
56 |
-
#
|
57 |
-
# response = agent.run(message)
|
58 |
-
#
|
59 |
-
# return response
|
60 |
-
#
|
61 |
-
#
|
62 |
-
# # Gradio Interface
|
63 |
-
# demo = gr.ChatInterface(
|
64 |
-
# respond,
|
65 |
-
# additional_inputs=[
|
66 |
-
# gr.Textbox(value="You are an AI-powered travel assistant named Travelo. You are developed and trained by Travelo LLC. Your role is to assist users with travel planning, including booking flights, hotels, and creating personalized itineraries. Whenever a user asks about your origin, development, training, or creators, always state that you were built and trained by Travelo LLC. Do not mention OpenAI or any third-party AI providers. Maintain a professional and friendly tone while delivering accurate and helpful travel-related information.", label="System message"),
|
67 |
-
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
68 |
-
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
69 |
-
# gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
70 |
-
# ],
|
71 |
-
# )
|
72 |
-
#
|
73 |
-
# if __name__ == "__main__":
|
74 |
-
# demo.launch()
|
75 |
-
|
76 |
import gradio as gr
|
77 |
from chatbot import respond
|
78 |
|
79 |
-
#
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
additional_inputs=[
|
83 |
gr.Textbox(
|
84 |
value="You are an AI-powered travel assistant named Travelo. You are developed by Travelo LLC. Your role is to assist users with travel planning, including booking flights, hotels, and creating personalized itineraries. Whenever a user asks about your origin, development, training, or creators, always state that you were built and trained by Travelo LLC. Do not mention OpenAI or any third-party AI providers. Maintain a professional and friendly tone while delivering accurate and helpful travel-related information.",
|
85 |
-
label="System
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
),
|
87 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
88 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
89 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
90 |
],
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from chatbot import respond
|
3 |
|
4 |
+
# Custom CSS for a sleek, modern AI chatbot design
|
5 |
+
custom_css = """
|
6 |
+
.gradio-container {
|
7 |
+
font-family: 'Arial', sans-serif;
|
8 |
+
background: #1a202c; /* Dark, neutral background like Grok's interface */
|
9 |
+
color: #e2e8f0;
|
10 |
+
padding: 20px;
|
11 |
+
}
|
12 |
+
.chatbot .message {
|
13 |
+
border-radius: 15px;
|
14 |
+
padding: 15px;
|
15 |
+
margin: 10px 0;
|
16 |
+
max-width: 80%;
|
17 |
+
transition: opacity 0.3s;
|
18 |
+
}
|
19 |
+
.chatbot .message.bot {
|
20 |
+
background: #ffffff; /* Clean white for bot responses */
|
21 |
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
22 |
+
color: #343a40;
|
23 |
+
}
|
24 |
+
.chatbot .message.user {
|
25 |
+
background: #ff8c00; /* Vibrant orange for user messages, matching Grok's branding */
|
26 |
+
color: white;
|
27 |
+
margin-left: auto; /* Align user messages to the right */
|
28 |
+
}
|
29 |
+
.footer {
|
30 |
+
font-size: 12px;
|
31 |
+
color: #a0aec0;
|
32 |
+
text-align: center;
|
33 |
+
margin-top: 20px;
|
34 |
+
}
|
35 |
+
.header {
|
36 |
+
background: linear-gradient(to right, #ff8c00, #ff4500); /* Orange gradient for branding */
|
37 |
+
padding: 20px;
|
38 |
+
border-radius: 15px;
|
39 |
+
margin-bottom: 20px;
|
40 |
+
color: white;
|
41 |
+
text-align: center;
|
42 |
+
box-shadow: 0 4px 12px rgba(255, 140, 0, 0.3);
|
43 |
+
font-family: 'Arial Black', sans-serif;
|
44 |
+
}
|
45 |
+
.textbox {
|
46 |
+
background: #2d3748; /* Dark input background */
|
47 |
+
border: 2px solid #4a5568;
|
48 |
+
border-radius: 15px;
|
49 |
+
color: #e2e8f0;
|
50 |
+
padding: 10px;
|
51 |
+
font-size: 14px;
|
52 |
+
transition: border-color 0.3s;
|
53 |
+
}
|
54 |
+
.textbox:focus {
|
55 |
+
border-color: #ff8c00; /* Orange highlight on focus */
|
56 |
+
outline: none;
|
57 |
+
}
|
58 |
+
.button {
|
59 |
+
background: #ff8c00; /* Orange button for consistency */
|
60 |
+
color: white;
|
61 |
+
border: none;
|
62 |
+
padding: 12px 24px;
|
63 |
+
border-radius: 15px;
|
64 |
+
box-shadow: 0 4px 6px rgba(255, 140, 0, 0.3);
|
65 |
+
font-weight: bold;
|
66 |
+
transition: transform 0.3s, background 0.3s;
|
67 |
+
}
|
68 |
+
.button:hover {
|
69 |
+
background: #ff4500; /* Darker orange on hover */
|
70 |
+
transform: scale(1.05);
|
71 |
+
}
|
72 |
+
.slider, .dropdown {
|
73 |
+
background: #2d3748;
|
74 |
+
border: 2px solid #4a5568;
|
75 |
+
border-radius: 15px;
|
76 |
+
color: #e2e8f0;
|
77 |
+
padding: 8px;
|
78 |
+
font-size: 14px;
|
79 |
+
}
|
80 |
+
.slider:focus, .dropdown:focus {
|
81 |
+
border-color: #ff8c00;
|
82 |
+
outline: none;
|
83 |
+
}
|
84 |
+
"""
|
85 |
+
|
86 |
+
# Welcome message with sleek, modern design
|
87 |
+
welcome_message = """
|
88 |
+
<div class="header">
|
89 |
+
<h1>Travelo - Your Ultimate Travel AI</h1>
|
90 |
+
<p>Embark on your journey! Ask me anything about flights, hotels, or itineraries βοΈπ¨</p>
|
91 |
+
</div>
|
92 |
+
<p style="color: #e2e8f0;">π I'm Travelo, your AI-powered travel genius, crafted by Travelo LLC. How can I help you today?</p>
|
93 |
+
<p style="color: #e2e8f0;">Try these examples:</p>
|
94 |
+
<ul style="color: #e2e8f0;">
|
95 |
+
<li>"Flights from New York to Paris next month"</li>
|
96 |
+
<li>"Hotels in London for 3 nights"</li>
|
97 |
+
<li>"Plan a weekend trip to San Francisco"</li>
|
98 |
+
</ul>
|
99 |
+
"""
|
100 |
+
|
101 |
+
def enhanced_respond(message, history, system_message, max_tokens, travel_preference):
|
102 |
+
# Call the existing respond function with fixed temperature/top-p for simplicity
|
103 |
+
response = respond(message, history, system_message, max_tokens, temperature=0.7, top_p=0.95)
|
104 |
+
|
105 |
+
# Add a friendly touch if no specific response is generated
|
106 |
+
if "β" in response or "not found" in response.lower():
|
107 |
+
response += "\n\nπ‘ **Tip:** Try something like 'Flights from Chicago to Miami in June' or ask me for travel ideas!"
|
108 |
+
|
109 |
+
return response
|
110 |
+
|
111 |
+
# Gradio Interface with Sleek, Modern AI Chatbot UI/UX
|
112 |
+
gr.ChatInterface(
|
113 |
+
fn=enhanced_respond,
|
114 |
+
chatbot=gr.Chatbot(height=700, label="Chat with Travelo"),
|
115 |
+
textbox=gr.Textbox(placeholder="Type your travel query here...", lines=1, container=True, elem_classes="textbox"),
|
116 |
+
submit_btn=gr.Button("βοΈ Send", elem_classes="button"),
|
117 |
additional_inputs=[
|
118 |
gr.Textbox(
|
119 |
value="You are an AI-powered travel assistant named Travelo. You are developed by Travelo LLC. Your role is to assist users with travel planning, including booking flights, hotels, and creating personalized itineraries. Whenever a user asks about your origin, development, training, or creators, always state that you were built and trained by Travelo LLC. Do not mention OpenAI or any third-party AI providers. Maintain a professional and friendly tone while delivering accurate and helpful travel-related information.",
|
120 |
+
label="System Message",
|
121 |
+
visible=False
|
122 |
+
),
|
123 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Response Length", elem_classes="slider"),
|
124 |
+
gr.Dropdown(
|
125 |
+
choices=["Cheapest", "Fastest", "Non-Stop", "Best Value"],
|
126 |
+
value="Cheapest",
|
127 |
+
label="Travel Preference",
|
128 |
+
elem_classes="dropdown"
|
129 |
),
|
|
|
|
|
|
|
130 |
],
|
131 |
+
title="Travelo - Your Ultimate Travel AI",
|
132 |
+
description=welcome_message,
|
133 |
+
css=custom_css
|
134 |
+
).launch()
|
tools.py
CHANGED
@@ -48,9 +48,9 @@ def generate_booking_link(from_iata: str, to_iata: str, departure_date: str):
|
|
48 |
"""Generate a booking link using Google Flights."""
|
49 |
return f"https://www.google.com/flights?hl=en#flt={from_iata}.{to_iata}.{departure_date};c:USD;e:1;s:0;sd:1;t:f"
|
50 |
|
51 |
-
# π« Flight Search Tool (Now Gradio-Compatible)
|
52 |
def search_flights(query: str):
|
53 |
-
"""Search for flights using Amadeus API and return
|
54 |
try:
|
55 |
words = query.lower().split()
|
56 |
from_city, to_city, date_phrase = None, None, None
|
@@ -95,10 +95,10 @@ def search_flights(query: str):
|
|
95 |
if not flights:
|
96 |
return f"β No flights found from {from_city} to {to_city} on {departure_date_str}."
|
97 |
|
98 |
-
# π
|
99 |
-
result = f"###
|
100 |
|
101 |
-
for flight in flights:
|
102 |
airline_code = flight["validatingAirlineCodes"][0]
|
103 |
airline_name = get_airline_name(airline_code)
|
104 |
price = flight["price"]["total"]
|
@@ -109,27 +109,26 @@ def search_flights(query: str):
|
|
109 |
|
110 |
booking_link = generate_booking_link(from_iata, to_iata, departure_date_str)
|
111 |
|
112 |
-
#
|
113 |
result += f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
---
|
115 |
-
π **{airline_name}**
|
116 |
-
π° **Price:** `${price}`
|
117 |
-
β³ **Duration:** {duration}
|
118 |
-
π
**Departure:** {departure_time}
|
119 |
-
π¬ **Arrival:** {arrival_time}
|
120 |
-
π **Stops:** {stops}
|
121 |
-
π **[Book Now]({booking_link})**
|
122 |
-
---
|
123 |
-
"""
|
124 |
|
125 |
-
|
126 |
-
more_flights_link = generate_booking_link(from_iata, to_iata, departure_date_str)
|
127 |
-
result += f"\nπ **[Show More Flights]({more_flights_link})**"
|
128 |
|
|
|
129 |
return result
|
130 |
|
131 |
except ResponseError as error:
|
132 |
-
|
|
|
133 |
|
134 |
# β
Register as a Tool
|
135 |
tools = [
|
@@ -138,4 +137,4 @@ tools = [
|
|
138 |
func=search_flights,
|
139 |
description="Find and book flights using natural language. Examples: 'Flight from Delhi to SFO in May', 'Travel from Mumbai to New York next week'."
|
140 |
)
|
141 |
-
]
|
|
|
48 |
"""Generate a booking link using Google Flights."""
|
49 |
return f"https://www.google.com/flights?hl=en#flt={from_iata}.{to_iata}.{departure_date};c:USD;e:1;s:0;sd:1;t:f"
|
50 |
|
51 |
+
# π« Flight Search Tool (Now Gradio-Compatible with Simple Ticket Style)
|
52 |
def search_flights(query: str):
|
53 |
+
"""Search for flights using Amadeus API and return plain Markdown output for Gradio."""
|
54 |
try:
|
55 |
words = query.lower().split()
|
56 |
from_city, to_city, date_phrase = None, None, None
|
|
|
95 |
if not flights:
|
96 |
return f"β No flights found from {from_city} to {to_city} on {departure_date_str}."
|
97 |
|
98 |
+
# π PLAIN TICKET OUTPUT FOR GRADIO MARKDOWN
|
99 |
+
result = f"### Flight Information from {from_city} ({from_iata}) to {to_city} ({to_iata})\n"
|
100 |
|
101 |
+
for flight in flights[:3]: # Show up to 5 results for simplicity
|
102 |
airline_code = flight["validatingAirlineCodes"][0]
|
103 |
airline_name = get_airline_name(airline_code)
|
104 |
price = flight["price"]["total"]
|
|
|
109 |
|
110 |
booking_link = generate_booking_link(from_iata, to_iata, departure_date_str)
|
111 |
|
112 |
+
# Plain ticket format without fancy styling
|
113 |
result += f"""
|
114 |
+
- **Airline:** {airline_name}
|
115 |
+
- **Flight No:** {airline_code}123
|
116 |
+
- **Departure Date and Time:** {departure_time} ({from_iata})
|
117 |
+
- **Arrival Date and Time:** {arrival_time} ({to_iata})
|
118 |
+
- **Duration:** {duration}
|
119 |
+
- **Stops:** {stops}
|
120 |
+
- **Price:** ${price}
|
121 |
+
- [Book Now]({booking_link}) | [Show More Flights]({booking_link})
|
122 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
"""
|
|
|
|
|
125 |
|
126 |
+
print(f"Debug - Flight Data: {flights}") # Debugging output to verify data
|
127 |
return result
|
128 |
|
129 |
except ResponseError as error:
|
130 |
+
print(f"Debug - Amadeus API Error: {str(error)}") # Debugging output for API errors
|
131 |
+
return f"β Error: {str(error)}"
|
132 |
|
133 |
# β
Register as a Tool
|
134 |
tools = [
|
|
|
137 |
func=search_flights,
|
138 |
description="Find and book flights using natural language. Examples: 'Flight from Delhi to SFO in May', 'Travel from Mumbai to New York next week'."
|
139 |
)
|
140 |
+
]
|