Spaces:
Running
Running
Refactor app.py: Ensure IS_PRO_ACCOUNT is available and improve comments for clarity
Browse files
app.py
CHANGED
|
@@ -31,11 +31,16 @@ from config.settings import (
|
|
| 31 |
HF_TOKEN,
|
| 32 |
MODELS,
|
| 33 |
PRO_ONLY_MODELS,
|
| 34 |
-
IS_PRO_ACCOUNT
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Local imports - source modules
|
| 38 |
-
from src.analytics.chat_evaluator import ChatEvaluator #
|
| 39 |
from src.knowledge_base.dataset import DatasetManager
|
| 40 |
from src.knowledge_base.vector_store import create_vector_store, load_vector_store
|
| 41 |
import config.constants as constants
|
|
@@ -82,15 +87,15 @@ def rebuild_kb_with_selected(sources_df):
|
|
| 82 |
if not selected_urls:
|
| 83 |
return "Error: No URLs selected for inclusion"
|
| 84 |
|
| 85 |
-
#
|
| 86 |
original_urls = constants.URLS.copy()
|
| 87 |
constants.URLS = selected_urls
|
| 88 |
|
| 89 |
try:
|
| 90 |
-
#
|
| 91 |
success, message = create_vector_store(mode="rebuild")
|
| 92 |
|
| 93 |
-
#
|
| 94 |
if success:
|
| 95 |
metadata = {
|
| 96 |
"last_updated": datetime.datetime.now().isoformat(),
|
|
@@ -98,7 +103,7 @@ def rebuild_kb_with_selected(sources_df):
|
|
| 98 |
"sources": selected_urls
|
| 99 |
}
|
| 100 |
|
| 101 |
-
#
|
| 102 |
json_content = json.dumps(metadata, indent=2).encode('utf-8')
|
| 103 |
api = HfApi(token=HF_TOKEN)
|
| 104 |
|
|
@@ -112,7 +117,7 @@ def rebuild_kb_with_selected(sources_df):
|
|
| 112 |
return message
|
| 113 |
|
| 114 |
finally:
|
| 115 |
-
#
|
| 116 |
constants.URLS = original_urls
|
| 117 |
|
| 118 |
except Exception as e:
|
|
@@ -196,11 +201,11 @@ def save_user_preferences(model_key, parameters=None):
|
|
| 196 |
|
| 197 |
preferences["parameters"][model_key] = parameters
|
| 198 |
|
| 199 |
-
#
|
| 200 |
json_content = json.dumps(preferences, indent=2)
|
| 201 |
api = HfApi(token=HF_TOKEN)
|
| 202 |
api.upload_file(
|
| 203 |
-
path_or_fileobj=json_content.encode('utf-8'), #
|
| 204 |
path_in_repo="preferences/user_preferences.json",
|
| 205 |
repo_id=DATASET_ID,
|
| 206 |
repo_type="dataset"
|
|
|
|
| 31 |
HF_TOKEN,
|
| 32 |
MODELS,
|
| 33 |
PRO_ONLY_MODELS,
|
| 34 |
+
IS_PRO_ACCOUNT
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# Ensure IS_PRO_ACCOUNT is available
|
| 38 |
+
if 'IS_PRO_ACCOUNT' not in globals():
|
| 39 |
+
from config.settings import check_account_type
|
| 40 |
+
IS_PRO_ACCOUNT, _ = check_account_type()
|
| 41 |
+
|
| 42 |
# Local imports - source modules
|
| 43 |
+
from src.analytics.chat_evaluator import ChatEvaluator # Fixed import
|
| 44 |
from src.knowledge_base.dataset import DatasetManager
|
| 45 |
from src.knowledge_base.vector_store import create_vector_store, load_vector_store
|
| 46 |
import config.constants as constants
|
|
|
|
| 87 |
if not selected_urls:
|
| 88 |
return "Error: No URLs selected for inclusion"
|
| 89 |
|
| 90 |
+
# Temporarily replace URLS with selected ones
|
| 91 |
original_urls = constants.URLS.copy()
|
| 92 |
constants.URLS = selected_urls
|
| 93 |
|
| 94 |
try:
|
| 95 |
+
# Rebuild knowledge base
|
| 96 |
success, message = create_vector_store(mode="rebuild")
|
| 97 |
|
| 98 |
+
# Save metadata if successful
|
| 99 |
if success:
|
| 100 |
metadata = {
|
| 101 |
"last_updated": datetime.datetime.now().isoformat(),
|
|
|
|
| 103 |
"sources": selected_urls
|
| 104 |
}
|
| 105 |
|
| 106 |
+
# Save to dataset
|
| 107 |
json_content = json.dumps(metadata, indent=2).encode('utf-8')
|
| 108 |
api = HfApi(token=HF_TOKEN)
|
| 109 |
|
|
|
|
| 117 |
return message
|
| 118 |
|
| 119 |
finally:
|
| 120 |
+
# Restore original URLs
|
| 121 |
constants.URLS = original_urls
|
| 122 |
|
| 123 |
except Exception as e:
|
|
|
|
| 201 |
|
| 202 |
preferences["parameters"][model_key] = parameters
|
| 203 |
|
| 204 |
+
# Save to dataset using bytes
|
| 205 |
json_content = json.dumps(preferences, indent=2)
|
| 206 |
api = HfApi(token=HF_TOKEN)
|
| 207 |
api.upload_file(
|
| 208 |
+
path_or_fileobj=json_content.encode('utf-8'), # Convert string to bytes
|
| 209 |
path_in_repo="preferences/user_preferences.json",
|
| 210 |
repo_id=DATASET_ID,
|
| 211 |
repo_type="dataset"
|