Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,99 +1,93 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
-
def greet(name):
|
4 |
-
return f"Hello {name}!"
|
5 |
|
6 |
-
|
7 |
-
iface.launch()
|
8 |
-
# import pandas as pd
|
9 |
|
10 |
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
|
24 |
-
|
25 |
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
import pandas as pd
|
|
|
|
|
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?', 'What can I do in Marrakech 40000 Morocco?']
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
+
title = "Real-time AI App with Groq API and LangChain to Answer Morroco Tourism questions"
|
82 |
+
demo = gr.Interface(
|
83 |
+
title=title,
|
84 |
+
fn=rag_memory_stream,
|
85 |
+
inputs="text",
|
86 |
+
outputs="text",
|
87 |
+
examples=examples,
|
88 |
+
allow_flagging="never",
|
89 |
+
)
|
90 |
|
91 |
|
92 |
+
if __name__ == "__main__":
|
93 |
+
demo.launch()
|