Utsav2001 commited on
Commit
918f0a4
Β·
verified Β·
1 Parent(s): 31902be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -8
app.py CHANGED
@@ -1,7 +1,29 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  # Streamlit UI
4
- st.title("Feedback-enabled Chatbot")
5
 
6
  if "prompts" not in st.session_state:
7
  st.session_state.prompts = []
@@ -9,12 +31,36 @@ if "responses" not in st.session_state:
9
  st.session_state.responses = []
10
  if "show_feedback" not in st.session_state:
11
  st.session_state.show_feedback = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # User input section
14
  user_input = st.text_input("Enter your message:")
15
  if st.button("Get Response"):
16
- # Generate AI response (replace with your model's logic)
17
- ai_response = f"Response to: {user_input}"
18
  st.session_state.prompts.append(user_input)
19
  st.session_state.responses.append(ai_response)
20
  st.session_state.show_feedback = True
@@ -23,20 +69,27 @@ if st.button("Get Response"):
23
  # Feedback section
24
  if st.session_state.show_feedback and len(st.session_state.prompts) > 0 and len(st.session_state.responses) > 0:
25
  st.write("Provide feedback for the last interaction:")
26
-
27
  col1, col2 = st.columns(2)
 
28
  with col1:
29
  if st.button("πŸ‘ Thumbs Up"):
30
- st.session_state.feedback_value = "Positive"
31
  st.session_state.show_feedback_box = True
32
  with col2:
33
  if st.button("πŸ‘Ž Thumbs Down"):
34
- st.session_state.feedback_value = "Negative"
35
  st.session_state.show_feedback_box = True
36
-
37
  if "show_feedback_box" in st.session_state and st.session_state.show_feedback_box:
38
  feedback_comment = st.text_area("Your Feedback:")
39
  if st.button("Submit Feedback"):
40
- st.success("Feedback submitted successfully!")
 
 
 
 
 
 
41
  st.session_state.show_feedback_box = False
42
  st.session_state.show_feedback = False
 
1
  import streamlit as st
2
+ import json
3
+ from datetime import datetime
4
+ from pathlib import Path
5
+ from uuid import uuid4
6
+ import os
7
+ from huggingface_hub import CommitScheduler
8
+
9
+ # Setup the directory for saving data
10
+ JSON_DATASET_DIR = Path("feedback_dataset")
11
+ JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
12
+
13
+ # Define a unique file name
14
+ JSON_DATASET_PATH = JSON_DATASET_DIR / f"feedback-{uuid4()}.json"
15
+
16
+ # Scheduler configuration for your dataset repo
17
+ scheduler = CommitScheduler(
18
+ repo_id="Utsav2001/Feedback", # Replace with your dataset repo
19
+ repo_type="dataset",
20
+ folder_path=JSON_DATASET_DIR, # Local directory to sync
21
+ path_in_repo="data", # Path in the dataset repository
22
+ token=os.getenv('hf_write') # Ensure your HF write token is set
23
+ )
24
 
25
  # Streamlit UI
26
+ st.title("Feedback-enabled Chatbot with Hugging Face Integration")
27
 
28
  if "prompts" not in st.session_state:
29
  st.session_state.prompts = []
 
31
  st.session_state.responses = []
32
  if "show_feedback" not in st.session_state:
33
  st.session_state.show_feedback = False
34
+ if "feedbacks" not in st.session_state:
35
+ st.session_state.feedbacks = []
36
+
37
+ # Function to save data using Hugging Face CommitScheduler
38
+ def save_feedback_to_hub(prompt, ai_response, feedback_type, feedback_comment):
39
+ try:
40
+ with scheduler.lock:
41
+ with JSON_DATASET_PATH.open("a") as f:
42
+ json.dump(
43
+ {
44
+ "prompt": prompt,
45
+ "ai_response": ai_response,
46
+ "feedback_type": feedback_type,
47
+ "feedback_comment": feedback_comment,
48
+ "datetime": datetime.now().isoformat(),
49
+ },
50
+ f,
51
+ )
52
+ f.write("\n")
53
+ # Push the changes to Hugging Face Hub
54
+ scheduler.push_to_hub()
55
+ return "Feedback saved successfully to Hugging Face Hub!"
56
+ except Exception as e:
57
+ return f"Error saving feedback: {e}"
58
 
59
  # User input section
60
  user_input = st.text_input("Enter your message:")
61
  if st.button("Get Response"):
62
+ # Simulate an AI response (replace with actual AI model logic)
63
+ ai_response = f"This is a response to: {user_input}"
64
  st.session_state.prompts.append(user_input)
65
  st.session_state.responses.append(ai_response)
66
  st.session_state.show_feedback = True
 
69
  # Feedback section
70
  if st.session_state.show_feedback and len(st.session_state.prompts) > 0 and len(st.session_state.responses) > 0:
71
  st.write("Provide feedback for the last interaction:")
72
+
73
  col1, col2 = st.columns(2)
74
+ feedback_type = None
75
  with col1:
76
  if st.button("πŸ‘ Thumbs Up"):
77
+ feedback_type = "Positive"
78
  st.session_state.show_feedback_box = True
79
  with col2:
80
  if st.button("πŸ‘Ž Thumbs Down"):
81
+ feedback_type = "Negative"
82
  st.session_state.show_feedback_box = True
83
+
84
  if "show_feedback_box" in st.session_state and st.session_state.show_feedback_box:
85
  feedback_comment = st.text_area("Your Feedback:")
86
  if st.button("Submit Feedback"):
87
+ save_status = save_feedback_to_hub(
88
+ st.session_state.prompts[-1],
89
+ st.session_state.responses[-1],
90
+ feedback_type,
91
+ feedback_comment
92
+ )
93
+ st.success(save_status)
94
  st.session_state.show_feedback_box = False
95
  st.session_state.show_feedback = False