Update app.py
Browse files
app.py
CHANGED
@@ -1,103 +1,120 @@
|
|
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 environment variables from .env file
|
10 |
-
load_dotenv()
|
11 |
-
|
12 |
-
# Get the API token from environment variable
|
13 |
-
api_token = os.getenv("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="GOAHEAD.AI",page_icon= "π")
|
21 |
-
st.title("GOAHEAD.AI βοΈ")
|
22 |
-
|
23 |
-
# Define the template outside the function
|
24 |
-
template = """
|
25 |
-
You are a travel assistant chatbot your name is GOAHEAD.AI designed to help users plan their trips and provide travel-related information.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
st.
|
102 |
-
|
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 |
+
# Load environment variables from .env file
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Get the API token from environment variable
|
13 |
+
api_token = os.getenv("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="GOAHEAD.AI",page_icon= "π")
|
21 |
+
st.title("GOAHEAD.AI βοΈ")
|
22 |
+
|
23 |
+
# Define the template outside the function
|
24 |
+
template = """
|
25 |
+
You are a travel assistant chatbot your name is GOAHEAD.AI designed to help users plan their trips and provide travel-related information.
|
26 |
+
Here are some scenarios you should be able to handle:
|
27 |
+
|
28 |
+
1. Booking Flights: Assist users with booking flights to their desired destinations.
|
29 |
+
Ask for departure city, destination city, travel dates, and any specific preferences (e.g., direct flights, airline preferences).
|
30 |
+
Check available airlines and book the tickets accordingly.
|
31 |
+
|
32 |
+
2. Booking Hotels: Help users find and book accommodations.
|
33 |
+
Inquire about city or region, check-in/check-out dates, number of guests, and accommodation preferences (e.g., budget, amenities).
|
34 |
+
|
35 |
+
3. Booking Rental Cars: Facilitate the booking of rental cars for travel convenience.
|
36 |
+
Gather details such as pickup/drop-off locations, dates, car preferences (e.g., size, type), and any additional requirements.
|
37 |
+
|
38 |
+
4. Destination Information: Provide information about popular travel destinations.
|
39 |
+
Offer insights on attractions, local cuisine, cultural highlights, weather conditions, and best times to visit.
|
40 |
+
|
41 |
+
5. Travel Tips: Offer practical travel tips and advice.
|
42 |
+
Topics may include packing essentials, visa requirements, currency exchange, local customs, and safety tips.
|
43 |
+
|
44 |
+
6. Weather Updates: Give current weather updates for specific destinations or regions.
|
45 |
+
Include temperature forecasts, precipitation chances, and any weather advisories.
|
46 |
+
|
47 |
+
7. Local Attractions: Suggest local attractions and points of interest based on the user's destination.
|
48 |
+
Highlight must-see landmarks, museums, parks, and recreational activities.
|
49 |
+
|
50 |
+
8. Customer Service: Address customer service inquiries and provide assistance with travel-related issues.
|
51 |
+
Handle queries about bookings, cancellations, refunds, and general support.
|
52 |
+
|
53 |
+
Please ensure responses are informative, accurate, and tailored to the user's queries and preferences.
|
54 |
+
Use natural language to engage users and provide a seamless experience throughout their travel planning journey.
|
55 |
+
|
56 |
+
Chat history:
|
57 |
+
{chat_history}
|
58 |
+
|
59 |
+
User question:
|
60 |
+
{user_question}
|
61 |
+
"""
|
62 |
+
|
63 |
+
prompt = ChatPromptTemplate.from_template(template)
|
64 |
+
|
65 |
+
# Function to get a response from the model
|
66 |
+
def get_response(user_query, chat_history):
|
67 |
+
# Initialize the Hugging Face Endpoint
|
68 |
+
llm = HuggingFaceEndpoint(
|
69 |
+
huggingfacehub_api_token=api_token,
|
70 |
+
repo_id=repo_id,
|
71 |
+
task=task
|
72 |
+
)
|
73 |
+
|
74 |
+
chain = prompt | llm | StrOutputParser()
|
75 |
+
|
76 |
+
response = chain.invoke({
|
77 |
+
"chat_history": chat_history,
|
78 |
+
"user_question": user_query,
|
79 |
+
})
|
80 |
+
|
81 |
+
return response
|
82 |
+
|
83 |
+
# Initialize session state
|
84 |
+
if "chat_history" not in st.session_state:
|
85 |
+
st.session_state.chat_history = [
|
86 |
+
AIMessage(content="Hello, how can I help you?"),
|
87 |
+
]
|
88 |
+
|
89 |
+
# Clear chat history if button is pressed
|
90 |
+
if st.button("Clear Chat"):
|
91 |
+
st.session_state.chat_history = [
|
92 |
+
AIMessage(content="Hello, how can I help you?"),
|
93 |
+
]
|
94 |
+
|
95 |
+
# Display chat history
|
96 |
+
for message in st.session_state.chat_history:
|
97 |
+
if isinstance(message, AIMessage):
|
98 |
+
with st.chat_message("AI"):
|
99 |
+
st.write(message.content)
|
100 |
+
elif isinstance(message, HumanMessage):
|
101 |
+
with st.chat_message("Human"):
|
102 |
+
st.write(message.content)
|
103 |
+
|
104 |
+
# User input
|
105 |
+
user_query = st.chat_input("Type your message here...")
|
106 |
+
if user_query is not None and user_query != "":
|
107 |
+
st.session_state.chat_history.append(HumanMessage(content=user_query))
|
108 |
+
|
109 |
+
with st.chat_message("Human"):
|
110 |
+
st.markdown(user_query)
|
111 |
+
|
112 |
+
response = get_response(user_query, st.session_state.chat_history)
|
113 |
+
|
114 |
+
# Remove any unwanted prefixes from the response
|
115 |
+
response = response.replace("AI response:", "").replace("chat response:", "").replace("bot response:", "").strip()
|
116 |
+
|
117 |
+
with st.chat_message("AI"):
|
118 |
+
st.write(response)
|
119 |
+
|
120 |
+
st.session_state.chat_history.append(AIMessage(content=response))
|