add feedback and sheets upload
Browse files- .gitignore +3 -1
- requirements.txt +3 -0
- src/__pycache__/Rag.cpython-313.pyc +0 -0
- src/__pycache__/google_sheets_uploader.cpython-313.pyc +0 -0
- src/app.py +49 -1
- src/google_sheets_uploader.py +47 -0
.gitignore
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
.env
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
# Service account credentials
|
3 |
+
credentials.json
|
requirements.txt
CHANGED
@@ -11,3 +11,6 @@ sentence-transformers>=2.7.0
|
|
11 |
# LLM providers
|
12 |
openai
|
13 |
together>=0.2.8
|
|
|
|
|
|
|
|
11 |
# LLM providers
|
12 |
openai
|
13 |
together>=0.2.8
|
14 |
+
|
15 |
+
gspread
|
16 |
+
oauth2client
|
src/__pycache__/Rag.cpython-313.pyc
CHANGED
Binary files a/src/__pycache__/Rag.cpython-313.pyc and b/src/__pycache__/Rag.cpython-313.pyc differ
|
|
src/__pycache__/google_sheets_uploader.cpython-313.pyc
ADDED
Binary file (1.49 kB). View file
|
|
src/app.py
CHANGED
@@ -5,6 +5,9 @@ from together import Together
|
|
5 |
import time
|
6 |
import os
|
7 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
8 |
|
9 |
load_dotenv()
|
10 |
|
@@ -14,6 +17,10 @@ if "current_embedder_name" not in st.session_state:
|
|
14 |
st.session_state.current_embedder_name = None
|
15 |
if "last_sources" not in st.session_state:
|
16 |
st.session_state.last_sources = []
|
|
|
|
|
|
|
|
|
17 |
|
18 |
st.set_page_config(
|
19 |
page_title="Bipolar Assistant Chatbot",
|
@@ -70,6 +77,18 @@ with st.sidebar:
|
|
70 |
top_p = st.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
|
71 |
max_length = st.slider('max_length', min_value=100, max_value=1000, value=500, step=10)
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
st.markdown("## 💬 Bipolar Assistant Chatbot")
|
74 |
|
75 |
if "messages" not in st.session_state:
|
@@ -95,9 +114,28 @@ with sources_col:
|
|
95 |
st.markdown("*Sources will appear here after you ask a question.*")
|
96 |
|
97 |
with chat_col:
|
98 |
-
for message in st.session_state.messages:
|
99 |
with st.chat_message(message["role"]):
|
100 |
st.markdown(message["content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
if user_input := st.chat_input("Ask me questions about the CANMAT bipolar guideline!"):
|
103 |
st.chat_message("user").markdown(user_input)
|
@@ -134,4 +172,14 @@ with chat_col:
|
|
134 |
|
135 |
st.session_state.messages.append({"role": "assistant", "content": collected})
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
st.rerun()
|
|
|
5 |
import time
|
6 |
import os
|
7 |
from dotenv import load_dotenv
|
8 |
+
from google_sheets_uploader import upload_to_google_sheets
|
9 |
+
import pandas as pd
|
10 |
+
import json
|
11 |
|
12 |
load_dotenv()
|
13 |
|
|
|
17 |
st.session_state.current_embedder_name = None
|
18 |
if "last_sources" not in st.session_state:
|
19 |
st.session_state.last_sources = []
|
20 |
+
if "session_data" not in st.session_state:
|
21 |
+
st.session_state.session_data = []
|
22 |
+
if "uploaded_rows_count" not in st.session_state:
|
23 |
+
st.session_state.uploaded_rows_count = 0
|
24 |
|
25 |
st.set_page_config(
|
26 |
page_title="Bipolar Assistant Chatbot",
|
|
|
77 |
top_p = st.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
|
78 |
max_length = st.slider('max_length', min_value=100, max_value=1000, value=500, step=10)
|
79 |
|
80 |
+
if st.button("Save and Upload to Google Sheets"):
|
81 |
+
try:
|
82 |
+
new_data = st.session_state.session_data[st.session_state.uploaded_rows_count:]
|
83 |
+
if new_data:
|
84 |
+
upload_to_google_sheets(new_data)
|
85 |
+
st.session_state.uploaded_rows_count = len(st.session_state.session_data)
|
86 |
+
st.success("Successfully uploaded to Google Sheets!")
|
87 |
+
else:
|
88 |
+
st.info("No new data to upload.")
|
89 |
+
except Exception as e:
|
90 |
+
st.error(f"An error occurred: {e}")
|
91 |
+
|
92 |
st.markdown("## 💬 Bipolar Assistant Chatbot")
|
93 |
|
94 |
if "messages" not in st.session_state:
|
|
|
114 |
st.markdown("*Sources will appear here after you ask a question.*")
|
115 |
|
116 |
with chat_col:
|
117 |
+
for i, message in enumerate(st.session_state.messages):
|
118 |
with st.chat_message(message["role"]):
|
119 |
st.markdown(message["content"])
|
120 |
+
if message["role"] == "assistant" and i > 0:
|
121 |
+
feedback_options = ["Good", "Bad", "Neutral"]
|
122 |
+
answer_feedback = st.radio(
|
123 |
+
"Rate your answer:",
|
124 |
+
options=feedback_options,
|
125 |
+
index=2, # Default to Neutral
|
126 |
+
key=f"answer_feedback_{i}",
|
127 |
+
horizontal=True,
|
128 |
+
)
|
129 |
+
st.session_state.session_data[i // 2 - 1]["feedback"] = answer_feedback
|
130 |
+
|
131 |
+
source_feedback = st.radio(
|
132 |
+
"Rate your sources:",
|
133 |
+
options=feedback_options,
|
134 |
+
index=2, # Default to Neutral
|
135 |
+
key=f"source_feedback_{i}",
|
136 |
+
horizontal=True,
|
137 |
+
)
|
138 |
+
st.session_state.session_data[i // 2 - 1]["source_feedback"] = source_feedback
|
139 |
|
140 |
if user_input := st.chat_input("Ask me questions about the CANMAT bipolar guideline!"):
|
141 |
st.chat_message("user").markdown(user_input)
|
|
|
172 |
|
173 |
st.session_state.messages.append({"role": "assistant", "content": collected})
|
174 |
|
175 |
+
st.session_state.session_data.append(
|
176 |
+
{
|
177 |
+
"query": user_input,
|
178 |
+
"response": collected,
|
179 |
+
"sources": json.dumps(st.session_state.last_sources, indent=4),
|
180 |
+
"feedback": "Neutral",
|
181 |
+
"source_feedback": "Neutral",
|
182 |
+
}
|
183 |
+
)
|
184 |
+
|
185 |
st.rerun()
|
src/google_sheets_uploader.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gspread
|
3 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
4 |
+
import pandas as pd
|
5 |
+
import os
|
6 |
+
import json
|
7 |
+
|
8 |
+
def get_gspread_client():
|
9 |
+
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
10 |
+
|
11 |
+
google_creds_json = os.environ.get("GOOGLE_CREDENTIALS_JSON")
|
12 |
+
|
13 |
+
if google_creds_json:
|
14 |
+
# Use credentials from Hugging Face secret
|
15 |
+
creds_dict = json.loads(google_creds_json)
|
16 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
17 |
+
else:
|
18 |
+
# Fallback to local credentials file
|
19 |
+
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
20 |
+
|
21 |
+
client = gspread.authorize(creds)
|
22 |
+
return client
|
23 |
+
|
24 |
+
def upload_to_google_sheets(session_data):
|
25 |
+
client = get_gspread_client()
|
26 |
+
sheet = client.open("Bipolar Chatbot Logs").sheet1
|
27 |
+
|
28 |
+
# Convert session_data to a pandas DataFrame
|
29 |
+
df = pd.DataFrame(session_data)
|
30 |
+
|
31 |
+
# Get existing headers from the sheet
|
32 |
+
try:
|
33 |
+
existing_headers = sheet.row_values(1)
|
34 |
+
except gspread.exceptions.APIError:
|
35 |
+
existing_headers = []
|
36 |
+
|
37 |
+
# Define column order
|
38 |
+
columns = ['query', 'response', 'sources', 'feedback', 'source_feedback']
|
39 |
+
df = df[columns]
|
40 |
+
|
41 |
+
# If the sheet is empty, write the headers
|
42 |
+
if not existing_headers:
|
43 |
+
sheet.append_row(columns)
|
44 |
+
|
45 |
+
# Append the DataFrame to the sheet
|
46 |
+
for row in df.itertuples(index=False, name=None):
|
47 |
+
sheet.append_row(list(row))
|