Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -861,14 +861,49 @@ def predict(message, chat_history, state):
|
|
| 861 |
)
|
| 862 |
|
| 863 |
|
| 864 |
-
|
| 865 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 866 |
if not PUSH_FEEDBACK:
|
| 867 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
_push_file_to_hub(FEEDBACK_PATH, "analytics/feedback.csv")
|
| 869 |
|
| 870 |
|
| 871 |
|
|
|
|
| 872 |
def save_feedback(rating, comment, state):
|
| 873 |
if rating is None:
|
| 874 |
return "Please select a rating (1–5).", gr.update(visible=True)
|
|
|
|
| 861 |
)
|
| 862 |
|
| 863 |
|
| 864 |
+
# --- Hub upload helper --------------------------------------------------------
|
| 865 |
+
def _push_file_to_hub(local_path: str, repo_path: str) -> None:
|
| 866 |
+
"""
|
| 867 |
+
Upload a local file to your Space repo.
|
| 868 |
+
|
| 869 |
+
Requires:
|
| 870 |
+
- PUSH_FEEDBACK=1
|
| 871 |
+
- HF_WRITE_TOKEN (write access to the Space)
|
| 872 |
+
- SPACE_REPO_ID (e.g., "username/YourSpace")
|
| 873 |
+
"""
|
| 874 |
if not PUSH_FEEDBACK:
|
| 875 |
return
|
| 876 |
+
if not os.path.exists(local_path):
|
| 877 |
+
dlog("UPLOAD", f"Skip: {local_path} does not exist")
|
| 878 |
+
return
|
| 879 |
+
if not SPACE_REPO_ID:
|
| 880 |
+
dlog("UPLOAD", "Skip: SPACE_REPO_ID not set")
|
| 881 |
+
return
|
| 882 |
+
if not HF_WRITE_TOKEN:
|
| 883 |
+
dlog("UPLOAD", "Skip: HF_WRITE_TOKEN not set")
|
| 884 |
+
return
|
| 885 |
+
|
| 886 |
+
try:
|
| 887 |
+
api = HfApi(token=HF_WRITE_TOKEN)
|
| 888 |
+
api.upload_file(
|
| 889 |
+
path_or_fileobj=local_path,
|
| 890 |
+
path_in_repo=repo_path,
|
| 891 |
+
repo_id=SPACE_REPO_ID,
|
| 892 |
+
repo_type="space",
|
| 893 |
+
commit_message=f"Update {repo_path}",
|
| 894 |
+
)
|
| 895 |
+
dlog("UPLOAD", f"Uploaded {repo_path} to Hub")
|
| 896 |
+
except Exception as e:
|
| 897 |
+
dlog("UPLOAD", f"Upload failed: {e}")
|
| 898 |
+
|
| 899 |
+
# --- Feedback uploader --------------------------------------------------------
|
| 900 |
+
def _push_feedback_to_hub() -> None:
|
| 901 |
+
"""Upload feedback.csv to analytics/feedback.csv in this Space repo (if enabled)."""
|
| 902 |
_push_file_to_hub(FEEDBACK_PATH, "analytics/feedback.csv")
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
+
|
| 907 |
def save_feedback(rating, comment, state):
|
| 908 |
if rating is None:
|
| 909 |
return "Please select a rating (1–5).", gr.update(visible=True)
|