Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
-
|
| 5 |
import os
|
| 6 |
import requests
|
| 7 |
import dateparser
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
travel_plan_output=""
|
| 13 |
|
| 14 |
def match_europe(scores):
|
| 15 |
a = scores.count("A")
|
|
@@ -107,64 +102,90 @@ with gr.Blocks() as demo:
|
|
| 107 |
create_quiz(questions_europe, match_europe)
|
| 108 |
|
| 109 |
|
| 110 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 111 |
-
# change the LLM
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
prompt = f"""
|
| 117 |
**Travel Plan Details:**
|
| 118 |
-
- **
|
| 119 |
-
- **
|
| 120 |
-
- **
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
"""
|
| 123 |
-
messages_plan = [{"role": "system", "content": prompt}]
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
messages = [{"role": "system", "content": "You are a chatbot that helps people plan their trips to make it easier for them. If someone says they want to travel somewhere, you respond with a 3 day itinerary, popular foods there, and some basic phrases in their language.You are also very friendly!"}]
|
| 129 |
-
# change the personality of the chatbot
|
| 130 |
-
if history:
|
| 131 |
-
messages.extend(history)
|
| 132 |
-
|
| 133 |
-
messages.append({"role" : "user", "content" : message})
|
| 134 |
-
|
| 135 |
-
response = ""
|
| 136 |
-
for message in client.chat_completion(
|
| 137 |
-
messages, max_tokens = 4096, stream=True
|
| 138 |
-
#temperature= .1, top_p= 0.7)
|
| 139 |
-
# max tokens = change the length of the response
|
| 140 |
-
# temp = between 0-2
|
| 141 |
-
# top-p = between 0-1
|
| 142 |
-
):
|
| 143 |
-
token = message.choices[0].delta.content
|
| 144 |
-
if token:
|
| 145 |
-
response += token
|
| 146 |
-
yield response
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
|
|
|
|
|
|
|
|
|
| 151 |
with gr.Tabs():
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
respond,
|
| 169 |
type="messages",
|
| 170 |
title="Travia",
|
|
@@ -172,11 +193,12 @@ with gr.Blocks() as app:
|
|
| 172 |
"I would like to travel to a place, but I don't know how to plan it",
|
| 173 |
"I need to budget for my trip.",
|
| 174 |
"I want to learn more about the language, food, and culture of the place I'm traveling to."
|
| 175 |
-
]
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
import dateparser
|
| 6 |
+
from geopy.geocoders import Nominatim
|
| 7 |
+
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def match_europe(scores):
|
| 10 |
a = scores.count("A")
|
|
|
|
| 102 |
create_quiz(questions_europe, match_europe)
|
| 103 |
|
| 104 |
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
travel_guide_output = ""
|
| 112 |
+
|
| 113 |
+
def parse_date(date_str):
|
| 114 |
+
if not date_str:
|
| 115 |
+
return None
|
| 116 |
+
parsed_date = dateparser.parse(date_str, settings={'PREFER_DATES_FROM': 'future'})
|
| 117 |
+
return parsed_date if parsed_date else None
|
| 118 |
+
|
| 119 |
+
def respond(your_location, destination, transportation, date, preference, include_options):
|
| 120 |
+
global travel_guide_output
|
| 121 |
+
travel_date = parse_date(date)
|
| 122 |
|
| 123 |
prompt = f"""
|
| 124 |
**Travel Plan Details:**
|
| 125 |
+
- **From:** {your_location}
|
| 126 |
+
- **To:** {destination}
|
| 127 |
+
- **Transportation:** {transportation}
|
| 128 |
+
- **Travel Date:** {date}
|
| 129 |
+
- **Budget Preference:** {preference}
|
| 130 |
+
- **Additional Info:** {', '.join(include_options) if include_options else 'None'}
|
| 131 |
+
Provide a detailed travel guide based on these details.
|
| 132 |
"""
|
|
|
|
| 133 |
|
| 134 |
+
try:
|
| 135 |
+
response = model.generate_content([{"role": "user", "parts": [{"text": prompt}]}])
|
| 136 |
+
travel_guide_output = response.text
|
| 137 |
+
return response.text
|
| 138 |
+
except Exception as e:
|
| 139 |
+
return f"An error occurred: {e}"
|
| 140 |
+
|
| 141 |
+
def chatbot_respond(message, chat_history):
|
| 142 |
+
global travel_guide_output
|
| 143 |
+
if not travel_guide_output:
|
| 144 |
+
return "No travel guide has been generated yet. Please enter your travel details first."
|
| 145 |
|
| 146 |
+
prompt = f"""
|
| 147 |
+
You are an AI travel assistant. The user has already generated a travel guide. Use the following details to assist them:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
{travel_guide_output}
|
| 150 |
+
|
| 151 |
+
If the user asks a question related to their trip, provide an answer based on this guide.
|
| 152 |
+
If the user asks general travel questions, respond accordingly.
|
| 153 |
+
If they make casual conversation, respond naturally.
|
| 154 |
+
|
| 155 |
+
User's message: {message}
|
| 156 |
+
"""
|
| 157 |
+
|
| 158 |
+
try:
|
| 159 |
+
response = model.generate_content([{"role": "user", "parts": [{"text": prompt}]}])
|
| 160 |
+
return response.text
|
| 161 |
+
except Exception as e:
|
| 162 |
+
return f"An error occurred: {e}"
|
| 163 |
|
| 164 |
+
with gr.Blocks() as app:
|
| 165 |
+
gr.Markdown(" 🌍 Travia")
|
| 166 |
+
|
| 167 |
with gr.Tabs():
|
| 168 |
+
with gr.TabItem("Quiz"):
|
| 169 |
+
create_quiz(questions_europe, match_europe)
|
| 170 |
+
|
| 171 |
+
with gr.TabItem("Travel Chatbot"):
|
| 172 |
+
with gr.Row():
|
| 173 |
+
with gr.Column(scale=1):
|
| 174 |
+
your_location_input = gr.Textbox(label="Your Current Location")
|
| 175 |
+
destination_input = gr.Textbox(label="Travel Destination")
|
| 176 |
+
transportation_dropdown = gr.Dropdown(["Bus", "Plane", "Train"], label="Preferred Transportation")
|
| 177 |
+
date_input = gr.Textbox(label="Travel Date (e.g., tomorrow, March 15)")
|
| 178 |
+
preference_dropdown = gr.Dropdown(["Luxurious", "Cheap", "Balanced"], label="Budget Preferences")
|
| 179 |
+
include_checkboxes = gr.CheckboxGroup([
|
| 180 |
+
"Restaurant recommendations", "Hotel suggestions", "Nearby attractions", "Local tips", "Packing guides"
|
| 181 |
+
], label="Include in Chat")
|
| 182 |
+
send_button = gr.Button("Generate Guide")
|
| 183 |
+
|
| 184 |
+
with gr.Column(scale=2):
|
| 185 |
+
output_box = gr.Markdown(label="AI Output", value="Please enter your travel details and click 'Generate Guide'.")
|
| 186 |
+
|
| 187 |
+
with gr.Column(scale=1):
|
| 188 |
+
gr.ChatInterface(
|
| 189 |
respond,
|
| 190 |
type="messages",
|
| 191 |
title="Travia",
|
|
|
|
| 193 |
"I would like to travel to a place, but I don't know how to plan it",
|
| 194 |
"I need to budget for my trip.",
|
| 195 |
"I want to learn more about the language, food, and culture of the place I'm traveling to."
|
| 196 |
+
])
|
| 197 |
+
|
| 198 |
+
send_button.click(
|
| 199 |
+
respond,
|
| 200 |
+
inputs=[your_location_input, destination_input, transportation_dropdown, date_input, preference_dropdown, include_checkboxes],
|
| 201 |
+
outputs=[output_box]
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
app.launch(debug = True)
|