jinruiyang
commited on
Commit
Β·
daaa971
1
Parent(s):
6319ca0
Fix: dynamic check for sheets_enabled instead of static
Browse files- backend/api.py +8 -4
- ir/demo.py +10 -7
backend/api.py
CHANGED
|
@@ -28,11 +28,15 @@ import sys
|
|
| 28 |
sys.path.insert(0, str(Path(__file__).parent.parent / "ir"))
|
| 29 |
try:
|
| 30 |
import sheets_storage
|
| 31 |
-
SHEETS_ENABLED = sheets_storage.is_sheets_enabled()
|
| 32 |
except ImportError:
|
| 33 |
-
SHEETS_ENABLED = False
|
| 34 |
sheets_storage = None
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# DeepL API configuration
|
| 37 |
DEEPL_API_KEY = os.environ.get("DEEPL_API_KEY", "")
|
| 38 |
DEEPL_API_URL = "https://api-free.deepl.com/v2/translate" # Use api.deepl.com for paid plans
|
|
@@ -301,7 +305,7 @@ async def api_rating(request: RatingRequest, req: Request):
|
|
| 301 |
|
| 302 |
# Try Google Sheets first (persistent cloud storage)
|
| 303 |
sheets_saved = False
|
| 304 |
-
if
|
| 305 |
try:
|
| 306 |
success = sheets_storage.save_rating_to_sheets(
|
| 307 |
query=request.query,
|
|
@@ -366,7 +370,7 @@ async def api_entity_feedback(request: EntityFeedbackRequest, req: Request):
|
|
| 366 |
|
| 367 |
# Try Google Sheets first (persistent cloud storage)
|
| 368 |
sheets_saved = False
|
| 369 |
-
if
|
| 370 |
try:
|
| 371 |
# Convert ratings dict to string for sheet
|
| 372 |
ratings_str = json.dumps(request.ratings) if request.ratings else ""
|
|
|
|
| 28 |
sys.path.insert(0, str(Path(__file__).parent.parent / "ir"))
|
| 29 |
try:
|
| 30 |
import sheets_storage
|
|
|
|
| 31 |
except ImportError:
|
|
|
|
| 32 |
sheets_storage = None
|
| 33 |
|
| 34 |
+
def is_sheets_enabled():
|
| 35 |
+
"""Check if sheets storage is available and configured (dynamic check)."""
|
| 36 |
+
if sheets_storage is None:
|
| 37 |
+
return False
|
| 38 |
+
return sheets_storage.is_sheets_enabled()
|
| 39 |
+
|
| 40 |
# DeepL API configuration
|
| 41 |
DEEPL_API_KEY = os.environ.get("DEEPL_API_KEY", "")
|
| 42 |
DEEPL_API_URL = "https://api-free.deepl.com/v2/translate" # Use api.deepl.com for paid plans
|
|
|
|
| 305 |
|
| 306 |
# Try Google Sheets first (persistent cloud storage)
|
| 307 |
sheets_saved = False
|
| 308 |
+
if is_sheets_enabled() and sheets_storage:
|
| 309 |
try:
|
| 310 |
success = sheets_storage.save_rating_to_sheets(
|
| 311 |
query=request.query,
|
|
|
|
| 370 |
|
| 371 |
# Try Google Sheets first (persistent cloud storage)
|
| 372 |
sheets_saved = False
|
| 373 |
+
if is_sheets_enabled() and sheets_storage:
|
| 374 |
try:
|
| 375 |
# Convert ratings dict to string for sheet
|
| 376 |
ratings_str = json.dumps(request.ratings) if request.ratings else ""
|
ir/demo.py
CHANGED
|
@@ -22,15 +22,18 @@ import gradio as gr
|
|
| 22 |
# Google Sheets storage for persistent feedback
|
| 23 |
try:
|
| 24 |
from . import sheets_storage
|
| 25 |
-
SHEETS_ENABLED = sheets_storage.is_sheets_enabled()
|
| 26 |
except ImportError:
|
| 27 |
try:
|
| 28 |
import sheets_storage
|
| 29 |
-
SHEETS_ENABLED = sheets_storage.is_sheets_enabled()
|
| 30 |
except ImportError:
|
| 31 |
-
SHEETS_ENABLED = False
|
| 32 |
sheets_storage = None
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# ============================================================
|
| 35 |
# UAE Theme Assets
|
| 36 |
# ============================================================
|
|
@@ -869,7 +872,7 @@ def save_entity_rating(
|
|
| 869 |
emoji = "π" if "relevant" in rating or "helpful" in rating else "π"
|
| 870 |
|
| 871 |
# Try Google Sheets first (persistent)
|
| 872 |
-
if
|
| 873 |
try:
|
| 874 |
success = sheets_storage.save_rating_to_sheets(
|
| 875 |
query=query,
|
|
@@ -905,7 +908,7 @@ def save_entity_rating(
|
|
| 905 |
with open(feedback_file, "w", encoding="utf-8") as f:
|
| 906 |
json.dump(all_feedback, f, ensure_ascii=False, indent=2)
|
| 907 |
|
| 908 |
-
storage_note = " (local only)" if not
|
| 909 |
return f"{emoji} Rated #{rank} {entity_name[:20]}... as {rating}{storage_note}"
|
| 910 |
|
| 911 |
except Exception as e:
|
|
@@ -938,7 +941,7 @@ def save_notes_feedback(
|
|
| 938 |
}
|
| 939 |
|
| 940 |
# Try Google Sheets first (persistent)
|
| 941 |
-
if
|
| 942 |
try:
|
| 943 |
success = sheets_storage.save_notes_to_sheets(
|
| 944 |
query=query,
|
|
@@ -969,7 +972,7 @@ def save_notes_feedback(
|
|
| 969 |
with open(feedback_file, "w", encoding="utf-8") as f:
|
| 970 |
json.dump(all_feedback, f, ensure_ascii=False, indent=2)
|
| 971 |
|
| 972 |
-
storage_note = " (local only)" if not
|
| 973 |
return f"β
Notes saved!{storage_note} Total: {len(all_feedback)} entries"
|
| 974 |
|
| 975 |
except Exception as e:
|
|
|
|
| 22 |
# Google Sheets storage for persistent feedback
|
| 23 |
try:
|
| 24 |
from . import sheets_storage
|
|
|
|
| 25 |
except ImportError:
|
| 26 |
try:
|
| 27 |
import sheets_storage
|
|
|
|
| 28 |
except ImportError:
|
|
|
|
| 29 |
sheets_storage = None
|
| 30 |
|
| 31 |
+
def is_sheets_enabled():
|
| 32 |
+
"""Check if sheets storage is available and configured (dynamic check)."""
|
| 33 |
+
if sheets_storage is None:
|
| 34 |
+
return False
|
| 35 |
+
return sheets_storage.is_sheets_enabled()
|
| 36 |
+
|
| 37 |
# ============================================================
|
| 38 |
# UAE Theme Assets
|
| 39 |
# ============================================================
|
|
|
|
| 872 |
emoji = "π" if "relevant" in rating or "helpful" in rating else "π"
|
| 873 |
|
| 874 |
# Try Google Sheets first (persistent)
|
| 875 |
+
if is_sheets_enabled() and sheets_storage:
|
| 876 |
try:
|
| 877 |
success = sheets_storage.save_rating_to_sheets(
|
| 878 |
query=query,
|
|
|
|
| 908 |
with open(feedback_file, "w", encoding="utf-8") as f:
|
| 909 |
json.dump(all_feedback, f, ensure_ascii=False, indent=2)
|
| 910 |
|
| 911 |
+
storage_note = " (local only)" if not is_sheets_enabled() else ""
|
| 912 |
return f"{emoji} Rated #{rank} {entity_name[:20]}... as {rating}{storage_note}"
|
| 913 |
|
| 914 |
except Exception as e:
|
|
|
|
| 941 |
}
|
| 942 |
|
| 943 |
# Try Google Sheets first (persistent)
|
| 944 |
+
if is_sheets_enabled() and sheets_storage:
|
| 945 |
try:
|
| 946 |
success = sheets_storage.save_notes_to_sheets(
|
| 947 |
query=query,
|
|
|
|
| 972 |
with open(feedback_file, "w", encoding="utf-8") as f:
|
| 973 |
json.dump(all_feedback, f, ensure_ascii=False, indent=2)
|
| 974 |
|
| 975 |
+
storage_note = " (local only)" if not is_sheets_enabled() else ""
|
| 976 |
return f"β
Notes saved!{storage_note} Total: {len(all_feedback)} entries"
|
| 977 |
|
| 978 |
except Exception as e:
|