Spaces:
Runtime error
Runtime error
Updated the app.py
Browse files
app.py
CHANGED
@@ -1,187 +1,89 @@
|
|
|
|
|
|
1 |
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
# df = pd.read_json("./tourisme_chatbot.json")
|
6 |
-
|
7 |
-
# context_data = []
|
8 |
-
# for i in range(len(df)):
|
9 |
-
# context = ""
|
10 |
-
# for j in range(4):
|
11 |
-
# context += df.columns[j]
|
12 |
-
# context += ": "
|
13 |
-
# context += df.iloc[i][j]
|
14 |
-
# context += " "
|
15 |
-
# context_data.append(context)
|
16 |
-
|
17 |
-
|
18 |
-
# import os
|
19 |
-
|
20 |
-
# # Get the secret key from the environment
|
21 |
-
# groq_key = os.environ.get('groq_api_key')
|
22 |
-
|
23 |
-
# ## LLM used for RAG
|
24 |
-
# from langchain_groq import ChatGroq
|
25 |
-
|
26 |
-
# llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key)
|
27 |
-
|
28 |
-
# ## Embedding model!
|
29 |
-
# from langchain_huggingface import HuggingFaceEmbeddings
|
30 |
-
# embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
31 |
-
|
32 |
-
# # create vector store!
|
33 |
-
# from langchain_chroma import Chroma
|
34 |
-
|
35 |
-
# vectorstore = Chroma(
|
36 |
-
# collection_name="tourism_dataset_store",
|
37 |
-
# embedding_function=embed_model,
|
38 |
-
# persist_directory="./",
|
39 |
-
# )
|
40 |
-
|
41 |
-
# # add data to vector nstore
|
42 |
-
# vectorstore.add_texts(context_data)
|
43 |
-
|
44 |
-
# retriever = vectorstore.as_retriever()
|
45 |
-
|
46 |
-
# from langchain_core.prompts import PromptTemplate
|
47 |
-
|
48 |
-
# template = ("""You are a Moroccan tourism expert.
|
49 |
-
# Use the provided context to answer the question.
|
50 |
-
# If you don't know the answer, say so. Explain your answer in detail.
|
51 |
-
# Do not discuss the context in your response; just provide the answer directly.
|
52 |
-
# Context: {context}
|
53 |
-
# Question: {question}
|
54 |
-
# Answer:""")
|
55 |
-
|
56 |
-
# rag_prompt = PromptTemplate.from_template(template)
|
57 |
-
|
58 |
-
# from langchain_core.output_parsers import StrOutputParser
|
59 |
-
# from langchain_core.runnables import RunnablePassthrough
|
60 |
-
|
61 |
-
# rag_chain = (
|
62 |
-
# {"context": retriever, "question": RunnablePassthrough()}
|
63 |
-
# | rag_prompt
|
64 |
-
# | llm
|
65 |
-
# | StrOutputParser()
|
66 |
-
# )
|
67 |
-
|
68 |
-
# import gradio as gr
|
69 |
-
|
70 |
-
# def rag_memory_stream(text):
|
71 |
-
# partial_text = ""
|
72 |
-
# for new_text in rag_chain.stream(text):
|
73 |
-
# partial_text += new_text
|
74 |
-
# yield partial_text
|
75 |
-
|
76 |
-
# examples = ['Tourist attraction sites in Morocco', 'What are some fun activities to do in Morocco?']
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
|
|
|
|
79 |
|
|
|
80 |
|
81 |
-
#
|
82 |
-
|
83 |
-
|
84 |
-
# fn=rag_memory_stream,
|
85 |
-
# inputs="text",
|
86 |
-
# outputs="text",
|
87 |
-
# examples=examples,
|
88 |
-
# allow_flagging="never",
|
89 |
-
# )
|
90 |
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
import gradio as gr
|
98 |
|
99 |
-
|
100 |
-
df = pd.read_json("./tourisme_chatbot.json")
|
101 |
|
102 |
-
context_data = []
|
103 |
-
for i in range(len(df)):
|
104 |
-
context = ""
|
105 |
-
for j in range(4):
|
106 |
-
context += df.columns[j]
|
107 |
-
context += ": "
|
108 |
-
context += df.iloc[i, j] # Using iloc to avoid the deprecation warning
|
109 |
-
context += " "
|
110 |
-
context_data.append(context)
|
111 |
-
|
112 |
-
# Lazy initialization function
|
113 |
-
llm = None
|
114 |
-
embed_model = None
|
115 |
-
vectorstore = None
|
116 |
-
groq_key = os.environ.get('groq_api_key')
|
117 |
-
|
118 |
-
def initialize_model():
|
119 |
-
global llm, embed_model, vectorstore
|
120 |
-
|
121 |
-
# Only initialize the models and vector store when needed
|
122 |
-
if llm is None:
|
123 |
-
from langchain_groq import ChatGroq
|
124 |
-
llm = ChatGroq(model="llama-3.1-70b-versatile", api_key=groq_key)
|
125 |
-
|
126 |
-
if embed_model is None:
|
127 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
128 |
-
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
129 |
-
|
130 |
-
if vectorstore is None:
|
131 |
-
from langchain_chroma import Chroma
|
132 |
-
vectorstore = Chroma(
|
133 |
-
collection_name="tourism_dataset_store",
|
134 |
-
embedding_function=embed_model,
|
135 |
-
persist_directory="./",
|
136 |
-
)
|
137 |
-
vectorstore.add_texts(context_data) # Adding the context data to the vector store
|
138 |
-
|
139 |
-
# Initialize model before creating RAG chain
|
140 |
-
initialize_model()
|
141 |
-
|
142 |
-
# RAG Chain setup
|
143 |
from langchain_core.prompts import PromptTemplate
|
144 |
-
from langchain_core.output_parsers import StrOutputParser
|
145 |
-
from langchain_core.runnables import RunnablePassthrough
|
146 |
|
147 |
-
# Define prompt template
|
148 |
template = ("""You are a Moroccan tourism expert.
|
149 |
Use the provided context to answer the question.
|
150 |
If you don't know the answer, say so. Explain your answer in detail.
|
151 |
Do not discuss the context in your response; just provide the answer directly.
|
|
|
152 |
Context: {context}
|
|
|
153 |
Question: {question}
|
|
|
154 |
Answer:""")
|
155 |
|
156 |
rag_prompt = PromptTemplate.from_template(template)
|
157 |
|
158 |
-
|
|
|
|
|
159 |
rag_chain = (
|
160 |
-
{"context":
|
161 |
| rag_prompt
|
162 |
| llm
|
163 |
| StrOutputParser()
|
164 |
)
|
165 |
|
166 |
-
|
|
|
167 |
def rag_memory_stream(text):
|
168 |
partial_text = ""
|
169 |
for new_text in rag_chain.stream(text):
|
170 |
partial_text += new_text
|
171 |
yield partial_text
|
172 |
|
173 |
-
|
174 |
-
examples = ['Tourist attraction sites in Morocco', 'What are some fun activities to do in Morocco?', 'What can I do in Marrakech 40000 Morocco?']
|
175 |
|
176 |
-
title = "Real-time AI App with Groq API and LangChain to Answer
|
177 |
demo = gr.Interface(
|
178 |
title=title,
|
179 |
fn=rag_memory_stream,
|
180 |
inputs="text",
|
181 |
outputs="text",
|
182 |
-
examples=examples,
|
183 |
allow_flagging="never",
|
184 |
)
|
185 |
|
186 |
-
if __name__ ==
|
187 |
-
demo.launch()
|
|
|
1 |
+
# import warnings
|
2 |
+
# warnings.filterwarnings('ignore')
|
3 |
|
4 |
+
import pandas as pd
|
5 |
+
df = pd.read_json("./tourisme_chatbot.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
context_data = []
|
8 |
+
for i in range(len(df)):
|
9 |
+
context = ""
|
10 |
+
for j in range(4):
|
11 |
+
context += df.columns[j]
|
12 |
+
context += ": "
|
13 |
+
context += df.iloc[i][j]
|
14 |
+
context += " "
|
15 |
+
context_data.append(context)
|
16 |
+
|
17 |
+
# Get the secret key from the environment
|
18 |
+
import os
|
19 |
+
groq_api_key = userdata.get('groq_api_key')
|
20 |
|
21 |
+
#LLM Used for RAG
|
22 |
+
from langchain_groq import ChatGroq
|
23 |
|
24 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_api_key)
|
25 |
|
26 |
+
#Embedding model
|
27 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
28 |
+
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# create vector store!
|
31 |
+
from langchain_chroma import Chroma
|
32 |
|
33 |
+
vectorstore = Chroma(
|
34 |
+
collection_name="tourism_dataset_store",
|
35 |
+
embedding_function=embed_model,
|
36 |
+
persist_directory="./",
|
37 |
+
)
|
38 |
|
39 |
+
# Add data to vector store
|
40 |
+
vectorstore.add_texts(context_data)
|
|
|
41 |
|
42 |
+
retriever = vectorstore.as_retriever()
|
|
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
from langchain_core.prompts import PromptTemplate
|
|
|
|
|
45 |
|
|
|
46 |
template = ("""You are a Moroccan tourism expert.
|
47 |
Use the provided context to answer the question.
|
48 |
If you don't know the answer, say so. Explain your answer in detail.
|
49 |
Do not discuss the context in your response; just provide the answer directly.
|
50 |
+
|
51 |
Context: {context}
|
52 |
+
|
53 |
Question: {question}
|
54 |
+
|
55 |
Answer:""")
|
56 |
|
57 |
rag_prompt = PromptTemplate.from_template(template)
|
58 |
|
59 |
+
from langchain_core.output_parsers import StrOutputParser
|
60 |
+
from langchain_core.runnables import RunnablePassthrough
|
61 |
+
|
62 |
rag_chain = (
|
63 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
64 |
| rag_prompt
|
65 |
| llm
|
66 |
| StrOutputParser()
|
67 |
)
|
68 |
|
69 |
+
import gradio as gr
|
70 |
+
|
71 |
def rag_memory_stream(text):
|
72 |
partial_text = ""
|
73 |
for new_text in rag_chain.stream(text):
|
74 |
partial_text += new_text
|
75 |
yield partial_text
|
76 |
|
77 |
+
examples = ['Tourist attraction sites in Morocco', 'What are some fun activities to do in Morocco?']
|
|
|
78 |
|
79 |
+
title = "Real-time AI App with Groq API and LangChain to Answer Moroccon Tourism questions"
|
80 |
demo = gr.Interface(
|
81 |
title=title,
|
82 |
fn=rag_memory_stream,
|
83 |
inputs="text",
|
84 |
outputs="text",
|
|
|
85 |
allow_flagging="never",
|
86 |
)
|
87 |
|
88 |
+
if __name__ == '__main__':
|
89 |
+
demo.launch()
|