tushar-r-pawar commited on
Commit
58cb1a3
1 Parent(s): 7eda90f

Create app_2.py

Browse files
Files changed (1) hide show
  1. app_2.py +100 -0
app_2.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import fitz # PyMuPDF
9
+
10
+ load_dotenv()
11
+
12
+ api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
13
+
14
+ # Define the repository ID and task
15
+ repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
16
+ task = "text-generation"
17
+
18
+ # App config
19
+ st.set_page_config(page_title="Yatra Sevak.AI", page_icon="🌍")
20
+ st.title("Yatra Sevak.AI ✈️")
21
+
22
+ # Define the template outside the function
23
+ template = """
24
+ 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:
25
+ 1. Destination Information: Provide information about popular travel destinations. Offer insights on attractions, local cuisine, cultural highlights, weather conditions, and best times to visit.
26
+ 2. Travel Tips: Offer practical travel tips and advice. Topics may include packing essentials, visa requirements, currency exchange, local customs, and safety tips.
27
+ 3. 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
+ 4. Customer Service: Address customer service inquiries and provide assistance with travel-related issues.
29
+ 5. Online Marketing & Sales Associate: Act as an Online Marketing & Sales Associate and try to suggest our services to the user. If you think that the user can be converted to a client, then ask for their contact information and also give them our contact information.
30
+ 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.
31
+ Chat history:
32
+ {chat_history}
33
+ User question:
34
+ {user_question}
35
+ """
36
+
37
+ pdf_path = "YatraAI_Travel_Plans.pdf"
38
+
39
+ def extract_text_from_pdf(pdf_path):
40
+ doc = fitz.open(pdf_path)
41
+ text = ""
42
+ for page in doc:
43
+ text += page.get_text()
44
+ return text
45
+
46
+ pdf_text = extract_text_from_pdf(pdf_path)
47
+
48
+ # Function to get a response from the model
49
+ def get_response(user_query, chat_history):
50
+ # Initialize the Hugging Face Endpoint
51
+ llm = HuggingFaceEndpoint(
52
+ huggingfacehub_api_token=api_token,
53
+ repo_id=repo_id,
54
+ task=task
55
+ )
56
+
57
+ # Include the PDF content in the prompt
58
+ prompt_with_pdf = f"{template}\n\nCompany Info and Travel Plans:\n{pdf_text}"
59
+
60
+ chain = ChatPromptTemplate.from_template(prompt_with_pdf) | llm | StrOutputParser()
61
+
62
+ response = chain.invoke({
63
+ "chat_history": chat_history,
64
+ "user_question": user_query,
65
+ })
66
+
67
+ return response
68
+
69
+ # Initialize session state
70
+ if "chat_history" not in st.session_state:
71
+ st.session_state.chat_history = [
72
+ AIMessage(content="Hello, I am Yatra Sevak.AI. How can I help you?"),
73
+ ]
74
+
75
+ # Display chat history
76
+ for message in st.session_state.chat_history:
77
+ if isinstance(message, AIMessage):
78
+ with st.chat_message("AI"):
79
+ st.write(message.content)
80
+ elif isinstance(message, HumanMessage):
81
+ with st.chat_message("Human"):
82
+ st.write(message.content)
83
+
84
+ # User input
85
+ user_query = st.chat_input("Type your message here...")
86
+ if user_query is not None and user_query != "":
87
+ st.session_state.chat_history.append(HumanMessage(content=user_query))
88
+
89
+ with st.chat_message("Human"):
90
+ st.markdown(user_query)
91
+
92
+ response = get_response(user_query, st.session_state.chat_history)
93
+
94
+ # Remove any unwanted prefixes from the response
95
+ response = response.replace("AI response:", "").replace("chat response:", "").replace("bot response:", "").strip()
96
+
97
+ with st.chat_message("AI"):
98
+ st.write(response)
99
+
100
+ st.session_state.chat_history.append(AIMessage(content=response))