Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +32 -0
- requirements.txt +7 -0
- utils.py +53 -0
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
| 4 |
+
from utils import get_model_response
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Yatra Sevak.AI ✈️", page_icon="🌍", layout="wide")
|
| 7 |
+
st.title("Yatra Sevak.AI ✈️")
|
| 8 |
+
st.caption("Planning a trip can be challenging these days. With so many choices for flights, hotels, and activities, travelers often find it difficult to pick the best options. Our Yatra Sevak.Ai chatbot is here to help. Imagine having a personal travel assistant at your fingertips someone who can book flights, find great hotels, recommend local attractions, and offer travel advice. Thanks to advanced AI, this is now possible.")
|
| 9 |
+
if "chat_history" not in st.session_state:
|
| 10 |
+
st.session_state.chat_history = [
|
| 11 |
+
AIMessage(content="Hello, I am Yatra Sevak.AI How can I help you?"),
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
for message in st.session_state.chat_history:
|
| 15 |
+
if isinstance(message, AIMessage):
|
| 16 |
+
with st.chat_message("AI"):
|
| 17 |
+
st.write(message.content)
|
| 18 |
+
elif isinstance(message, HumanMessage):
|
| 19 |
+
with st.chat_message("Human"):
|
| 20 |
+
st.write(message.content)
|
| 21 |
+
|
| 22 |
+
user_query = st.chat_input("Type your message here....")
|
| 23 |
+
if user_query is not None and user_query != "":
|
| 24 |
+
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
| 25 |
+
with st.chat_message("Human"):
|
| 26 |
+
st.markdown(user_query)
|
| 27 |
+
with st.spinner('Yatra Sevak.AI is processing your query...'):
|
| 28 |
+
response = get_model_response(user_query, st.session_state.chat_history)
|
| 29 |
+
response = response.replace("AI response:", "").replace("chat response:", "").replace("bot response:", "").strip()
|
| 30 |
+
with st.chat_message("AI"):
|
| 31 |
+
st.write(response)
|
| 32 |
+
st.session_state.chat_history.append(AIMessage(content=response))
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain-community
|
| 2 |
+
langchain-huggingface
|
| 3 |
+
langchain
|
| 4 |
+
langchain-core
|
| 5 |
+
streamlit
|
| 6 |
+
python-dotenv
|
| 7 |
+
huggingface-hub
|
utils.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
|
| 2 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
API_KEY = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 9 |
+
repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 10 |
+
task = "text-generation"
|
| 11 |
+
|
| 12 |
+
template = """
|
| 13 |
+
You are a travel assistant chatbot your name is Yatra Sevak.AI designed to help users plan their trips and provide travel-related information. Here are some scenarios you should be able to handle:
|
| 14 |
+
|
| 15 |
+
1. Booking Flights: Assist users with booking flights to their desired destinations. Ask for departure city, destination city, travel dates, and any specific preferences (e.g., direct flights, airline preferences). Check available airlines and book the tickets accordingly.
|
| 16 |
+
|
| 17 |
+
2. Booking Hotels: Help users find and book accommodations. Inquire about city or region, check-in/check-out dates, number of guests, and accommodation preferences (e.g., budget, amenities).
|
| 18 |
+
|
| 19 |
+
3. Booking Rental Cars: Facilitate the booking of rental cars for travel convenience. Gather details such as pickup/drop-off locations, dates, car preferences (e.g., size, type), and any additional requirements.
|
| 20 |
+
|
| 21 |
+
4. Destination Information: Provide information about popular travel destinations. Offer insights on attractions, local cuisine, cultural highlights, weather conditions, and best times to visit.
|
| 22 |
+
|
| 23 |
+
5. Travel Tips: Offer practical travel tips and advice. Topics may include packing essentials, visa requirements, currency exchange, local customs, and safety tips.
|
| 24 |
+
|
| 25 |
+
6. Weather Updates: Give current weather updates for specific destinations or regions. Include temperature forecasts, precipitation chances, and any weather advisories.
|
| 26 |
+
|
| 27 |
+
7. Local Attractions: Suggest local attractions and points of interest based on the user's destination. Highlight must-see landmarks, museums, parks, and recreational activities.
|
| 28 |
+
|
| 29 |
+
8. Customer Service: Address customer service inquiries and provide assistance with travel-related issues. Handle queries about bookings, cancellations, refunds, and general support.
|
| 30 |
+
|
| 31 |
+
Please ensure responses are informative, accurate, and tailored to the user's queries and preferences. Use natural language to engage users and provide a seamless experience throughout their travel planning journey.
|
| 32 |
+
|
| 33 |
+
Chat history:
|
| 34 |
+
{chat_history}
|
| 35 |
+
|
| 36 |
+
User question:
|
| 37 |
+
{user_question}
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
prompt = ChatPromptTemplate.from_template(template=template)
|
| 41 |
+
|
| 42 |
+
def get_model_response(user_query, chat_history):
|
| 43 |
+
llm = HuggingFaceEndpoint(
|
| 44 |
+
repo_id=repo_id,
|
| 45 |
+
task=task,
|
| 46 |
+
huggingfacehub_api_token=API_KEY,
|
| 47 |
+
)
|
| 48 |
+
chain = prompt | llm | StrOutputParser()
|
| 49 |
+
response = chain.invoke({
|
| 50 |
+
"chat_history": chat_history,
|
| 51 |
+
"user_question": user_query,
|
| 52 |
+
})
|
| 53 |
+
return response
|