Update app.py
Browse files
app.py
CHANGED
@@ -122,7 +122,7 @@ def load_qa_history():
|
|
122 |
return []
|
123 |
|
124 |
def save_qa_history(history_entry):
|
125 |
-
"""Save QA history entry to local JSON file and push to
|
126 |
try:
|
127 |
history_file = Path("qa_history.json")
|
128 |
|
@@ -132,6 +132,7 @@ def save_qa_history(history_entry):
|
|
132 |
try:
|
133 |
history_data = json.load(f)
|
134 |
except json.JSONDecodeError:
|
|
|
135 |
history_data = []
|
136 |
else:
|
137 |
history_data = []
|
@@ -142,9 +143,15 @@ def save_qa_history(history_entry):
|
|
142 |
# Save updated history locally
|
143 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
144 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
|
|
|
|
145 |
|
146 |
# Push to GitHub
|
147 |
github_token = os.getenv('GITHUB_TOKEN') or st.secrets['GITHUB_TOKEN']
|
|
|
|
|
|
|
|
|
148 |
from github import Github
|
149 |
g = Github(github_token)
|
150 |
|
@@ -154,22 +161,27 @@ def save_qa_history(history_entry):
|
|
154 |
try:
|
155 |
# Try to get the file first
|
156 |
contents = repo.get_contents("qa_history.json")
|
157 |
-
repo.update_file(
|
158 |
path="qa_history.json",
|
159 |
message="Update QA history",
|
160 |
content=json.dumps(history_data, ensure_ascii=False, indent=2),
|
161 |
sha=contents.sha
|
162 |
)
|
163 |
-
|
|
|
|
|
164 |
# File doesn't exist, create it
|
165 |
-
repo.create_file(
|
166 |
path="qa_history.json",
|
167 |
message="Create QA history",
|
168 |
content=json.dumps(history_data, ensure_ascii=False, indent=2)
|
169 |
)
|
|
|
170 |
|
171 |
except Exception as e:
|
172 |
st.error(f"Error saving QA history: {str(e)}")
|
|
|
|
|
173 |
|
174 |
def add_to_qa_history(query: str, answer: str):
|
175 |
"""Add new QA pair to history"""
|
@@ -185,12 +197,21 @@ def add_to_qa_history(query: str, answer: str):
|
|
185 |
def clear_qa_history():
|
186 |
"""Clear QA history file"""
|
187 |
try:
|
|
|
|
|
|
|
188 |
# Write empty list to history file
|
189 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
190 |
-
json.dump(
|
|
|
|
|
191 |
|
192 |
# Push to GitHub
|
193 |
github_token = os.getenv('GITHUB_TOKEN') or st.secrets['GITHUB_TOKEN']
|
|
|
|
|
|
|
|
|
194 |
from github import Github
|
195 |
g = Github(github_token)
|
196 |
|
@@ -200,21 +221,27 @@ def clear_qa_history():
|
|
200 |
try:
|
201 |
# Try to get the file first
|
202 |
contents = repo.get_contents("qa_history.json")
|
203 |
-
repo.update_file(
|
204 |
path="qa_history.json",
|
205 |
-
message="
|
206 |
content=json.dumps(history_data, ensure_ascii=False, indent=2),
|
207 |
sha=contents.sha
|
208 |
)
|
209 |
-
|
|
|
|
|
210 |
# File doesn't exist, create it
|
211 |
-
repo.create_file(
|
212 |
path="qa_history.json",
|
213 |
-
message="
|
214 |
content=json.dumps(history_data, ensure_ascii=False, indent=2)
|
215 |
)
|
|
|
|
|
216 |
except Exception as e:
|
217 |
st.error(f"Error clearing QA history: {str(e)}")
|
|
|
|
|
218 |
|
219 |
def add_to_history(role: str, message: str):
|
220 |
"""Add message to chat history and save if it's a complete QA pair"""
|
|
|
122 |
return []
|
123 |
|
124 |
def save_qa_history(history_entry):
|
125 |
+
"""Save QA history entry to local JSON file and push to GitHub"""
|
126 |
try:
|
127 |
history_file = Path("qa_history.json")
|
128 |
|
|
|
132 |
try:
|
133 |
history_data = json.load(f)
|
134 |
except json.JSONDecodeError:
|
135 |
+
st.error("Error parsing existing JSON file")
|
136 |
history_data = []
|
137 |
else:
|
138 |
history_data = []
|
|
|
143 |
# Save updated history locally
|
144 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
145 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
146 |
+
|
147 |
+
st.info("Saved locally, attempting GitHub upload...")
|
148 |
|
149 |
# Push to GitHub
|
150 |
github_token = os.getenv('GITHUB_TOKEN') or st.secrets['GITHUB_TOKEN']
|
151 |
+
if not github_token:
|
152 |
+
st.error("GitHub token not found!")
|
153 |
+
return
|
154 |
+
|
155 |
from github import Github
|
156 |
g = Github(github_token)
|
157 |
|
|
|
161 |
try:
|
162 |
# Try to get the file first
|
163 |
contents = repo.get_contents("qa_history.json")
|
164 |
+
response = repo.update_file(
|
165 |
path="qa_history.json",
|
166 |
message="Update QA history",
|
167 |
content=json.dumps(history_data, ensure_ascii=False, indent=2),
|
168 |
sha=contents.sha
|
169 |
)
|
170 |
+
st.success(f"Successfully updated file on GitHub: {response.commit.html_url}")
|
171 |
+
except Exception as e:
|
172 |
+
st.info(f"File doesn't exist yet, creating new one... Error was: {str(e)}")
|
173 |
# File doesn't exist, create it
|
174 |
+
response = repo.create_file(
|
175 |
path="qa_history.json",
|
176 |
message="Create QA history",
|
177 |
content=json.dumps(history_data, ensure_ascii=False, indent=2)
|
178 |
)
|
179 |
+
st.success(f"Successfully created file on GitHub: {response['commit'].html_url}")
|
180 |
|
181 |
except Exception as e:
|
182 |
st.error(f"Error saving QA history: {str(e)}")
|
183 |
+
import traceback
|
184 |
+
st.error(f"Full error: {traceback.format_exc()}")
|
185 |
|
186 |
def add_to_qa_history(query: str, answer: str):
|
187 |
"""Add new QA pair to history"""
|
|
|
197 |
def clear_qa_history():
|
198 |
"""Clear QA history file"""
|
199 |
try:
|
200 |
+
# Create empty list for history data
|
201 |
+
history_data = []
|
202 |
+
|
203 |
# Write empty list to history file
|
204 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
205 |
+
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
206 |
+
|
207 |
+
st.info("Cleared local file, attempting GitHub update...")
|
208 |
|
209 |
# Push to GitHub
|
210 |
github_token = os.getenv('GITHUB_TOKEN') or st.secrets['GITHUB_TOKEN']
|
211 |
+
if not github_token:
|
212 |
+
st.error("GitHub token not found!")
|
213 |
+
return
|
214 |
+
|
215 |
from github import Github
|
216 |
g = Github(github_token)
|
217 |
|
|
|
221 |
try:
|
222 |
# Try to get the file first
|
223 |
contents = repo.get_contents("qa_history.json")
|
224 |
+
response = repo.update_file(
|
225 |
path="qa_history.json",
|
226 |
+
message="Clear QA history",
|
227 |
content=json.dumps(history_data, ensure_ascii=False, indent=2),
|
228 |
sha=contents.sha
|
229 |
)
|
230 |
+
st.success(f"Successfully cleared file on GitHub: {response.commit.html_url}")
|
231 |
+
except Exception as e:
|
232 |
+
st.info(f"File doesn't exist yet, creating empty one... Error was: {str(e)}")
|
233 |
# File doesn't exist, create it
|
234 |
+
response = repo.create_file(
|
235 |
path="qa_history.json",
|
236 |
+
message="Initialize empty QA history",
|
237 |
content=json.dumps(history_data, ensure_ascii=False, indent=2)
|
238 |
)
|
239 |
+
st.success(f"Successfully created empty file on GitHub: {response['commit'].html_url}")
|
240 |
+
|
241 |
except Exception as e:
|
242 |
st.error(f"Error clearing QA history: {str(e)}")
|
243 |
+
import traceback
|
244 |
+
st.error(f"Full error: {traceback.format_exc()}")
|
245 |
|
246 |
def add_to_history(role: str, message: str):
|
247 |
"""Add message to chat history and save if it's a complete QA pair"""
|