Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +31 -32
src/streamlit_app.py
CHANGED
|
@@ -8,37 +8,31 @@ import json
|
|
| 8 |
# =============================================================================
|
| 9 |
# CONFIGURATION - Using Secrets Management
|
| 10 |
# =============================================================================
|
|
|
|
| 11 |
|
|
|
|
| 12 |
def get_api_credentials():
|
| 13 |
-
"""Get API credentials
|
| 14 |
try:
|
| 15 |
# Try Streamlit secrets first (for Hugging Face Spaces)
|
| 16 |
api_token = st.secrets.get("NOCODB_API_TOKEN", os.environ.get("NOCODB_API_TOKEN", ""))
|
| 17 |
together_key = st.secrets.get("TOGETHER_API_KEY", os.environ.get("TOGETHER_API_KEY", ""))
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
st.error("NocoDB URLs not configured properly in secrets.")
|
| 26 |
-
return None, None, None, None, None
|
| 27 |
-
|
| 28 |
-
# Construct full URLs
|
| 29 |
-
content_url = f"{base_url}/{content_table_id}"
|
| 30 |
-
similarity_urls = [f"{base_url}/{table_id}" for table_id in similarity_table_ids if table_id]
|
| 31 |
-
|
| 32 |
-
return api_token, together_key, content_url, similarity_urls
|
| 33 |
-
except Exception as e:
|
| 34 |
-
st.error(f"Error loading credentials: {str(e)}")
|
| 35 |
-
return None, None, None, None
|
| 36 |
|
| 37 |
# Initialize Together AI client
|
| 38 |
@st.cache_resource
|
| 39 |
def get_ai_client():
|
| 40 |
"""Initialize Together AI client"""
|
| 41 |
-
_, together_key, _
|
| 42 |
if not together_key:
|
| 43 |
st.error("Together AI API key not found. Please configure it in the secrets.")
|
| 44 |
return None
|
|
@@ -50,9 +44,9 @@ def get_ai_client():
|
|
| 50 |
@st.cache_data(ttl=300) # Cache for 5 minutes
|
| 51 |
def get_streaming_content():
|
| 52 |
"""Fetch streaming content from NocoDB with pagination"""
|
| 53 |
-
api_token, _,
|
| 54 |
|
| 55 |
-
if not api_token or not
|
| 56 |
st.error("NocoDB credentials not configured. Please set up your secrets.")
|
| 57 |
return []
|
| 58 |
|
|
@@ -68,7 +62,7 @@ def get_streaming_content():
|
|
| 68 |
try:
|
| 69 |
while True:
|
| 70 |
offset = (page - 1) * page_size
|
| 71 |
-
url = f"{
|
| 72 |
|
| 73 |
response = requests.get(url, headers=headers)
|
| 74 |
|
|
@@ -76,9 +70,6 @@ def get_streaming_content():
|
|
| 76 |
data = response.json()
|
| 77 |
current_page_data = data.get('list', [])
|
| 78 |
|
| 79 |
-
# Filter out None values and ensure all items are dictionaries
|
| 80 |
-
current_page_data = [item for item in current_page_data if item and isinstance(item, dict)]
|
| 81 |
-
|
| 82 |
if not current_page_data: # No more data to fetch
|
| 83 |
break
|
| 84 |
|
|
@@ -259,11 +250,11 @@ def extract_unique_names(content_list, field):
|
|
| 259 |
def get_similar_content(content, n_recommendations=5):
|
| 260 |
"""Get pre-computed similar content from database"""
|
| 261 |
try:
|
| 262 |
-
# Get database credentials
|
| 263 |
-
api_token, _,
|
| 264 |
|
| 265 |
-
if not api_token or not
|
| 266 |
-
st.error("NocoDB credentials
|
| 267 |
return []
|
| 268 |
|
| 269 |
headers = {
|
|
@@ -280,15 +271,22 @@ def get_similar_content(content, n_recommendations=5):
|
|
| 280 |
"where": query
|
| 281 |
}
|
| 282 |
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
try:
|
| 285 |
-
|
|
|
|
| 286 |
|
| 287 |
if response.status_code == 200:
|
| 288 |
data = response.json()
|
| 289 |
if data.get('list'):
|
| 290 |
for entry in data['list']:
|
| 291 |
-
# Parse similar items
|
| 292 |
try:
|
| 293 |
similar_items = json.loads(entry['similar_items'])
|
| 294 |
|
|
@@ -300,6 +298,7 @@ def get_similar_content(content, n_recommendations=5):
|
|
| 300 |
content_params = {
|
| 301 |
"where": query
|
| 302 |
}
|
|
|
|
| 303 |
content_response = requests.get(content_url, headers=headers, params=content_params)
|
| 304 |
|
| 305 |
if content_response.status_code == 200:
|
|
@@ -335,7 +334,7 @@ def main():
|
|
| 335 |
st.write("*At your service! Allow me to curate the perfect streaming entertainment for you.*")
|
| 336 |
|
| 337 |
# Check API credentials
|
| 338 |
-
api_token, together_key, _
|
| 339 |
|
| 340 |
if not together_key:
|
| 341 |
st.error("⚠️ Together AI API key not configured!")
|
|
|
|
| 8 |
# =============================================================================
|
| 9 |
# CONFIGURATION - Using Secrets Management
|
| 10 |
# =============================================================================
|
| 11 |
+
NOCODB_URL = "https://mtoft20-potm.hf.space" # Base URL
|
| 12 |
|
| 13 |
+
# Get sensitive data from Streamlit secrets or environment variables
|
| 14 |
def get_api_credentials():
|
| 15 |
+
"""Get API credentials from secrets or environment"""
|
| 16 |
try:
|
| 17 |
# Try Streamlit secrets first (for Hugging Face Spaces)
|
| 18 |
api_token = st.secrets.get("NOCODB_API_TOKEN", os.environ.get("NOCODB_API_TOKEN", ""))
|
| 19 |
together_key = st.secrets.get("TOGETHER_API_KEY", os.environ.get("TOGETHER_API_KEY", ""))
|
| 20 |
+
endpoint_path = st.secrets.get("NOCODB_ENDPOINT_PATH", os.environ.get("NOCODB_ENDPOINT_PATH", ""))
|
| 21 |
|
| 22 |
+
return api_token, together_key, endpoint_path
|
| 23 |
+
except:
|
| 24 |
+
# Fallback to environment variables
|
| 25 |
+
api_token = os.environ.get("NOCODB_API_TOKEN", "")
|
| 26 |
+
together_key = os.environ.get("TOGETHER_API_KEY", "")
|
| 27 |
+
endpoint_path = os.environ.get("NOCODB_ENDPOINT_PATH", "")
|
| 28 |
|
| 29 |
+
return api_token, together_key, endpoint_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Initialize Together AI client
|
| 32 |
@st.cache_resource
|
| 33 |
def get_ai_client():
|
| 34 |
"""Initialize Together AI client"""
|
| 35 |
+
_, together_key, _ = get_api_credentials()
|
| 36 |
if not together_key:
|
| 37 |
st.error("Together AI API key not found. Please configure it in the secrets.")
|
| 38 |
return None
|
|
|
|
| 44 |
@st.cache_data(ttl=300) # Cache for 5 minutes
|
| 45 |
def get_streaming_content():
|
| 46 |
"""Fetch streaming content from NocoDB with pagination"""
|
| 47 |
+
api_token, _, endpoint_path = get_api_credentials()
|
| 48 |
|
| 49 |
+
if not api_token or not endpoint_path:
|
| 50 |
st.error("NocoDB credentials not configured. Please set up your secrets.")
|
| 51 |
return []
|
| 52 |
|
|
|
|
| 62 |
try:
|
| 63 |
while True:
|
| 64 |
offset = (page - 1) * page_size
|
| 65 |
+
url = f"{NOCODB_URL}{endpoint_path}?limit={page_size}&offset={offset}"
|
| 66 |
|
| 67 |
response = requests.get(url, headers=headers)
|
| 68 |
|
|
|
|
| 70 |
data = response.json()
|
| 71 |
current_page_data = data.get('list', [])
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
if not current_page_data: # No more data to fetch
|
| 74 |
break
|
| 75 |
|
|
|
|
| 250 |
def get_similar_content(content, n_recommendations=5):
|
| 251 |
"""Get pre-computed similar content from database"""
|
| 252 |
try:
|
| 253 |
+
# Get database credentials
|
| 254 |
+
api_token, _, endpoint_path = get_api_credentials()
|
| 255 |
|
| 256 |
+
if not api_token or not endpoint_path:
|
| 257 |
+
st.error("NocoDB credentials not configured properly.")
|
| 258 |
return []
|
| 259 |
|
| 260 |
headers = {
|
|
|
|
| 271 |
"where": query
|
| 272 |
}
|
| 273 |
|
| 274 |
+
# Use the same endpoint path but with different table IDs
|
| 275 |
+
similarity_table_paths = [
|
| 276 |
+
endpoint_path.replace("mvtt3arw5ni7uqp", "mp7bnn9tzhojh7k"),
|
| 277 |
+
endpoint_path.replace("mvtt3arw5ni7uqp", "m8e5rglns4acmef"),
|
| 278 |
+
endpoint_path.replace("mvtt3arw5ni7uqp", "m2driodimid10k6")
|
| 279 |
+
]
|
| 280 |
+
|
| 281 |
+
for table_path in similarity_table_paths:
|
| 282 |
try:
|
| 283 |
+
url = f"{NOCODB_URL}{table_path}"
|
| 284 |
+
response = requests.get(url, headers=headers, params=params)
|
| 285 |
|
| 286 |
if response.status_code == 200:
|
| 287 |
data = response.json()
|
| 288 |
if data.get('list'):
|
| 289 |
for entry in data['list']:
|
|
|
|
| 290 |
try:
|
| 291 |
similar_items = json.loads(entry['similar_items'])
|
| 292 |
|
|
|
|
| 298 |
content_params = {
|
| 299 |
"where": query
|
| 300 |
}
|
| 301 |
+
content_url = f"{NOCODB_URL}{endpoint_path}"
|
| 302 |
content_response = requests.get(content_url, headers=headers, params=content_params)
|
| 303 |
|
| 304 |
if content_response.status_code == 200:
|
|
|
|
| 334 |
st.write("*At your service! Allow me to curate the perfect streaming entertainment for you.*")
|
| 335 |
|
| 336 |
# Check API credentials
|
| 337 |
+
api_token, together_key, _ = get_api_credentials()
|
| 338 |
|
| 339 |
if not together_key:
|
| 340 |
st.error("⚠️ Together AI API key not configured!")
|