Update app.py
Browse files
app.py
CHANGED
@@ -141,10 +141,11 @@ def load_qa_history():
|
|
141 |
|
142 |
def save_qa_history(history_entry):
|
143 |
"""
|
144 |
-
Save QA history to Git with improved
|
145 |
"""
|
146 |
try:
|
147 |
-
|
|
|
148 |
history_file = repo_path / "qa_history.json"
|
149 |
|
150 |
# Load existing history
|
@@ -167,38 +168,40 @@ def save_qa_history(history_entry):
|
|
167 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
168 |
|
169 |
# Git operations
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
repo.index.add([str(history_file)])
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
try:
|
179 |
-
origin.push()
|
180 |
-
print(f"Successfully pushed commit {commit.hexsha}")
|
181 |
-
return True
|
182 |
-
except git.GitCommandError as push_error:
|
183 |
-
if "Authentication failed" in str(push_error):
|
184 |
-
print("Authentication failed - please check your Git credentials")
|
185 |
-
else:
|
186 |
-
print(f"Push failed: {push_error}")
|
187 |
-
return False
|
188 |
-
else:
|
189 |
-
print("No changes to commit")
|
190 |
return True
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
195 |
|
196 |
except Exception as e:
|
197 |
print(f"Save operation failed: {str(e)}")
|
198 |
return False
|
199 |
|
200 |
def add_to_qa_history(query: str, answer: str):
|
201 |
-
"""Add new QA pair to history
|
202 |
history_entry = {
|
203 |
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
204 |
"query": query,
|
@@ -206,7 +209,16 @@ def add_to_qa_history(query: str, answer: str):
|
|
206 |
}
|
207 |
|
208 |
# Append to session state history
|
|
|
|
|
209 |
st.session_state.all_history.append(history_entry)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
return history_entry
|
211 |
|
212 |
def fix_repo_path():
|
|
|
141 |
|
142 |
def save_qa_history(history_entry):
|
143 |
"""
|
144 |
+
Save QA history to Git with improved error handling and logging.
|
145 |
"""
|
146 |
try:
|
147 |
+
# Get repository path
|
148 |
+
repo_path = Path(REPO_PATH) # Use the global REPO_PATH instead of env variable
|
149 |
history_file = repo_path / "qa_history.json"
|
150 |
|
151 |
# Load existing history
|
|
|
168 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
169 |
|
170 |
# Git operations
|
171 |
+
repo = Repo(str(repo_path))
|
172 |
+
|
173 |
+
# Check if the file is tracked
|
174 |
+
current_files = repo.git.ls_files().split('\n')
|
175 |
+
if str(history_file.relative_to(repo_path)) not in current_files:
|
176 |
+
repo.index.add([str(history_file)])
|
177 |
+
print(f"Added new file: {history_file}")
|
178 |
+
else:
|
179 |
repo.index.add([str(history_file)])
|
180 |
+
print(f"Updated existing file: {history_file}")
|
181 |
+
|
182 |
+
if repo.is_dirty(untracked_files=True):
|
183 |
+
commit = repo.index.commit(f"Update QA history at {datetime.now().isoformat()}")
|
184 |
+
origin = repo.remote('origin')
|
185 |
|
186 |
+
try:
|
187 |
+
origin.push()
|
188 |
+
print(f"Successfully pushed commit {commit.hexsha}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
return True
|
190 |
+
except git.GitCommandError as push_error:
|
191 |
+
print(f"Push error: {str(push_error)}")
|
192 |
+
if "Authentication failed" in str(push_error):
|
193 |
+
print("Authentication failed - please check your Git credentials")
|
194 |
+
return False
|
195 |
+
else:
|
196 |
+
print("No changes to commit")
|
197 |
+
return True
|
198 |
|
199 |
except Exception as e:
|
200 |
print(f"Save operation failed: {str(e)}")
|
201 |
return False
|
202 |
|
203 |
def add_to_qa_history(query: str, answer: str):
|
204 |
+
"""Add new QA pair to history with immediate save attempt"""
|
205 |
history_entry = {
|
206 |
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
207 |
"query": query,
|
|
|
209 |
}
|
210 |
|
211 |
# Append to session state history
|
212 |
+
if 'all_history' not in st.session_state:
|
213 |
+
st.session_state.all_history = []
|
214 |
st.session_state.all_history.append(history_entry)
|
215 |
+
|
216 |
+
# Immediately try to save if GitHub sync is enabled
|
217 |
+
if st.session_state.github_sync_enabled:
|
218 |
+
success = save_qa_history(history_entry)
|
219 |
+
if not success:
|
220 |
+
print("Failed to save QA history to GitHub")
|
221 |
+
|
222 |
return history_entry
|
223 |
|
224 |
def fix_repo_path():
|