mtoft20 commited on
Commit
a2dfcf6
·
verified ·
1 Parent(s): 8943e1c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +20 -20
src/streamlit_app.py CHANGED
@@ -17,16 +17,20 @@ def get_api_credentials():
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
@@ -44,9 +48,9 @@ def get_ai_client():
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,7 +66,7 @@ def get_streaming_content():
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
 
@@ -251,9 +255,9 @@ 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
 
@@ -271,16 +275,12 @@ def get_similar_content(content, n_recommendations=5):
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:
@@ -298,7 +298,7 @@ def get_similar_content(content, n_recommendations=5):
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,7 +334,7 @@ def main():
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!")
 
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
 
21
+ # Get endpoints for content and similarities
22
+ content_endpoint = st.secrets.get("NOCODB_CONTENT_ENDPOINT", os.environ.get("NOCODB_CONTENT_ENDPOINT", ""))
23
+ similarity_endpoints = st.secrets.get("NOCODB_SIMILARITY_ENDPOINTS", os.environ.get("NOCODB_SIMILARITY_ENDPOINTS", "")).split(",")
24
+
25
+ return api_token, together_key, content_endpoint, similarity_endpoints
26
  except:
27
  # Fallback to environment variables
28
  api_token = os.environ.get("NOCODB_API_TOKEN", "")
29
  together_key = os.environ.get("TOGETHER_API_KEY", "")
30
+ content_endpoint = os.environ.get("NOCODB_CONTENT_ENDPOINT", "")
31
+ similarity_endpoints = os.environ.get("NOCODB_SIMILARITY_ENDPOINTS", "").split(",")
32
 
33
+ return api_token, together_key, content_endpoint, similarity_endpoints
34
 
35
  # Initialize Together AI client
36
  @st.cache_resource
 
48
  @st.cache_data(ttl=300) # Cache for 5 minutes
49
  def get_streaming_content():
50
  """Fetch streaming content from NocoDB with pagination"""
51
+ api_token, _, content_endpoint, _ = get_api_credentials()
52
 
53
+ if not api_token or not content_endpoint:
54
  st.error("NocoDB credentials not configured. Please set up your secrets.")
55
  return []
56
 
 
66
  try:
67
  while True:
68
  offset = (page - 1) * page_size
69
+ url = f"{NOCODB_URL}{content_endpoint}?limit={page_size}&offset={offset}"
70
 
71
  response = requests.get(url, headers=headers)
72
 
 
255
  """Get pre-computed similar content from database"""
256
  try:
257
  # Get database credentials
258
+ api_token, _, content_endpoint, similarity_endpoints = get_api_credentials()
259
 
260
+ if not api_token or not similarity_endpoints:
261
  st.error("NocoDB credentials not configured properly.")
262
  return []
263
 
 
275
  "where": query
276
  }
277
 
278
+ for endpoint in similarity_endpoints:
279
+ if not endpoint.strip(): # Skip empty endpoints
280
+ continue
281
+
 
 
 
 
282
  try:
283
+ url = f"{NOCODB_URL}{endpoint}"
284
  response = requests.get(url, headers=headers, params=params)
285
 
286
  if response.status_code == 200:
 
298
  content_params = {
299
  "where": query
300
  }
301
+ content_url = f"{NOCODB_URL}{content_endpoint}"
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!")