ZarinT commited on
Commit
c87bf64
·
verified ·
1 Parent(s): 5ed45d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -29
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import io, os
@@ -74,6 +76,10 @@ reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
74
 
75
  vectorstore_global = None
76
 
 
 
 
 
77
  def load_environment():
78
  load_dotenv()
79
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
@@ -239,6 +245,30 @@ def handle_user_query(query):
239
  else:
240
  context = faiss_search_with_keywords(query)
241
  return self_reasoning(query, context)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
  def main():
244
  load_environment()
@@ -262,39 +292,36 @@ def main():
262
 
263
  user_question = st.text_input("Ask your question:", key="user_input")
264
 
265
- if st.button("Submit") and user_question:
266
  set_global_vectorstore(st.session_state.vectorstore)
267
  response = handle_user_query(user_question)
268
- st.session_state.chat_history.append({"user": user_question, "bot": response})
269
-
270
-
271
- if "feedback_log" not in st.session_state:
272
- st.session_state.feedback_log = []
273
-
274
- for i, chat in enumerate(st.session_state.chat_history):
275
- st.write(f"**You:** {chat['user']}")
276
- st.write(f"**Bot:** {chat['bot']}")
277
 
278
- rating = st.radio(
279
- "How would you rate this response?",
280
- options=["1", "2", "3", "4", "5"],
281
- key=f"rating_{i}",
282
- horizontal=True
283
- )
284
 
285
-
286
- st.session_state.feedback_log.append({
287
- "question": chat["user"],
288
- "answer": chat["bot"],
289
- "rating": rating
290
- })
291
-
292
-
293
-
294
- if st.session_state.feedback_log:
295
- feedback_df = pd.DataFrame(st.session_state.feedback_log)
296
- current_date = datetime.now().strftime("%Y%m%d_%H%M%S")
297
- feedback_df.to_csv(f"feedback_{current_date}.csv", index=False)
 
 
 
 
 
 
 
 
 
 
 
298
 
299
  if __name__ == "__main__":
300
  load_environment()
 
1
+ from huggingface_hub import HfApi
2
+ import tempfile
3
  import streamlit as st
4
  import pandas as pd
5
  import io, os
 
76
 
77
  vectorstore_global = None
78
 
79
+ # Initialize feedback list
80
+ if "feedback_log" not in st.session_state:
81
+ st.session_state.feedback_log = []
82
+
83
  def load_environment():
84
  load_dotenv()
85
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
 
245
  else:
246
  context = faiss_search_with_keywords(query)
247
  return self_reasoning(query, context)
248
+
249
+ def save_feedback_to_huggingface():
250
+ if not st.session_state.feedback_log:
251
+ return # No feedback yet
252
+
253
+ feedback_df = pd.DataFrame(st.session_state.feedback_log)
254
+ now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
255
+ filename = f"feedback_{now}.csv"
256
+
257
+ with tempfile.TemporaryDirectory() as tmpdir:
258
+ filepath = os.path.join(tmpdir, filename)
259
+ feedback_df.to_csv(filepath, index=False)
260
+
261
+ api = HfApi(token=os.environ["HF_TOKEN"])
262
+ api.upload_file(
263
+ path_or_fileobj=filepath,
264
+ path_in_repo=filename,
265
+ repo_id="ZarinT/chatbot-feedback",
266
+ repo_type="dataset"
267
+ )
268
+
269
+ # Clear feedback log after upload
270
+ st.session_state.feedback_log.clear()
271
+
272
 
273
  def main():
274
  load_environment()
 
292
 
293
  user_question = st.text_input("Ask your question:", key="user_input")
294
 
295
+ if st.session_state.chat_ready and user_question:
296
  set_global_vectorstore(st.session_state.vectorstore)
297
  response = handle_user_query(user_question)
 
 
 
 
 
 
 
 
 
298
 
299
+ st.session_state.chat_history.append({"user": user_question, "bot": response})
 
 
 
 
 
300
 
301
+ # After bot responds, ask for feedback
302
+ with st.form(key=f"feedback_form_{len(st.session_state.chat_history)}"):
303
+ rating = st.radio(
304
+ "Rate this response:",
305
+ options=["1", "2", "3", "4", "5"],
306
+ key=f"rating_{len(st.session_state.chat_history)}",
307
+ horizontal=True
308
+ )
309
+ submitted = st.form_submit_button("Submit Rating")
310
+ if submitted:
311
+ feedback = {
312
+ "question": user_question,
313
+ "response": response,
314
+ "rating": rating,
315
+ "timestamp": datetime.datetime.now().isoformat()
316
+ }
317
+ st.session_state.feedback_log.append(feedback)
318
+
319
+ # If 5 feedbacks collected, auto upload to Hugging Face
320
+ if len(st.session_state.feedback_log) >= 5:
321
+ save_feedback_to_huggingface()
322
+
323
+ # Clear input box automatically
324
+ st.experimental_rerun()
325
 
326
  if __name__ == "__main__":
327
  load_environment()