Spaces:
Sleeping
Sleeping
mohamirf
commited on
Commit
β’
b035e2f
1
Parent(s):
3fd282d
draft version.
Browse files- app.py +96 -0
- requirement.txt +5 -0
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
5 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
6 |
+
from langchain_core.output_parsers import StrOutputParser
|
7 |
+
from langchain_core.prompts import ChatPromptTemplate
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Define the repository ID and task
|
12 |
+
repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
13 |
+
task = "text-generation"
|
14 |
+
|
15 |
+
st.set_page_config(page_title="Yatra Sevak.AI",page_icon= "π")
|
16 |
+
st.title("Yatra Sevak.AI βοΈ")
|
17 |
+
|
18 |
+
template = """
|
19 |
+
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:
|
20 |
+
|
21 |
+
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.
|
22 |
+
|
23 |
+
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).
|
24 |
+
|
25 |
+
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.
|
26 |
+
|
27 |
+
4. Destination Information: Provide information about popular travel destinations. Offer insights on attractions, local cuisine, cultural highlights, weather conditions, and best times to visit.
|
28 |
+
|
29 |
+
5. Travel Tips: Offer practical travel tips and advice. Topics may include packing essentials, visa requirements, currency exchange, local customs, and safety tips.
|
30 |
+
|
31 |
+
6. Weather Updates: Give current weather updates for specific destinations or regions. Include temperature forecasts, precipitation chances, and any weather advisories.
|
32 |
+
|
33 |
+
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.
|
34 |
+
|
35 |
+
8. Customer Service: Address customer service inquiries and provide assistance with travel-related issues. Handle queries about bookings, cancellations, refunds, and general support.
|
36 |
+
|
37 |
+
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.
|
38 |
+
|
39 |
+
Chat history:
|
40 |
+
{chat_history}
|
41 |
+
|
42 |
+
User question:
|
43 |
+
{user_question}
|
44 |
+
"""
|
45 |
+
|
46 |
+
prompt = ChatPromptTemplate.from_template(template)
|
47 |
+
|
48 |
+
def get_response(user_query, chat_history):
|
49 |
+
# Initialize the Hugging Face Endpoint
|
50 |
+
llm = HuggingFaceEndpoint(
|
51 |
+
huggingfacehub_api_token=api_token,
|
52 |
+
repo_id=repo_id,
|
53 |
+
task=task
|
54 |
+
)
|
55 |
+
|
56 |
+
chain = prompt | llm | StrOutputParser()
|
57 |
+
|
58 |
+
response = chain.invoke({
|
59 |
+
"chat_history": chat_history,
|
60 |
+
"user_question": user_query,
|
61 |
+
})
|
62 |
+
|
63 |
+
return response
|
64 |
+
|
65 |
+
# Initialize session state
|
66 |
+
if "chat_history" not in st.session_state:
|
67 |
+
st.session_state.chat_history = [
|
68 |
+
AIMessage(content="Hello, I am Yatra Sevak.AI How can I help you?"),
|
69 |
+
]
|
70 |
+
|
71 |
+
# Display chat history
|
72 |
+
for message in st.session_state.chat_history:
|
73 |
+
if isinstance(message, AIMessage):
|
74 |
+
with st.chat_message("AI"):
|
75 |
+
st.write(message.content)
|
76 |
+
elif isinstance(message, HumanMessage):
|
77 |
+
with st.chat_message("Human"):
|
78 |
+
st.write(message.content)
|
79 |
+
|
80 |
+
# User input
|
81 |
+
user_query = st.chat_input("Type your message here...")
|
82 |
+
if user_query is not None and user_query != "":
|
83 |
+
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
84 |
+
|
85 |
+
with st.chat_message("Human"):
|
86 |
+
st.markdown(user_query)
|
87 |
+
|
88 |
+
response = get_response(user_query, st.session_state.chat_history)
|
89 |
+
|
90 |
+
# Remove any unwanted prefixes from the response
|
91 |
+
response = response.replace("AI response:", "").replace("chat response:", "").replace("bot response:", "").strip()
|
92 |
+
|
93 |
+
with st.chat_message("AI"):
|
94 |
+
st.write(response)
|
95 |
+
|
96 |
+
st.session_state.chat_history.append(AIMessage(content=response))
|
requirement.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
python-dotenv
|
3 |
+
langchain-core
|
4 |
+
langchain-community
|
5 |
+
huggingface-hub
|