Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
""
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import pandas as pd
|
2 |
+
|
3 |
+
|
4 |
+
df = pd.read_json("hf://datasets/oussamachaouki/chatbot_tourisme/tourisme_chatbot.json")
|
5 |
+
|
6 |
+
context_data = []
|
7 |
+
for i in range(len(df)):
|
8 |
+
context = ""
|
9 |
+
for j in range(1,4):
|
10 |
+
context += df.columns[j]
|
11 |
+
context += ": "
|
12 |
+
context += df.iloc[i][j]
|
13 |
+
context += " "
|
14 |
+
context_data.append(context)
|
15 |
+
|
16 |
+
|
17 |
+
import os
|
18 |
+
|
19 |
+
# Get the secret key from the environment
|
20 |
+
groq_key = os.environ.get('groq_api_keys')
|
21 |
+
|
22 |
+
## LLM used for RAG
|
23 |
+
from langchain_groq import ChatGroq
|
24 |
+
|
25 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key)
|
26 |
+
|
27 |
+
## Embedding model!
|
28 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
29 |
+
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
30 |
+
|
31 |
+
# create vector store!
|
32 |
+
from langchain_chroma import Chroma
|
33 |
+
|
34 |
+
vectorstore = Chroma(
|
35 |
+
collection_name="tourism_dataset_store",
|
36 |
+
embedding_function=embed_model,
|
37 |
+
persist_directory="./",
|
38 |
+
)
|
39 |
+
|
40 |
+
# add data to vector nstore
|
41 |
+
vectorstore.add_texts(context_data)
|
42 |
+
|
43 |
+
retriever = vectorstore.as_retriever()
|
44 |
+
|
45 |
+
from langchain_core.prompts import PromptTemplate
|
46 |
+
|
47 |
+
template = ("""You are a Moroccan tourism expert.
|
48 |
+
Use the provided context to answer the question.
|
49 |
+
If you don't know the answer, say so. Explain your answer in detail.
|
50 |
+
Do not discuss the context in your response; just provide the answer directly.
|
51 |
+
Context: {context}
|
52 |
+
Question: {question}
|
53 |
+
Answer:""")
|
54 |
+
|
55 |
+
rag_prompt = PromptTemplate.from_template(template)
|
56 |
+
|
57 |
+
from langchain_core.output_parsers import StrOutputParser
|
58 |
+
from langchain_core.runnables import RunnablePassthrough
|
59 |
+
|
60 |
+
rag_chain = (
|
61 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
62 |
+
| rag_prompt
|
63 |
+
| llm
|
64 |
+
| StrOutputParser()
|
65 |
+
)
|
66 |
+
|
67 |
import gradio as gr
|
68 |
+
|
69 |
+
def rag_memory_stream(text):
|
70 |
+
partial_text = ""
|
71 |
+
for new_text in rag_chain.stream(text):
|
72 |
+
partial_text += new_text
|
73 |
+
yield partial_text
|
74 |
+
|
75 |
+
examples = ['Tourist attraction sites in Morocco', 'What are some fun activities to do in Morocco?', 'What can I do in Marrakech 40000 Morocco?']
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
title = "Real-time AI App with Groq API and LangChain to Answer Morroco Tourism questions"
|
81 |
+
demo = gr.Interface(
|
82 |
+
title=title,
|
83 |
+
fn=rag_memory_stream,
|
84 |
+
inputs="text",
|
85 |
+
outputs="text",
|
86 |
+
examples=examples,
|
87 |
+
allow_flagging="never",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
)
|
89 |
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
+
demo.launch()
|