Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,14 +11,16 @@ from huggingface_hub import login
|
|
11 |
# Login to Hugging Face
|
12 |
login(token=st.secrets["HF_TOKEN"])
|
13 |
|
14 |
-
# Load FAISS index
|
15 |
-
db
|
16 |
-
|
17 |
-
|
18 |
-
)
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
search_type="mmr",
|
23 |
search_kwargs={'k': 1}
|
24 |
)
|
@@ -26,8 +28,8 @@ retriever = db.as_retriever(
|
|
26 |
# Define prompt template
|
27 |
prompt_template = """
|
28 |
### [INST]
|
29 |
-
Instruction: You are a Q&A assistant. Your goal is to answer questions as accurately as possible based on the instructions and context provided without using prior knowledge.You answer in FRENCH
|
30 |
-
Analyse carefully the context and provide a direct answer based on the context. If the user said Bonjour or Hello your only answer will be
|
31 |
Answer in french only
|
32 |
|
33 |
{context}
|
@@ -36,26 +38,30 @@ Vous devez répondre aux questions en français.
|
|
36 |
{question}
|
37 |
[/INST]
|
38 |
Answer in french only
|
39 |
-
|
40 |
"""
|
41 |
|
42 |
repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
43 |
|
44 |
-
#
|
45 |
-
mistral_llm
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# Create prompt and LLM chain
|
50 |
prompt = PromptTemplate(
|
51 |
input_variables=["question"],
|
52 |
template=prompt_template,
|
53 |
)
|
54 |
-
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)
|
55 |
|
56 |
# Create QA chain
|
57 |
qa = RetrievalQA.from_chain_type(
|
58 |
-
llm=mistral_llm,
|
59 |
chain_type="stuff",
|
60 |
retriever=retriever,
|
61 |
chain_type_kwargs={"prompt": prompt},
|
@@ -71,14 +77,18 @@ def chatbot_response(user_input):
|
|
71 |
|
72 |
# Define function to save feedback to CSV
|
73 |
def save_feedback(question, response, rating, comment):
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
|
83 |
# Create columns for logos
|
84 |
col1, col2, col3 = st.columns([2, 3, 2])
|
@@ -124,7 +134,6 @@ if submit_button:
|
|
124 |
if st.button("Submit Feedback"):
|
125 |
if comment.strip() != "":
|
126 |
save_feedback(user_input, bot_response, rating, comment)
|
127 |
-
st.success("Thank you for your feedback!")
|
128 |
else:
|
129 |
st.warning("⚠️ Please enter a comment.")
|
130 |
|
|
|
11 |
# Login to Hugging Face
|
12 |
login(token=st.secrets["HF_TOKEN"])
|
13 |
|
14 |
+
# Load FAISS index and ensure it only happens once
|
15 |
+
if 'db' not in st.session_state:
|
16 |
+
st.session_state.db = FAISS.load_local(
|
17 |
+
"faiss_index",
|
18 |
+
HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2'),
|
19 |
+
allow_dangerous_deserialization=True
|
20 |
+
)
|
21 |
+
|
22 |
+
# Use session state for retriever
|
23 |
+
retriever = st.session_state.db.as_retriever(
|
24 |
search_type="mmr",
|
25 |
search_kwargs={'k': 1}
|
26 |
)
|
|
|
28 |
# Define prompt template
|
29 |
prompt_template = """
|
30 |
### [INST]
|
31 |
+
Instruction: You are a Q&A assistant. Your goal is to answer questions as accurately as possible based on the instructions and context provided without using prior knowledge. You answer in FRENCH
|
32 |
+
Analyse carefully the context and provide a direct answer based on the context. If the user said Bonjour or Hello your only answer will be Hi! comment puis-je vous aider?
|
33 |
Answer in french only
|
34 |
|
35 |
{context}
|
|
|
38 |
{question}
|
39 |
[/INST]
|
40 |
Answer in french only
|
41 |
+
Vous devez répondre aux questions en français.
|
42 |
"""
|
43 |
|
44 |
repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
45 |
|
46 |
+
# Load the model only once
|
47 |
+
if 'mistral_llm' not in st.session_state:
|
48 |
+
st.session_state.mistral_llm = HuggingFaceEndpoint(
|
49 |
+
repo_id=repo_id,
|
50 |
+
max_length=2048,
|
51 |
+
temperature=0.05,
|
52 |
+
huggingfacehub_api_token=st.secrets["HF_TOKEN"]
|
53 |
+
)
|
54 |
|
55 |
# Create prompt and LLM chain
|
56 |
prompt = PromptTemplate(
|
57 |
input_variables=["question"],
|
58 |
template=prompt_template,
|
59 |
)
|
60 |
+
llm_chain = LLMChain(llm=st.session_state.mistral_llm, prompt=prompt)
|
61 |
|
62 |
# Create QA chain
|
63 |
qa = RetrievalQA.from_chain_type(
|
64 |
+
llm=st.session_state.mistral_llm,
|
65 |
chain_type="stuff",
|
66 |
retriever=retriever,
|
67 |
chain_type_kwargs={"prompt": prompt},
|
|
|
77 |
|
78 |
# Define function to save feedback to CSV
|
79 |
def save_feedback(question, response, rating, comment):
|
80 |
+
try:
|
81 |
+
filename = '/tmp/feedback.csv' # Use /tmp directory for temporary storage in Spaces
|
82 |
+
file_exists = os.path.isfile(filename)
|
83 |
+
with open(filename, 'a', newline='', encoding='utf-8') as csvfile:
|
84 |
+
fieldnames = ['question', 'response', 'rating', 'comment']
|
85 |
+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
86 |
+
if not file_exists:
|
87 |
+
writer.writeheader()
|
88 |
+
writer.writerow({'question': question, 'response': response, 'rating': rating, 'comment': comment})
|
89 |
+
st.success("Thank you for your feedback! It has been saved.")
|
90 |
+
except Exception as e:
|
91 |
+
st.error(f"Error saving feedback: {e}")
|
92 |
|
93 |
# Create columns for logos
|
94 |
col1, col2, col3 = st.columns([2, 3, 2])
|
|
|
134 |
if st.button("Submit Feedback"):
|
135 |
if comment.strip() != "":
|
136 |
save_feedback(user_input, bot_response, rating, comment)
|
|
|
137 |
else:
|
138 |
st.warning("⚠️ Please enter a comment.")
|
139 |
|