import warnings
warnings.filterwarnings("ignore", message="The 'tuples' format for chatbot messages is deprecated")
# Ensure we're using Gradio 4.x
import gradio as gr
print(f"Gradio version: {gr.__version__}")
import json
import zipfile
import io
import os
from datetime import datetime
from dotenv import load_dotenv
import requests
from bs4 import BeautifulSoup
import tempfile
from pathlib import Path
from support_docs import create_support_docs, export_conversation_to_markdown
# Simple URL content fetching using requests and BeautifulSoup
def get_grounding_context_simple(urls):
"""Fetch grounding context using enhanced HTTP requests"""
if not urls:
return ""
context_parts = []
for i, url in enumerate(urls, 1):
if url and url.strip():
# Use enhanced URL extraction for any URLs within the URL text
extracted_urls = extract_urls_from_text(url.strip())
target_url = extracted_urls[0] if extracted_urls else url.strip()
content = enhanced_fetch_url_content(target_url)
context_parts.append(f"Context from URL {i} ({target_url}):\n{content}")
if context_parts:
return "\n\n" + "\n\n".join(context_parts) + "\n\n"
return ""
# Load environment variables from .env file
load_dotenv()
# Utility functions
import re
def extract_urls_from_text(text):
"""Extract URLs from text using regex with enhanced validation"""
url_pattern = r'https?://[^\s<>"{}|\\^`\[\]"]+'
urls = re.findall(url_pattern, text)
# Basic URL validation and cleanup
validated_urls = []
for url in urls:
# Remove trailing punctuation that might be captured
url = url.rstrip('.,!?;:')
# Basic domain validation
if '.' in url and len(url) > 10:
validated_urls.append(url)
return validated_urls
def validate_url_domain(url):
"""Basic URL domain validation"""
try:
from urllib.parse import urlparse
parsed = urlparse(url)
# Check for valid domain structure
if parsed.netloc and '.' in parsed.netloc:
return True
except:
pass
return False
def enhanced_fetch_url_content(url, enable_search_validation=False):
"""Enhanced URL content fetching with optional search validation"""
if not validate_url_domain(url):
return f"Invalid URL format: {url}"
try:
# Enhanced headers for better compatibility
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive'
}
response = requests.get(url, timeout=15, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# Enhanced content cleaning
for element in soup(["script", "style", "nav", "header", "footer", "aside", "form", "button"]):
element.decompose()
# Extract main content preferentially
main_content = soup.find('main') or soup.find('article') or soup.find('div', class_=lambda x: bool(x and 'content' in x.lower())) or soup
text = main_content.get_text()
# Enhanced text cleaning
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = ' '.join(chunk for chunk in chunks if chunk and len(chunk) > 2)
# Smart truncation - try to end at sentence boundaries
if len(text) > 4000:
truncated = text[:4000]
last_period = truncated.rfind('.')
if last_period > 3000: # If we can find a reasonable sentence break
text = truncated[:last_period + 1]
else:
text = truncated + "..."
return text if text.strip() else "No readable content found at this URL"
except requests.exceptions.Timeout:
return f"Timeout error fetching {url} (15s limit exceeded)"
except requests.exceptions.RequestException as e:
return f"Error fetching {url}: {str(e)}"
except Exception as e:
return f"Error processing content from {url}: {str(e)}"
# Template for generated space app (based on mvp_simple.py)
SPACE_TEMPLATE = '''import gradio as gr
import tempfile
import os
import requests
import json
import re
from bs4 import BeautifulSoup
from datetime import datetime
import urllib.parse
# Configuration
SPACE_NAME = "{name}"
SPACE_DESCRIPTION = "{description}"
SYSTEM_PROMPT = """{system_prompt}"""
MODEL = "{model}"
GROUNDING_URLS = {grounding_urls}
# Get access code from environment variable for security
ACCESS_CODE = os.environ.get("SPACE_ACCESS_CODE", "{access_code}")
ENABLE_DYNAMIC_URLS = {enable_dynamic_urls}
# Get API key from environment - customizable variable name with validation
API_KEY = os.environ.get("{api_key_var}")
if API_KEY:
API_KEY = API_KEY.strip() # Remove any whitespace
if not API_KEY: # Check if empty after stripping
API_KEY = None
# API Key validation and logging
def validate_api_key():
"""Validate API key configuration with detailed logging"""
if not API_KEY:
print(f"⚠️ API KEY CONFIGURATION ERROR:")
print(f" Variable name: {api_key_var}")
print(f" Status: Not set or empty")
print(f" Action needed: Set '{api_key_var}' in HuggingFace Space secrets")
print(f" Expected format: sk-or-xxxxxxxxxx")
return False
elif not API_KEY.startswith('sk-or-'):
print(f"⚠️ API KEY FORMAT WARNING:")
print(f" Variable name: {api_key_var}")
print(f" Current value: {{API_KEY[:10]}}..." if len(API_KEY) > 10 else API_KEY)
print(f" Expected format: sk-or-xxxxxxxxxx")
print(f" Note: OpenRouter keys should start with 'sk-or-'")
return True # Still try to use it
else:
print(f"✅ API Key configured successfully")
print(f" Variable: {api_key_var}")
print(f" Format: Valid OpenRouter key")
return True
# Validate on startup
try:
API_KEY_VALID = validate_api_key()
except NameError:
# During template generation, API_KEY might not be defined yet
API_KEY_VALID = False
def validate_url_domain(url):
"""Basic URL domain validation"""
try:
from urllib.parse import urlparse
parsed = urlparse(url)
# Check for valid domain structure
if parsed.netloc and '.' in parsed.netloc:
return True
except:
pass
return False
def fetch_url_content(url):
"""Enhanced URL content fetching with improved compatibility and error handling"""
if not validate_url_domain(url):
return f"Invalid URL format: {{url}}"
try:
# Enhanced headers for better compatibility
headers = {{
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive'
}}
response = requests.get(url, timeout=15, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# Enhanced content cleaning
for element in soup(["script", "style", "nav", "header", "footer", "aside", "form", "button"]):
element.decompose()
# Extract main content preferentially
main_content = soup.find('main') or soup.find('article') or soup.find('div', class_=lambda x: bool(x and 'content' in x.lower())) or soup
text = main_content.get_text()
# Enhanced text cleaning
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = ' '.join(chunk for chunk in chunks if chunk and len(chunk) > 2)
# Smart truncation - try to end at sentence boundaries
if len(text) > 4000:
truncated = text[:4000]
last_period = truncated.rfind('.')
if last_period > 3000: # If we can find a reasonable sentence break
text = truncated[:last_period + 1]
else:
text = truncated + "..."
return text if text.strip() else "No readable content found at this URL"
except requests.exceptions.Timeout:
return f"Timeout error fetching {{url}} (15s limit exceeded)"
except requests.exceptions.RequestException as e:
return f"Error fetching {{url}}: {{str(e)}}"
except Exception as e:
return f"Error processing content from {{url}}: {{str(e)}}"
def extract_urls_from_text(text):
"""Extract URLs from text using regex with enhanced validation"""
import re
url_pattern = r'https?://[^\\s<>"{{}}|\\\\^`\\[\\]"]+'
urls = re.findall(url_pattern, text)
# Basic URL validation and cleanup
validated_urls = []
for url in urls:
# Remove trailing punctuation that might be captured
url = url.rstrip('.,!?;:')
# Basic domain validation
if '.' in url and len(url) > 10:
validated_urls.append(url)
return validated_urls
# Global cache for URL content to avoid re-crawling in generated spaces
_url_content_cache = {{}}
def get_grounding_context():
"""Fetch context from grounding URLs with caching"""
if not GROUNDING_URLS:
return ""
# Create cache key from URLs
cache_key = tuple(sorted([url for url in GROUNDING_URLS if url and url.strip()]))
# Check cache first
if cache_key in _url_content_cache:
return _url_content_cache[cache_key]
context_parts = []
for i, url in enumerate(GROUNDING_URLS, 1):
if url.strip():
content = fetch_url_content(url.strip())
context_parts.append(f"Context from URL {{i}} ({{url}}):\\n{{content}}")
if context_parts:
result = "\\n\\n" + "\\n\\n".join(context_parts) + "\\n\\n"
else:
result = ""
# Cache the result
_url_content_cache[cache_key] = result
return result
def export_conversation_to_markdown(conversation_history):
"""Export conversation history to markdown format"""
if not conversation_history:
return "No conversation to export."
markdown_content = f"""# Conversation Export
Generated on: {{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}}
---
"""
message_pair_count = 0
for i, message in enumerate(conversation_history):
if isinstance(message, dict):
role = message.get('role', 'unknown')
content = message.get('content', '')
if role == 'user':
message_pair_count += 1
markdown_content += f"## User Message {{message_pair_count}}\\n\\n{{content}}\\n\\n"
elif role == 'assistant':
markdown_content += f"## Assistant Response {{message_pair_count}}\\n\\n{{content}}\\n\\n---\\n\\n"
elif isinstance(message, (list, tuple)) and len(message) >= 2:
# Handle legacy tuple format: ["user msg", "assistant msg"]
message_pair_count += 1
user_msg, assistant_msg = message[0], message[1]
if user_msg:
markdown_content += f"## User Message {{message_pair_count}}\\n\\n{{user_msg}}\\n\\n"
if assistant_msg:
markdown_content += f"## Assistant Response {{message_pair_count}}\\n\\n{{assistant_msg}}\\n\\n---\\n\\n"
return markdown_content
def generate_response(message, history):
"""Generate response using OpenRouter API"""
# Enhanced API key validation with helpful messages
if not API_KEY:
error_msg = f"🔑 **API Key Required**\\n\\n"
error_msg += f"Please configure your OpenRouter API key:\\n"
error_msg += f"1. Go to Settings (⚙️) in your HuggingFace Space\\n"
error_msg += f"2. Click 'Variables and secrets'\\n"
error_msg += f"3. Add secret: **{api_key_var}**\\n"
error_msg += f"4. Value: Your OpenRouter API key (starts with `sk-or-`)\\n\\n"
error_msg += f"Get your API key at: https://openrouter.ai/keys"
print(f"❌ API request failed: No API key configured for {api_key_var}")
return error_msg
# Get grounding context
grounding_context = get_grounding_context()
# If dynamic URLs are enabled, check message for URLs to fetch
if ENABLE_DYNAMIC_URLS:
urls_in_message = extract_urls_from_text(message)
if urls_in_message:
# Fetch content from URLs mentioned in the message
dynamic_context_parts = []
for url in urls_in_message[:3]: # Limit to 3 URLs per message
content = fetch_url_content(url)
dynamic_context_parts.append(f"\\n\\nDynamic context from {{url}}:\\n{{content}}")
if dynamic_context_parts:
grounding_context += "\\n".join(dynamic_context_parts)
# Build enhanced system prompt with grounding context
enhanced_system_prompt = SYSTEM_PROMPT + grounding_context
# Build messages array for the API
messages = [{{"role": "system", "content": enhanced_system_prompt}}]
# Add conversation history - handle both modern messages format and legacy tuples
for chat in history:
if isinstance(chat, dict):
# Modern format: {{"role": "user", "content": "..."}} or {{"role": "assistant", "content": "..."}}
messages.append(chat)
elif isinstance(chat, (list, tuple)) and len(chat) >= 2:
# Legacy format: ["user msg", "assistant msg"] or ("user msg", "assistant msg")
user_msg, assistant_msg = chat[0], chat[1]
if user_msg:
messages.append({{"role": "user", "content": user_msg}})
if assistant_msg:
messages.append({{"role": "assistant", "content": assistant_msg}})
# Add current message
messages.append({{"role": "user", "content": message}})
# Make API request with enhanced error handling
try:
print(f"🔄 Making API request to OpenRouter...")
print(f" Model: {{MODEL}}")
print(f" Messages: {{len(messages)}} in conversation")
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={{
"Authorization": f"Bearer {{API_KEY}}",
"Content-Type": "application/json",
"HTTP-Referer": "https://huggingface.co", # Required by some providers
"X-Title": "HuggingFace Space" # Helpful for tracking
}},
json={{
"model": MODEL,
"messages": messages,
"temperature": {temperature},
"max_tokens": {max_tokens}
}},
timeout=30
)
print(f"📡 API Response: {{response.status_code}}")
if response.status_code == 200:
try:
result = response.json()
# Enhanced validation of API response structure
if 'choices' not in result or not result['choices']:
print(f"⚠️ API response missing choices: {{result}}")
return "API Error: No response choices available"
elif 'message' not in result['choices'][0]:
print(f"⚠️ API response missing message: {{result}}")
return "API Error: No message in response"
elif 'content' not in result['choices'][0]['message']:
print(f"⚠️ API response missing content: {{result}}")
return "API Error: No content in message"
else:
content = result['choices'][0]['message']['content']
# Check for empty content
if not content or content.strip() == "":
print(f"⚠️ API returned empty content")
return "API Error: Empty response content"
print(f"✅ API request successful")
return content
except (KeyError, IndexError, json.JSONDecodeError) as e:
print(f"❌ Failed to parse API response: {{str(e)}}")
return f"API Error: Failed to parse response - {{str(e)}}"
elif response.status_code == 401:
error_msg = f"🔐 **Authentication Error**\\n\\n"
error_msg += f"Your API key appears to be invalid or expired.\\n\\n"
error_msg += f"**Troubleshooting:**\\n"
error_msg += f"1. Check that your **{api_key_var}** secret is set correctly\\n"
error_msg += f"2. Verify your API key at: https://openrouter.ai/keys\\n"
error_msg += f"3. Ensure your key starts with `sk-or-`\\n"
error_msg += f"4. Check that you have credits on your OpenRouter account"
print(f"❌ API authentication failed: {{response.status_code}} - {{response.text[:200]}}")
return error_msg
elif response.status_code == 429:
error_msg = f"⏱️ **Rate Limit Exceeded**\\n\\n"
error_msg += f"Too many requests. Please wait a moment and try again.\\n\\n"
error_msg += f"**Troubleshooting:**\\n"
error_msg += f"1. Wait 30-60 seconds before trying again\\n"
error_msg += f"2. Check your OpenRouter usage limits\\n"
error_msg += f"3. Consider upgrading your OpenRouter plan"
print(f"❌ Rate limit exceeded: {{response.status_code}}")
return error_msg
elif response.status_code == 400:
try:
error_data = response.json()
error_message = error_data.get('error', {{}}).get('message', 'Unknown error')
except:
error_message = response.text
error_msg = f"⚠️ **Request Error**\\n\\n"
error_msg += f"The API request was invalid:\\n"
error_msg += f"`{{error_message}}`\\n\\n"
if "model" in error_message.lower():
error_msg += f"**Model Issue:** The model `{{MODEL}}` may not be available.\\n"
error_msg += f"Try switching to a different model in your Space configuration."
print(f"❌ Bad request: {{response.status_code}} - {{error_message}}")
return error_msg
else:
error_msg = f"🚫 **API Error {{response.status_code}}**\\n\\n"
error_msg += f"An unexpected error occurred. Please try again.\\n\\n"
error_msg += f"If this persists, check:\\n"
error_msg += f"1. OpenRouter service status\\n"
error_msg += f"2. Your API key and credits\\n"
error_msg += f"3. The model availability"
print(f"❌ API error: {{response.status_code}} - {{response.text[:200]}}")
return error_msg
except requests.exceptions.Timeout:
error_msg = f"⏰ **Request Timeout**\\n\\n"
error_msg += f"The API request took too long (30s limit).\\n\\n"
error_msg += f"**Troubleshooting:**\\n"
error_msg += f"1. Try again with a shorter message\\n"
error_msg += f"2. Check your internet connection\\n"
error_msg += f"3. Try a different model"
print(f"❌ Request timeout after 30 seconds")
return error_msg
except requests.exceptions.ConnectionError:
error_msg = f"🌐 **Connection Error**\\n\\n"
error_msg += f"Could not connect to OpenRouter API.\\n\\n"
error_msg += f"**Troubleshooting:**\\n"
error_msg += f"1. Check your internet connection\\n"
error_msg += f"2. Check OpenRouter service status\\n"
error_msg += f"3. Try again in a few moments"
print(f"❌ Connection error to OpenRouter API")
return error_msg
except Exception as e:
error_msg = f"❌ **Unexpected Error**\\n\\n"
error_msg += f"An unexpected error occurred:\\n"
error_msg += f"`{{str(e)}}`\\n\\n"
error_msg += f"Please try again or contact support if this persists."
print(f"❌ Unexpected error: {{str(e)}}")
return error_msg
# Access code verification
access_granted = gr.State(False)
_access_granted_global = False # Global fallback
def verify_access_code(code):
\"\"\"Verify the access code\"\"\"
global _access_granted_global
if not ACCESS_CODE:
_access_granted_global = True
return gr.update(visible=False), gr.update(visible=True), gr.update(value=True)
if code == ACCESS_CODE:
_access_granted_global = True
return gr.update(visible=False), gr.update(visible=True), gr.update(value=True)
else:
_access_granted_global = False
return gr.update(visible=True, value="❌ Incorrect access code. Please try again."), gr.update(visible=False), gr.update(value=False)
def protected_generate_response(message, history):
\"\"\"Protected response function that checks access\"\"\"
# Check if access is granted via the global variable
if ACCESS_CODE and not _access_granted_global:
return "Please enter the access code to continue."
return generate_response(message, history)
# Global variable to store chat history for export
chat_history_store = []
def store_and_generate_response(message, history):
\"\"\"Wrapper function that stores history and generates response\"\"\"
global chat_history_store
# Generate response using the protected function
response = protected_generate_response(message, history)
# Convert current history to the format we need for export
# history comes in as [["user1", "bot1"], ["user2", "bot2"], ...]
chat_history_store = []
if history:
for exchange in history:
if isinstance(exchange, (list, tuple)) and len(exchange) >= 2:
chat_history_store.append({{"role": "user", "content": exchange[0]}})
chat_history_store.append({{"role": "assistant", "content": exchange[1]}})
# Add the current exchange
chat_history_store.append({{"role": "user", "content": message}})
chat_history_store.append({{"role": "assistant", "content": response}})
return response
def export_current_conversation():
\"\"\"Export the current conversation\"\"\"
if not chat_history_store:
return gr.update(visible=False)
markdown_content = export_conversation_to_markdown(chat_history_store)
# Save to temporary file
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False, encoding='utf-8') as f:
f.write(markdown_content)
temp_file = f.name
return gr.update(value=temp_file, visible=True)
def export_conversation(history):
\"\"\"Export conversation to markdown file\"\"\"
if not history:
return gr.update(visible=False)
markdown_content = export_conversation_to_markdown(history)
# Save to temporary file
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False, encoding='utf-8') as f:
f.write(markdown_content)
temp_file = f.name
return gr.update(value=temp_file, visible=True)
# Configuration status display
def get_configuration_status():
\"\"\"Generate a configuration status message for display\"\"\"
status_parts = []
if API_KEY_VALID:
status_parts.append("✅ **API Key:** Configured and valid")
else:
status_parts.append("❌ **API Key:** Not configured - Set `{api_key_var}` in Space secrets")
status_parts.append(f"🤖 **Model:** {{MODEL}}")
status_parts.append(f"🌡️ **Temperature:** {temperature}")
status_parts.append(f"📝 **Max Tokens:** {max_tokens}")
if GROUNDING_URLS:
status_parts.append(f"🔗 **URL Grounding:** {{len(GROUNDING_URLS)}} URLs configured")
if ENABLE_DYNAMIC_URLS:
status_parts.append("🔄 **Dynamic URLs:** Enabled")
if ACCESS_CODE:
status_parts.append("🔐 **Access Control:** Enabled")
else:
status_parts.append("🌐 **Access:** Public")
return "\\n".join(status_parts)
# Create interface with access code protection
with gr.Blocks(title=SPACE_NAME) as demo:
gr.Markdown(f"# {{SPACE_NAME}}")
gr.Markdown(SPACE_DESCRIPTION)
# Configuration status (always visible)
with gr.Accordion("📊 Configuration Status", open=not API_KEY_VALID):
gr.Markdown(get_configuration_status())
# Access code section (shown only if ACCESS_CODE is set)
with gr.Column(visible=bool(ACCESS_CODE)) as access_section:
gr.Markdown("### 🔐 Access Required")
gr.Markdown("Please enter the access code provided by your instructor:")
access_input = gr.Textbox(
label="Access Code",
placeholder="Enter access code...",
type="password"
)
access_btn = gr.Button("Submit", variant="primary")
access_error = gr.Markdown(visible=False)
# Main chat interface (hidden until access granted)
with gr.Column(visible=not bool(ACCESS_CODE)) as chat_section:
chat_interface = gr.ChatInterface(
fn=store_and_generate_response, # Use wrapper function to store history
title="", # Title already shown above
description="", # Description already shown above
examples={examples},
type="messages" # Use modern message format for better compatibility
)
# Export functionality
with gr.Row():
export_btn = gr.Button("📥 Export Conversation", variant="secondary", size="sm")
export_file = gr.File(label="Download Conversation", visible=False)
# Connect export functionality
export_btn.click(
export_current_conversation,
outputs=[export_file]
)
# Connect access verification
if ACCESS_CODE:
access_btn.click(
verify_access_code,
inputs=[access_input],
outputs=[access_error, chat_section, access_granted]
)
access_input.submit(
verify_access_code,
inputs=[access_input],
outputs=[access_error, chat_section, access_granted]
)
if __name__ == "__main__":
demo.launch()
'''
# Available models - Updated with valid OpenRouter model IDs
MODELS = [
"google/gemini-2.0-flash-001", # Fast, reliable, general tasks
"google/gemma-3-27b-it", # High-performance open model
"anthropic/claude-3.5-haiku", # Complex reasoning and analysis
"openai/gpt-4o-mini-search-preview", # Balanced performance and cost with search
"openai/gpt-4.1-nano", # Lightweight OpenAI model
"nvidia/llama-3.1-nemotron-70b-instruct", # Large open-source model
"mistralai/devstral-small" # Coding-focused model
]
def get_grounding_context(urls):
"""Fetch context from grounding URLs"""
if not urls:
return ""
context_parts = []
for i, url in enumerate(urls, 1):
if url and url.strip():
content = enhanced_fetch_url_content(url.strip())
context_parts.append(f"Context from URL {i} ({url}):\n{content}")
if context_parts:
return "\n\n" + "\n\n".join(context_parts) + "\n\n"
return ""
def create_readme(config):
"""Generate README with deployment instructions"""
return f"""---
title: {config['name']}
emoji: 🤖
colorFrom: blue
colorTo: red
sdk: gradio
sdk_version: 5.35.0
app_file: app.py
pinned: false
---
# {config['name']}
{config['description']}
## Quick Deploy to HuggingFace Spaces
### Step 1: Create the Space
1. Go to https://huggingface.co/spaces
2. Click "Create new Space"
3. Choose a name for your Space
4. Select **Gradio** as the SDK
5. Set visibility (Public/Private)
6. Click "Create Space"
### Step 2: Upload Files
1. In your new Space, click "Files" tab
2. Upload these files from the zip:
- `app.py`
- `requirements.txt`
3. Wait for "Building" to complete
### Step 3: Add API Key
1. Go to Settings (gear icon)
2. Click "Variables and secrets"
3. Click "New secret"
4. Name: `{config['api_key_var']}`
5. Value: Your OpenRouter API key
6. Click "Add"
{f'''### Step 4: Configure Access Control
Your Space is configured with access code protection. Students will need to enter the access code to use the chatbot.
1. Go to Settings (gear icon)
2. Click "Variables and secrets"
3. Click "New secret"
4. Name: `SPACE_ACCESS_CODE`
5. Value: `{config['access_code']}`
6. Click "Add"
**Important**: The access code is now stored securely as an environment variable and is not visible in your app code.
To disable access protection:
1. Go to Settings → Variables and secrets
2. Delete the `SPACE_ACCESS_CODE` secret
3. The Space will rebuild automatically with no access protection
''' if config['access_code'] else ''}
### Step {4 if not config['access_code'] else 5}: Get Your API Key
1. Go to https://openrouter.ai/keys
2. Sign up/login if needed
3. Click "Create Key"
4. Copy the key (starts with `sk-or-`)
### Step {5 if not config['access_code'] else 6}: Test Your Space
- Go back to "App" tab
- Your Space should be running!
- Try the example prompts or ask a question
## Configuration
- **Model**: {config['model']}
- **Temperature**: {config['temperature']}
- **Max Tokens**: {config['max_tokens']}
- **API Key Variable**: {config['api_key_var']}"""
# Add optional configuration items
if config['access_code']:
readme_content += f"""
- **Access Code**: {config['access_code']} (Students need this to access the chatbot)"""
if config.get('enable_dynamic_urls'):
readme_content += """
- **Dynamic URL Fetching**: Enabled (Assistant can fetch URLs mentioned in conversations)"""
readme_content += f"""
## Customization
To modify your Space:
1. Edit `app.py` in your Space
2. Update configuration variables at the top
3. Changes deploy automatically
## Troubleshooting
- **"Please set your {config['api_key_var']}"**: Add the secret in Space settings
- **Error 401**: Invalid API key or no credits
- **Error 429**: Rate limit - wait and try again
- **Build failed**: Check requirements.txt formatting
## More Help
- HuggingFace Spaces: https://huggingface.co/docs/hub/spaces
- OpenRouter Docs: https://openrouter.ai/docs
- Gradio Docs: https://gradio.app/docs
---
Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} with Chat U/I Helper
"""
return readme_content
def create_requirements():
"""Generate requirements.txt"""
return "gradio>=4.44.1\nrequests>=2.32.3\nbeautifulsoup4>=4.12.3\npython-dotenv>=1.0.0"
def generate_zip(name, description, system_prompt, model, api_key_var, temperature, max_tokens, examples_text, access_code="", enable_dynamic_urls=False, url1="", url2="", url3="", url4=""):
"""Generate deployable zip file"""
# Process examples
if examples_text and examples_text.strip():
examples_list = [ex.strip() for ex in examples_text.split('\n') if ex.strip()]
examples_python = repr(examples_list) # Convert to Python literal representation
else:
examples_python = repr([
"Hello! How can you help me?",
"Tell me something interesting",
"What can you do?"
])
# Process grounding URLs
grounding_urls = []
for url in [url1, url2, url3, url4]:
if url and url.strip():
grounding_urls.append(url.strip())
# Use the provided system prompt directly
# Create config
config = {
'name': name,
'description': description,
'system_prompt': system_prompt,
'model': model,
'api_key_var': api_key_var,
'temperature': temperature,
'max_tokens': int(max_tokens),
'examples': examples_python,
'grounding_urls': json.dumps(grounding_urls),
'access_code': "", # Access code stored in environment variable for security
'enable_dynamic_urls': enable_dynamic_urls
}
# Generate files
app_content = SPACE_TEMPLATE.format(**config)
# Pass original access_code to README for documentation
readme_config = config.copy()
readme_config['access_code'] = access_code or ""
readme_content = create_readme(readme_config)
requirements_content = create_requirements()
# Create zip file with clean naming
filename = f"{name.lower().replace(' ', '_').replace('-', '_')}.zip"
# Create zip in memory and save to disk
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
zip_file.writestr('app.py', app_content)
zip_file.writestr('requirements.txt', requirements_content)
zip_file.writestr('README.md', readme_content)
zip_file.writestr('config.json', json.dumps(config, indent=2))
# Write zip to file
zip_buffer.seek(0)
with open(filename, 'wb') as f:
f.write(zip_buffer.getvalue())
return filename
# Define callback functions outside the interface
def update_sandbox_preview(config_data):
"""Update the sandbox preview with generated content"""
if not config_data:
return "Generate a space configuration to see preview here.", "
No preview available
"
# Create preview info
preview_text = f"""**Space Configuration:**
- **Name:** {config_data.get('name', 'N/A')}
- **Model:** {config_data.get('model', 'N/A')}
- **Temperature:** {config_data.get('temperature', 'N/A')}
- **Max Tokens:** {config_data.get('max_tokens', 'N/A')}
- **Dynamic URLs:** {'✅ Enabled' if config_data.get('enable_dynamic_urls') else '❌ Disabled'}
**System Prompt Preview:**
```
{config_data.get('system_prompt', 'No system prompt configured')[:500]}{'...' if len(config_data.get('system_prompt', '')) > 500 else ''}
```
**Deployment Package:** `{config_data.get('filename', 'Not generated')}`"""
# Create a basic HTML preview of the chat interface
preview_html = f"""
{config_data.get('name', 'Chat Interface')}
{config_data.get('description', 'A customizable AI chat interface')}
Chat Interface Preview
Assistant: Hello! I'm ready to help you. How can I assist you today?