Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,99 @@
|
|
1 |
-
import
|
2 |
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
context_data = []
|
7 |
-
for i in range(len(df)):
|
8 |
-
context = ""
|
9 |
-
for j in range(4):
|
10 |
-
context += df.columns[j]
|
11 |
-
context += ": "
|
12 |
-
context += df.iloc[i][j]
|
13 |
-
context += " "
|
14 |
-
context_data.append(context)
|
15 |
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
# Get the secret key from the environment
|
20 |
-
groq_key = os.environ.get('groq_api_key')
|
21 |
|
22 |
-
|
23 |
-
from langchain_groq import ChatGroq
|
24 |
|
25 |
-
|
|
|
26 |
|
27 |
-
##
|
28 |
-
from
|
29 |
-
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
30 |
|
31 |
-
#
|
32 |
-
from langchain_chroma import Chroma
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
persist_directory="./",
|
38 |
-
)
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
|
|
46 |
|
47 |
-
|
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 |
-
|
56 |
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
{"context": retriever, "question": RunnablePassthrough()}
|
62 |
-
| rag_prompt
|
63 |
-
| llm
|
64 |
-
| StrOutputParser()
|
65 |
-
)
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
def rag_memory_stream(text):
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
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 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
)
|
89 |
|
90 |
|
91 |
-
if __name__ == "__main__":
|
92 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
|
3 |
+
def greet(name):
|
4 |
+
return f"Hello {name}!"
|
5 |
|
6 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
+
iface.launch()
|
8 |
+
# import pandas as pd
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# df = pd.read_json("./tourisme_chatbot.json")
|
12 |
|
13 |
+
# context_data = []
|
14 |
+
# for i in range(len(df)):
|
15 |
+
# context = ""
|
16 |
+
# for j in range(4):
|
17 |
+
# context += df.columns[j]
|
18 |
+
# context += ": "
|
19 |
+
# context += df.iloc[i][j]
|
20 |
+
# context += " "
|
21 |
+
# context_data.append(context)
|
22 |
|
|
|
|
|
23 |
|
24 |
+
# import os
|
|
|
25 |
|
26 |
+
# # Get the secret key from the environment
|
27 |
+
# groq_key = os.environ.get('groq_api_key')
|
28 |
|
29 |
+
# ## LLM used for RAG
|
30 |
+
# from langchain_groq import ChatGroq
|
|
|
31 |
|
32 |
+
# llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key)
|
|
|
33 |
|
34 |
+
# ## Embedding model!
|
35 |
+
# from langchain_huggingface import HuggingFaceEmbeddings
|
36 |
+
# embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
|
|
|
|
37 |
|
38 |
+
# # create vector store!
|
39 |
+
# from langchain_chroma import Chroma
|
40 |
|
41 |
+
# vectorstore = Chroma(
|
42 |
+
# collection_name="tourism_dataset_store",
|
43 |
+
# embedding_function=embed_model,
|
44 |
+
# persist_directory="./",
|
45 |
+
# )
|
46 |
|
47 |
+
# # add data to vector nstore
|
48 |
+
# vectorstore.add_texts(context_data)
|
49 |
|
50 |
+
# retriever = vectorstore.as_retriever()
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# from langchain_core.prompts import PromptTemplate
|
53 |
|
54 |
+
# template = ("""You are a Moroccan tourism expert.
|
55 |
+
# Use the provided context to answer the question.
|
56 |
+
# If you don't know the answer, say so. Explain your answer in detail.
|
57 |
+
# Do not discuss the context in your response; just provide the answer directly.
|
58 |
+
# Context: {context}
|
59 |
+
# Question: {question}
|
60 |
+
# Answer:""")
|
61 |
|
62 |
+
# rag_prompt = PromptTemplate.from_template(template)
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# from langchain_core.output_parsers import StrOutputParser
|
65 |
+
# from langchain_core.runnables import RunnablePassthrough
|
66 |
+
|
67 |
+
# rag_chain = (
|
68 |
+
# {"context": retriever, "question": RunnablePassthrough()}
|
69 |
+
# | rag_prompt
|
70 |
+
# | llm
|
71 |
+
# | StrOutputParser()
|
72 |
+
# )
|
73 |
+
|
74 |
+
# import gradio as gr
|
75 |
|
76 |
+
# def rag_memory_stream(text):
|
77 |
+
# partial_text = ""
|
78 |
+
# for new_text in rag_chain.stream(text):
|
79 |
+
# partial_text += new_text
|
80 |
+
# yield partial_text
|
81 |
|
82 |
+
# examples = ['Tourist attraction sites in Morocco', 'What are some fun activities to do in Morocco?', 'What can I do in Marrakech 40000 Morocco?']
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
+
# title = "Real-time AI App with Groq API and LangChain to Answer Morroco Tourism questions"
|
88 |
+
# demo = gr.Interface(
|
89 |
+
# title=title,
|
90 |
+
# fn=rag_memory_stream,
|
91 |
+
# inputs="text",
|
92 |
+
# outputs="text",
|
93 |
+
# examples=examples,
|
94 |
+
# allow_flagging="never",
|
95 |
+
# )
|
96 |
|
97 |
|
98 |
+
# if __name__ == "__main__":
|
99 |
+
# demo.launch()
|