milwright commited on
Commit
69cb0c3
·
1 Parent(s): f92783b

fix preview to match space_template: add dynamic url extraction and api headers

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py CHANGED
@@ -567,6 +567,20 @@ class SpaceGenerator:
567
  except:
568
  pass
569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
  # Build messages for API with grounding context in system prompt
571
  system_content = config.get('system_prompt', 'You are a helpful AI assistant.')
572
  language = config.get('language', 'English')
@@ -603,6 +617,13 @@ class SpaceGenerator:
603
  "stream": False
604
  }
605
 
 
 
 
 
 
 
 
606
  response = requests.post(
607
  "https://openrouter.ai/api/v1/chat/completions",
608
  headers=headers,
 
567
  except:
568
  pass
569
 
570
+ # Check for dynamic URLs in message if enabled
571
+ if config.get('enable_dynamic_urls', True):
572
+ urls_in_message = extract_urls_from_text(message)
573
+ if urls_in_message:
574
+ dynamic_context = "\n📎 **Dynamic Context:**\n"
575
+ for url in urls_in_message[:3]: # Limit to 3 URLs
576
+ try:
577
+ content = fetch_url_content(url, max_chars=2500)
578
+ if not content.startswith("❌"):
579
+ dynamic_context += f"\n{content}"
580
+ except:
581
+ pass
582
+ grounding_context += dynamic_context
583
+
584
  # Build messages for API with grounding context in system prompt
585
  system_content = config.get('system_prompt', 'You are a helpful AI assistant.')
586
  language = config.get('language', 'English')
 
617
  "stream": False
618
  }
619
 
620
+ # Add additional headers to match space template
621
+ space_id = os.environ.get('SPACE_ID', '')
622
+ headers.update({
623
+ "HTTP-Referer": f"https://huggingface.co/spaces/{space_id}" if space_id else "https://huggingface.co",
624
+ "X-Title": config.get('name', 'AI Assistant')
625
+ })
626
+
627
  response = requests.post(
628
  "https://openrouter.ai/api/v1/chat/completions",
629
  headers=headers,