tushar-r-pawar commited on
Commit
9945096
1 Parent(s): 2d18704

Upload app.py

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