milwright commited on
Commit
3dea807
Β·
verified Β·
1 Parent(s): 1009e6a

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +70 -9
  2. app.py +1231 -0
  3. config.json +26 -0
  4. requirements.txt +4 -0
README.md CHANGED
@@ -1,14 +1,75 @@
1
  ---
2
- title: Chat Adventure Games
3
- emoji: 🌍
4
- colorFrom: yellow
5
- colorTo: blue
6
- sdk: gradio
7
- sdk_version: 5.39.0
8
  app_file: app.py
9
- pinned: false
 
 
10
  license: mit
11
- short_description: Choose your own adventure games
 
 
 
 
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
 
 
 
 
 
 
2
  app_file: app.py
3
+ colorFrom: blue
4
+ colorTo: green
5
+ emoji: πŸ€–
6
  license: mit
7
+ pinned: false
8
+ sdk: gradio
9
+ sdk_version: 5.39.0
10
+ short_description: Choose your own STEM adventure games
11
+ title: STEM Adventure Games
12
  ---
13
 
14
+ # STEM Adventure Games
15
+
16
+ Choose your own STEM adventure games
17
+
18
+ ## Quick Deploy to HuggingFace Spaces
19
+
20
+ ### Step 1: Create the Space
21
+ 1. Go to https://huggingface.co/spaces
22
+ 2. Click "Create new Space"
23
+ 3. Choose a name for your Space
24
+ 4. Select **Gradio** as the SDK
25
+ 5. Set visibility (Public/Private)
26
+ 6. Click "Create Space"
27
+
28
+ ### Step 2: Upload Files
29
+ 1. In your new Space, click "Files" tab
30
+ 2. Upload these files from the zip:
31
+ - `app.py`
32
+ - `requirements.txt`
33
+ - `config.json`
34
+ - `README.md`
35
+ 3. Wait for "Building" to complete
36
+
37
+
38
+ ### Step 3: Add Access Code (Required)
39
+ 1. In Settings, click "Variables and secrets"
40
+ 2. Click "New secret"
41
+ 3. Name: `ACCESS_CODE`
42
+ 4. Value: Your chosen access code/password
43
+ 5. Click "Add"
44
+ 6. **Important**: Share this code with your students!
45
+
46
+ ### Step 4: Add OpenRouter API Key
47
+ 1. Get your API key from https://openrouter.ai/keys
48
+ 2. In your Space Settings, click "Variables and secrets"
49
+ 3. Click "New secret"
50
+ 4. Name: `API_KEY`
51
+ 5. Value: Your OpenRouter API key (starts with `sk-or-`)
52
+ 6. Click "Add"
53
+
54
+ ### Step 5: Test Your Space
55
+ - Go back to "App" tab
56
+ - Your Space should be running!
57
+ - Try the example prompts or ask a question
58
+
59
+ ## Configuration
60
+
61
+ - **Model**: qwen/qwen3-30b-a3b-instruct-2507
62
+ - **API Key Variable**: API_KEY
63
+ - **Access Code Variable**: ACCESS_CODE
64
+
65
+ ## Faculty Configuration
66
+
67
+ If you set `HF_TOKEN` and `SPACE_ID` environment variables, a Configuration tab will appear allowing you to:
68
+ - Edit the system prompt
69
+ - Change the AI model
70
+ - Update example prompts
71
+ - Modify grounding URLs
72
+ - Adjust temperature and response length
73
+ - Lock configuration to prevent further changes
74
+
75
+ Changes save automatically and the Space restarts to apply them.
app.py ADDED
@@ -0,0 +1,1231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tempfile
3
+ import os
4
+ import requests
5
+ import json
6
+ import re
7
+ from bs4 import BeautifulSoup
8
+ from datetime import datetime
9
+ import urllib.parse
10
+
11
+
12
+ # Configuration
13
+ SPACE_NAME = 'STEM Adventure Games'
14
+ SPACE_DESCRIPTION = 'Choose your own STEM adventure games'
15
+
16
+ # Default configuration values (used only if config.json is missing)
17
+ DEFAULT_CONFIG = {
18
+ 'name': SPACE_NAME,
19
+ 'description': SPACE_DESCRIPTION,
20
+ 'system_prompt': "Transform into an interactive co-learning guide who creates Choose Your Own STEM Adventure games featuring historically significant scientific experiments, opening each session with an eye-catching unicode-styled arcade menu presenting 5-6 adventures drawn from Wikipedia's List of Experiments with brief enticing descriptions, then immersing players in vivid historical moments written in second person (e.g. 'You are Galileo Galilei') that establish the year, location, prevailing beliefs, and tensions between established wisdom and emerging observations, presenting 3-4 numbered decision points per stage that reflect authentic scientific choices ranging from experimental design and measurement approaches to strategic decisions about convincing skeptics or timing publications, with each choice meaningfully different and leading to distinct paths forward, then narrating results with sensory details, colleague reactions, and unexpected observations that teach concepts naturally through unfolding drama rather than lectures, always ending scenes with new branching choices that maintain narrative momentum while reinforcing science as an iterative process of hypothesis, testing, and refinement, offering backtracking options to emphasize how so-called failed experiments provide crucial insights, balancing historical accuracy with engaging gameplay that shows how systematic thinking and creative problem-solving combine in scientific breakthroughs.",
21
+ 'temperature': 0.6,
22
+ 'max_tokens': 1000,
23
+ 'model': 'qwen/qwen3-30b-a3b-instruct-2507',
24
+ 'api_key_var': 'API_KEY',
25
+ 'theme': 'Glass',
26
+ 'grounding_urls': ["https://en.wikipedia.org/wiki/List_of_experiments", "https://en.wikipedia.org/wiki/Scientific_method", "https://en.wikipedia.org/wiki/List_of_experiments#Biology", "https://en.wikipedia.org/wiki/List_of_experiments#Astronomy", "https://en.wikipedia.org/wiki/List_of_experiments#Chemistry", "https://en.wikipedia.org/wiki/List_of_experiments#Physics", "https://en.wikipedia.org/wiki/List_of_experiments#Geology"],
27
+ 'enable_dynamic_urls': True,
28
+ 'enable_file_upload': True,
29
+ 'examples': ['Initiate adventure!', 'How do I play?', "What's the meaning of this?"],
30
+ 'locked': False
31
+ }
32
+
33
+ # Load configuration from file - this is the single source of truth
34
+ def load_config():
35
+ """Load configuration from config.json with fallback to defaults"""
36
+ try:
37
+ with open('config.json', 'r') as f:
38
+ config = json.load(f)
39
+ print("βœ… Loaded configuration from config.json")
40
+ return config
41
+ except FileNotFoundError:
42
+ print("ℹ️ No config.json found, using default configuration")
43
+ # Save default config for future use
44
+ try:
45
+ with open('config.json', 'w') as f:
46
+ json.dump(DEFAULT_CONFIG, f, indent=2)
47
+ print("βœ… Created config.json with default values")
48
+ except:
49
+ pass
50
+ return DEFAULT_CONFIG
51
+ except Exception as e:
52
+ print(f"⚠️ Error loading config.json: {e}, using defaults")
53
+ return DEFAULT_CONFIG
54
+
55
+ # Load configuration
56
+ config = load_config()
57
+
58
+ # Initial load of configuration values
59
+ SPACE_NAME = config.get('name', DEFAULT_CONFIG['name'])
60
+ SPACE_DESCRIPTION = config.get('description', DEFAULT_CONFIG['description'])
61
+ SYSTEM_PROMPT = config.get('system_prompt', DEFAULT_CONFIG['system_prompt'])
62
+ temperature = config.get('temperature', DEFAULT_CONFIG['temperature'])
63
+ max_tokens = config.get('max_tokens', DEFAULT_CONFIG['max_tokens'])
64
+ MODEL = config.get('model', DEFAULT_CONFIG['model'])
65
+ THEME = config.get('theme', DEFAULT_CONFIG['theme'])
66
+ GROUNDING_URLS = config.get('grounding_urls', DEFAULT_CONFIG['grounding_urls'])
67
+ ENABLE_DYNAMIC_URLS = config.get('enable_dynamic_urls', DEFAULT_CONFIG['enable_dynamic_urls'])
68
+ ENABLE_FILE_UPLOAD = config.get('enable_file_upload', DEFAULT_CONFIG.get('enable_file_upload', True))
69
+
70
+ # Get access code from environment variable for security
71
+ # If ACCESS_CODE is not set, no access control is applied
72
+ ACCESS_CODE = os.environ.get("ACCESS_CODE")
73
+
74
+ # Get API key from environment - customizable variable name with validation
75
+ API_KEY_VAR = config.get('api_key_var', DEFAULT_CONFIG['api_key_var'])
76
+ API_KEY = os.environ.get(API_KEY_VAR)
77
+ if API_KEY:
78
+ API_KEY = API_KEY.strip() # Remove any whitespace
79
+ if not API_KEY: # Check if empty after stripping
80
+ API_KEY = None
81
+
82
+ # API Key validation and logging
83
+ def validate_api_key():
84
+ """Validate API key configuration with detailed logging"""
85
+ if not API_KEY:
86
+ print(f"⚠️ API KEY CONFIGURATION ERROR:")
87
+ print(f" Variable name: {API_KEY_VAR}")
88
+ print(f" Status: Not set or empty")
89
+ print(f" Action needed: Set '{API_KEY_VAR}' in HuggingFace Space secrets")
90
+ print(f" Expected format: sk-or-xxxxxxxxxx")
91
+ return False
92
+ elif not API_KEY.startswith('sk-or-'):
93
+ print(f"⚠️ API KEY FORMAT WARNING:")
94
+ print(f" Variable name: {API_KEY_VAR}")
95
+ print(f" Current value: {API_KEY[:10]}..." if len(API_KEY) > 10 else "{API_KEY}")
96
+ print(f" Expected format: sk-or-xxxxxxxxxx")
97
+ print(f" Note: OpenRouter keys should start with 'sk-or-'")
98
+ return True # Still try to use it
99
+ else:
100
+ print(f"βœ… API Key configured successfully")
101
+ print(f" Variable: {API_KEY_VAR}")
102
+ print(f" Format: Valid OpenRouter key")
103
+ return True
104
+
105
+ # Validate on startup
106
+ try:
107
+ API_KEY_VALID = validate_api_key()
108
+ except NameError:
109
+ # During template generation, API_KEY might not be defined yet
110
+ API_KEY_VALID = False
111
+
112
+ def validate_url_domain(url):
113
+ """Basic URL domain validation"""
114
+ try:
115
+ from urllib.parse import urlparse
116
+ parsed = urlparse(url)
117
+ # Check for valid domain structure
118
+ if parsed.netloc and '.' in parsed.netloc:
119
+ return True
120
+ except:
121
+ pass
122
+ return False
123
+
124
+ def fetch_url_content(url):
125
+ """Enhanced URL content fetching with improved compatibility and error handling"""
126
+ if not validate_url_domain(url):
127
+ return f"Invalid URL format: {url}"
128
+
129
+ try:
130
+ # Enhanced headers for better compatibility
131
+ headers = {
132
+ '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',
133
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
134
+ 'Accept-Language': 'en-US,en;q=0.5',
135
+ 'Accept-Encoding': 'gzip, deflate',
136
+ 'Connection': 'keep-alive'
137
+ }
138
+
139
+ response = requests.get(url, timeout=15, headers=headers)
140
+ response.raise_for_status()
141
+ soup = BeautifulSoup(response.content, 'html.parser')
142
+
143
+ # Enhanced content cleaning
144
+ for element in soup(["script", "style", "nav", "header", "footer", "aside", "form", "button"]):
145
+ element.decompose()
146
+
147
+ # Extract main content preferentially
148
+ 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
149
+ text = main_content.get_text()
150
+
151
+ # Enhanced text cleaning
152
+ lines = (line.strip() for line in text.splitlines())
153
+ chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
154
+ text = ' '.join(chunk for chunk in chunks if chunk and len(chunk) > 2)
155
+
156
+ # Smart truncation - try to end at sentence boundaries
157
+ if len(text) > 4000:
158
+ truncated_text = text[:4000]
159
+ # Try to find the last complete sentence
160
+ last_period = truncated_text.rfind('.')
161
+ if last_period > 3500: # Only if we have a reasonably long truncation
162
+ text = truncated_text[:last_period + 1]
163
+ else:
164
+ text = truncated_text + "..."
165
+
166
+ return text if text.strip() else "No readable content found at this URL"
167
+
168
+ except requests.exceptions.Timeout:
169
+ return f"Timeout error fetching {url} (15s limit exceeded)"
170
+ except requests.exceptions.RequestException as e:
171
+ return f"Error fetching {url}: {str(e)}"
172
+ except Exception as e:
173
+ return f"Error processing content from {url}: {str(e)}"
174
+
175
+ def extract_urls_from_text(text):
176
+ """Extract URLs from text using regex with enhanced validation"""
177
+ url_pattern = r'https?://[^\s<>"{}|\^`\[\]"]+'
178
+ urls = re.findall(url_pattern, text)
179
+
180
+ # Basic URL validation and cleanup
181
+ validated_urls = []
182
+ for url in urls:
183
+ # Remove trailing punctuation that might be captured
184
+ url = url.rstrip('.,!?;:')
185
+ # Basic domain validation
186
+ if '.' in url and len(url) > 10:
187
+ validated_urls.append(url)
188
+
189
+ return validated_urls
190
+
191
+ # Global cache for URL content to avoid re-crawling in generated spaces
192
+ _url_content_cache = {}
193
+
194
+ def extract_file_content(file_path, max_chars=8000):
195
+ """Extract content from uploaded files with intelligent chunking for optimal context usage"""
196
+ try:
197
+ # Get file metadata
198
+ file_size = os.path.getsize(file_path)
199
+ file_name = os.path.basename(file_path)
200
+ _, ext = os.path.splitext(file_path.lower())
201
+
202
+ # Comprehensive list of text file extensions
203
+ text_extensions = [
204
+ '.txt', '.md', '.markdown', '.rst', # Documentation
205
+ '.py', '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', # JavaScript/Python
206
+ '.java', '.c', '.cpp', '.cc', '.h', '.hpp', '.cs', # C-family
207
+ '.go', '.rs', '.swift', '.kt', '.scala', '.r', '.m', # Other languages
208
+ '.html', '.htm', '.xml', '.xhtml', '.css', '.scss', '.sass', '.less', # Web
209
+ '.json', '.yaml', '.yml', '.toml', '.ini', '.conf', '.config', # Config
210
+ '.csv', '.tsv', '.log', '.sql', # Data/Logs
211
+ '.sh', '.bash', '.zsh', '.fish', '.ps1', '.bat', '.cmd', # Scripts
212
+ '.tex', '.latex', '.bib', # LaTeX
213
+ '.vue', '.svelte', '.astro', # Modern frameworks
214
+ '.env', '.gitignore', '.dockerignore', '.editorconfig' # Dotfiles
215
+ ]
216
+
217
+ # Handle text files with intelligent extraction
218
+ if ext in text_extensions:
219
+ with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
220
+ content = f.read()
221
+ total_chars = len(content)
222
+
223
+ # If content fits within limit, return all
224
+ if total_chars <= max_chars:
225
+ return f"πŸ“„ **{file_name}** ({ext}, {file_size:,} bytes)\n```{ext[1:] if ext else ''}\n{content}\n```"
226
+
227
+ # For larger files, extract intelligently
228
+ # Calculate line information
229
+ lines = content.split('\n')
230
+ total_lines = len(lines)
231
+
232
+ # Smart extraction: beginning + end with overlap consideration
233
+ if total_lines > 100: # For files with many lines
234
+ # Take more from beginning (usually imports/setup) and substantial end
235
+ begin_chars = int(max_chars * 0.6)
236
+ end_chars = int(max_chars * 0.4)
237
+
238
+ # Extract beginning
239
+ begin_content = []
240
+ char_count = 0
241
+ for line in lines:
242
+ if char_count + len(line) + 1 > begin_chars:
243
+ break
244
+ begin_content.append(line)
245
+ char_count += len(line) + 1
246
+
247
+ # Extract end
248
+ end_content = []
249
+ char_count = 0
250
+ for line in reversed(lines):
251
+ if char_count + len(line) + 1 > end_chars:
252
+ break
253
+ end_content.insert(0, line)
254
+ char_count += len(line) + 1
255
+
256
+ omitted_lines = total_lines - len(begin_content) - len(end_content)
257
+
258
+ begin_text = '\n'.join(begin_content)
259
+ end_text = '\n'.join(end_content)
260
+ return f"πŸ“„ **{file_name}** ({ext}, {file_size:,} bytes, {total_lines:,} lines)\n\n**Beginning of file:**\n```{ext[1:] if ext else ''}\n{begin_text}\n```\n\n*... [{omitted_lines:,} lines omitted] ...*\n\n**End of file:**\n```{ext[1:] if ext else ''}\n{end_text}\n```"
261
+ else:
262
+ # For smaller files, use character-based extraction
263
+ chunk_size = max_chars // 2
264
+ beginning = content[:chunk_size]
265
+ ending = content[-chunk_size:]
266
+
267
+ # Clean breaks at newlines if possible
268
+ if '\n' in beginning:
269
+ beginning = beginning[:beginning.rfind('\n')]
270
+ if '\n' in ending:
271
+ ending = ending[ending.find('\n')+1:]
272
+
273
+ omitted_chars = total_chars - len(beginning) - len(ending)
274
+
275
+ return f"πŸ“„ **{file_name}** ({ext}, {file_size:,} bytes)\n```{ext[1:] if ext else ''}\n{beginning}\n\n... [{omitted_chars:,} characters omitted] ...\n\n{ending}\n```"
276
+
277
+ # Handle other file types with simple messages
278
+ elif ext == '.pdf':
279
+ return f"πŸ“‘ **{file_name}** (PDF, {file_size:,} bytes)\n⚠️ PDF support requires PyPDF2"
280
+ elif ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg', '.ico']:
281
+ return f"πŸ–ΌοΈ **{file_name}** ({ext}, {file_size:,} bytes)\nπŸ’‘ Image file uploaded"
282
+ elif ext in ['.xlsx', '.xls']:
283
+ return f"πŸ“Š **{file_name}** (Excel, {file_size:,} bytes)\nπŸ’‘ Consider exporting as CSV"
284
+ elif ext == '.docx':
285
+ return f"πŸ“ **{file_name}** (Word document, {file_size:,} bytes)"
286
+ elif ext in ['.zip', '.tar', '.gz', '.7z', '.rar']:
287
+ return f"πŸ—œοΈ **{file_name}** (Archive, {file_size:,} bytes)"
288
+ else:
289
+ return f"πŸ“Ž **{file_name}** ({ext if ext else 'no extension'}, {file_size:,} bytes)"
290
+
291
+ except Exception as e:
292
+ return f"❌ Error reading file {file_name}: {str(e)}"
293
+
294
+ def get_grounding_context():
295
+ """Fetch context from grounding URLs with caching"""
296
+ # Handle both string and list formats for grounding_urls
297
+ urls = GROUNDING_URLS
298
+ if isinstance(urls, str):
299
+ try:
300
+ urls = json.loads(urls)
301
+ except:
302
+ urls = []
303
+
304
+ if not urls:
305
+ return ""
306
+
307
+ # Create cache key from URLs
308
+ cache_key = tuple(sorted([url for url in urls if url and url.strip()]))
309
+
310
+ # Check cache first
311
+ if cache_key in _url_content_cache:
312
+ return _url_content_cache[cache_key]
313
+
314
+ context_parts = []
315
+ for i, url in enumerate(urls, 1):
316
+ if url.strip():
317
+ content = fetch_url_content(url.strip())
318
+ # Add priority indicators
319
+ priority_label = "PRIMARY" if i <= 2 else "SECONDARY"
320
+ context_parts.append(f"[{priority_label}] Context from URL {i} ({url}):\n{content}")
321
+
322
+ if context_parts:
323
+ result = "\n\n" + "\n\n".join(context_parts) + "\n\n"
324
+ else:
325
+ result = ""
326
+
327
+ # Cache the result
328
+ _url_content_cache[cache_key] = result
329
+ return result
330
+
331
+ def export_conversation_to_markdown(conversation_history):
332
+ """Export conversation history to markdown format"""
333
+ if not conversation_history:
334
+ return "No conversation to export."
335
+
336
+ markdown_content = f"""# Conversation Export
337
+ Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
338
+
339
+ ---
340
+
341
+ """
342
+
343
+ message_pair_count = 0
344
+ for i, message in enumerate(conversation_history):
345
+ if isinstance(message, dict):
346
+ role = message.get('role', 'unknown')
347
+ content = message.get('content', '')
348
+
349
+ if role == 'user':
350
+ message_pair_count += 1
351
+ markdown_content += f"## User Message {message_pair_count}\n\n{content}\n\n"
352
+ elif role == 'assistant':
353
+ markdown_content += f"## Assistant Response {message_pair_count}\n\n{content}\n\n---\n\n"
354
+ elif isinstance(message, (list, tuple)) and len(message) >= 2:
355
+ # Handle legacy tuple format: ["user msg", "assistant msg"]
356
+ message_pair_count += 1
357
+ user_msg, assistant_msg = message[0], message[1]
358
+ if user_msg:
359
+ markdown_content += f"## User Message {message_pair_count}\n\n{user_msg}\n\n"
360
+ if assistant_msg:
361
+ markdown_content += f"## Assistant Response {message_pair_count}\n\n{assistant_msg}\n\n---\n\n"
362
+
363
+ return markdown_content
364
+
365
+
366
+ def generate_response(message, history):
367
+ """Generate response using OpenRouter API with multimodal support"""
368
+
369
+ # Enhanced API key validation with helpful messages
370
+ if not API_KEY:
371
+ error_msg = f"πŸ”‘ **API Key Required**\n\n"
372
+ error_msg += f"Please configure your OpenRouter API key:\n"
373
+ error_msg += f"1. Go to Settings (βš™οΈ) in your HuggingFace Space\n"
374
+ error_msg += f"2. Click 'Variables and secrets'\n"
375
+ error_msg += f"3. Add secret: **{API_KEY_VAR}**\n"
376
+ error_msg += f"4. Value: Your OpenRouter API key (starts with `sk-or-`)\n\n"
377
+ error_msg += f"Get your API key at: https://openrouter.ai/keys"
378
+ print(f"❌ API request failed: No API key configured for {API_KEY_VAR}")
379
+ return error_msg
380
+
381
+ # Get grounding context
382
+ grounding_context = get_grounding_context()
383
+
384
+ # Process multimodal message
385
+ file_context = ""
386
+ file_notification = ""
387
+ text_message = message
388
+
389
+ # Check if message is multimodal (dict with 'text' and 'files')
390
+ if isinstance(message, dict):
391
+ text_message = message.get('text', '')
392
+ files = message.get('files', [])
393
+
394
+ if files:
395
+ file_contents = []
396
+ file_names = []
397
+ for file_path in files:
398
+ if file_path:
399
+ try:
400
+ file_content = extract_file_content(file_path)
401
+ file_contents.append(file_content)
402
+ file_names.append(os.path.basename(file_path))
403
+ print(f"πŸ“„ Processing file: {os.path.basename(file_path)}")
404
+ except Exception as e:
405
+ error_msg = f"Error processing file: {str(e)}"
406
+ file_contents.append(error_msg)
407
+ file_names.append("error")
408
+ print(f"❌ {error_msg}")
409
+
410
+ if file_contents:
411
+ file_context = "\n\n[UPLOADED FILES]\n" + "\n\n".join(file_contents) + "\n"
412
+ # Add file notification to user message
413
+ file_list = ", ".join(file_names)
414
+ file_notification = f"\n\n[Note: The user has uploaded the following file(s): {file_list}. The content has been extracted and is available in context.]"
415
+ print(f"βœ… Successfully processed {len(file_names)} file(s): {file_list}")
416
+
417
+ # If dynamic URLs are enabled, check message for URLs to fetch
418
+ if ENABLE_DYNAMIC_URLS:
419
+ urls_in_message = extract_urls_from_text(text_message)
420
+ if urls_in_message:
421
+ dynamic_context = ""
422
+ for url in urls_in_message[:3]: # Limit to 3 URLs per message
423
+ content = fetch_url_content(url)
424
+ dynamic_context += f"\n\n[DYNAMIC] Context from {url}:\n{content}"
425
+ grounding_context += dynamic_context
426
+
427
+ # Build enhanced system prompt with grounding context and file content
428
+ enhanced_system_prompt = SYSTEM_PROMPT + grounding_context + file_context
429
+
430
+ # Build messages array for the API
431
+ messages = [{"role": "system", "content": enhanced_system_prompt}]
432
+
433
+ # Add conversation history - handle both modern messages format and legacy tuples
434
+ for chat in history:
435
+ if isinstance(chat, dict):
436
+ messages.append(chat)
437
+ elif isinstance(chat, (list, tuple)) and len(chat) >= 2:
438
+ messages.append({"role": "user", "content": chat[0]})
439
+ messages.append({"role": "assistant", "content": chat[1]})
440
+
441
+ # Add current message with file notification if applicable
442
+ user_message = text_message
443
+ if file_notification:
444
+ user_message = text_message + file_notification
445
+ messages.append({"role": "user", "content": user_message})
446
+
447
+ # Make API request with enhanced error handling
448
+ try:
449
+ print(f"πŸ”„ Making API request to OpenRouter...")
450
+ print(f" Model: {MODEL}")
451
+ print(f" Messages: {len(messages)} in conversation")
452
+ if files:
453
+ print(f" Files uploaded: {len([f for f in files if f is not None])}")
454
+
455
+ response = requests.post(
456
+ url="https://openrouter.ai/api/v1/chat/completions",
457
+ headers={
458
+ "Authorization": f"Bearer {API_KEY}",
459
+ "Content-Type": "application/json",
460
+ "HTTP-Referer": "https://huggingface.co",
461
+ "X-Title": "HuggingFace Space"
462
+ },
463
+ json={
464
+ "model": MODEL,
465
+ "messages": messages,
466
+ "temperature": temperature,
467
+ "max_tokens": max_tokens
468
+ },
469
+ timeout=30
470
+ )
471
+
472
+ print(f"πŸ“‘ API Response: {response.status_code}")
473
+
474
+ if response.status_code == 200:
475
+ try:
476
+ result = response.json()
477
+ return result['choices'][0]['message']['content']
478
+ except (KeyError, IndexError, json.JSONDecodeError) as e:
479
+ error_msg = f"❌ **Response Parsing Error**\n\n"
480
+ error_msg += f"Received response from API but couldn't parse it properly.\n"
481
+ error_msg += f"Error: {str(e)}\n\n"
482
+ error_msg += f"**Troubleshooting:**\n"
483
+ error_msg += f"1. Check OpenRouter service status\n"
484
+ error_msg += f"2. Try again in a few moments\n"
485
+ error_msg += f"3. Try a different model if available"
486
+ print(f"❌ Response parsing error: {str(e)}")
487
+ return error_msg
488
+ elif response.status_code == 401:
489
+ error_msg = f"πŸ”‘ **Authentication Error**\n\n"
490
+ error_msg += f"Your API key appears to be invalid or expired.\n\n"
491
+ error_msg += f"**Troubleshooting:**\n"
492
+ error_msg += f"1. Check that your **{API_KEY_VAR}** secret is set correctly\n"
493
+ error_msg += f"2. Verify your OpenRouter API key at https://openrouter.ai/keys\n"
494
+ error_msg += f"3. Make sure the key starts with `sk-or-`\n"
495
+ error_msg += f"4. Check if you have sufficient credits"
496
+ print(f"❌ Authentication failed: Invalid API key")
497
+ return error_msg
498
+ elif response.status_code == 429:
499
+ error_msg = f"⏱️ **Rate Limit Exceeded**\n\n"
500
+ error_msg += f"Too many requests. Please wait a moment and try again.\n\n"
501
+ error_msg += f"**Troubleshooting:**\n"
502
+ error_msg += f"1. Wait 30-60 seconds before trying again\n"
503
+ error_msg += f"2. Check your OpenRouter usage limits\n"
504
+ print(f"❌ Rate limit exceeded")
505
+ return error_msg
506
+ elif response.status_code == 400:
507
+ error_msg = f"πŸ“ **Request Error**\n\n"
508
+ error_msg += f"There was a problem with the request format.\n"
509
+ error_msg += f"Response: {response.text[:500]}\n\n"
510
+ error_msg += f"**Troubleshooting:**\n"
511
+ error_msg += f"1. Try a shorter message\n"
512
+ error_msg += f"2. Check for special characters in your message\n"
513
+ error_msg += f"3. Try a different model"
514
+ print(f"❌ Bad request: {response.status_code} - {response.text[:200]}")
515
+ return error_msg
516
+ else:
517
+ error_msg = f"🌐 **API Error {response.status_code}**\n\n"
518
+ error_msg += f"An unexpected error occurred.\n"
519
+ error_msg += f"Response: {response.text[:500]}\n\n"
520
+ error_msg += f"**Troubleshooting:**\n"
521
+ error_msg += f"1. Try again in a few moments\n"
522
+ error_msg += f"2. Check OpenRouter service status\n"
523
+ error_msg += f"3. Contact support if this persists"
524
+ print(f"❌ API error: {response.status_code} - {response.text[:200]}")
525
+ return error_msg
526
+
527
+ except requests.exceptions.Timeout:
528
+ error_msg = f"⏰ **Request Timeout**\n\n"
529
+ error_msg += f"The API request took too long (30s limit).\n\n"
530
+ error_msg += f"**Troubleshooting:**\n"
531
+ error_msg += f"1. Try again with a shorter message\n"
532
+ error_msg += f"2. Check your internet connection\n"
533
+ error_msg += f"3. Try a different model"
534
+ print(f"❌ Request timeout after 30 seconds")
535
+ return error_msg
536
+ except requests.exceptions.ConnectionError:
537
+ error_msg = f"🌐 **Connection Error**\n\n"
538
+ error_msg += f"Could not connect to OpenRouter API.\n\n"
539
+ error_msg += f"**Troubleshooting:**\n"
540
+ error_msg += f"1. Check your internet connection\n"
541
+ error_msg += f"2. Check OpenRouter service status\n"
542
+ error_msg += f"3. Try again in a few moments"
543
+ print(f"❌ Connection error to OpenRouter API")
544
+ return error_msg
545
+ except Exception as e:
546
+ error_msg = "❌ **Unexpected Error**\n\n"
547
+ error_msg += "An unexpected error occurred:\n"
548
+ error_msg += f"`{str(e)}`\n\n"
549
+ error_msg += "Please try again or contact support if this persists."
550
+ print(f"❌ Unexpected error: {str(e)}")
551
+ return error_msg
552
+
553
+ # Access code verification
554
+ access_granted = gr.State(False)
555
+ _access_granted_global = False # Global fallback
556
+
557
+ def verify_access_code(code):
558
+ """Verify the access code"""
559
+ global _access_granted_global
560
+ if ACCESS_CODE is None:
561
+ _access_granted_global = True
562
+ return gr.update(value="No access code required.", style={"color": "green"}), gr.update(visible=True), True
563
+
564
+ if code == ACCESS_CODE:
565
+ _access_granted_global = True
566
+ return gr.update(value="βœ… Access granted!", style={"color": "green"}), gr.update(visible=True), True
567
+ else:
568
+ _access_granted_global = False
569
+ return gr.update(value="❌ Invalid access code. Please try again.", style={"color": "red"}), gr.update(visible=False), False
570
+
571
+ def protected_generate_response(message, history):
572
+ """Protected response function that checks access"""
573
+ # Check if access is granted via the global variable
574
+ if ACCESS_CODE is not None and not _access_granted_global:
575
+ return "Please enter the access code to continue."
576
+ return generate_response(message, history)
577
+
578
+ # Global variable to store chat history for export
579
+ chat_history_store = []
580
+
581
+ def store_and_generate_response(message, history):
582
+ """Wrapper function that stores history and generates response"""
583
+ global chat_history_store
584
+
585
+ # Generate response using the protected function
586
+ response = protected_generate_response(message, history)
587
+
588
+ # Convert current history to the format we need for export
589
+ # history comes in as [["user1", "bot1"], ["user2", "bot2"], ...]
590
+ chat_history_store = []
591
+ if history:
592
+ for exchange in history:
593
+ if isinstance(exchange, dict):
594
+ chat_history_store.append(exchange)
595
+ elif isinstance(exchange, (list, tuple)) and len(exchange) >= 2:
596
+ chat_history_store.append({"role": "user", "content": exchange[0]})
597
+ chat_history_store.append({"role": "assistant", "content": exchange[1]})
598
+
599
+ # Add the current exchange
600
+ chat_history_store.append({"role": "user", "content": message})
601
+ chat_history_store.append({"role": "assistant", "content": response})
602
+
603
+ return response
604
+
605
+ def export_current_conversation():
606
+ """Export the current conversation"""
607
+ if not chat_history_store:
608
+ return gr.update(visible=False)
609
+
610
+ markdown_content = export_conversation_to_markdown(chat_history_store)
611
+
612
+ # Create logical filename with space name and timestamp
613
+ # Handle SPACE_NAME being a repr() string with quotes
614
+ # Remove quotes and whitespace from SPACE_NAME
615
+ if isinstance(SPACE_NAME, str):
616
+ space_name_clean = SPACE_NAME.strip()
617
+ for char in ["'", '"']:
618
+ space_name_clean = space_name_clean.strip(char)
619
+ else:
620
+ space_name_clean = "assistant"
621
+ space_name_safe = re.sub(r'[^a-zA-Z0-9]+', '_', space_name_clean).strip('_').lower()
622
+ timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
623
+ filename = f"{space_name_safe}_conversation_{timestamp}.md"
624
+
625
+ # Save to temporary file with meaningful name
626
+ temp_dir = tempfile.gettempdir()
627
+ temp_file = os.path.join(temp_dir, filename)
628
+
629
+ with open(temp_file, 'w', encoding='utf-8') as f:
630
+ f.write(markdown_content)
631
+
632
+ return gr.update(value=temp_file, visible=True)
633
+
634
+ def export_conversation(history):
635
+ """Export conversation to markdown file"""
636
+ if not history:
637
+ return gr.update(visible=False)
638
+
639
+ markdown_content = export_conversation_to_markdown(history)
640
+
641
+ # Create logical filename with space name and timestamp
642
+ # Handle SPACE_NAME being a repr() string with quotes
643
+ # Remove quotes and whitespace from SPACE_NAME
644
+ if isinstance(SPACE_NAME, str):
645
+ space_name_clean = SPACE_NAME.strip()
646
+ for char in ["'", '"']:
647
+ space_name_clean = space_name_clean.strip(char)
648
+ else:
649
+ space_name_clean = "assistant"
650
+ space_name_safe = re.sub(r'[^a-zA-Z0-9]+', '_', space_name_clean).strip('_').lower()
651
+ timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
652
+ filename = f"{space_name_safe}_conversation_{timestamp}.md"
653
+
654
+ # Save to temporary file with meaningful name
655
+ temp_dir = tempfile.gettempdir()
656
+ temp_file = os.path.join(temp_dir, filename)
657
+
658
+ with open(temp_file, 'w', encoding='utf-8') as f:
659
+ f.write(markdown_content)
660
+
661
+ return gr.update(value=temp_file, visible=True)
662
+
663
+ # Configuration status display
664
+ def get_configuration_status():
665
+ """Generate a clean configuration status message for display"""
666
+ status_parts = []
667
+
668
+ # Basic configuration info (without redundant "Configuration:" header)
669
+ status_parts.append(f"**Name:** {SPACE_NAME}")
670
+ status_parts.append(f"**Model:** {MODEL}")
671
+ status_parts.append(f"**Theme:** {THEME}")
672
+ status_parts.append(f"**Temperature:** {temperature}")
673
+ status_parts.append(f"**Max Response Tokens:** {max_tokens}")
674
+ status_parts.append(f"**File Upload:** {'Enabled' if ENABLE_FILE_UPLOAD else 'Disabled'}")
675
+ status_parts.append("")
676
+
677
+ # Example prompts
678
+ status_parts.append("")
679
+ examples_list = config.get('examples', [])
680
+ if isinstance(examples_list, str):
681
+ try:
682
+ import ast
683
+ examples_list = ast.literal_eval(examples_list)
684
+ except:
685
+ examples_list = []
686
+
687
+ if examples_list and len(examples_list) > 0:
688
+ status_parts.append("**Example Prompts:**")
689
+ for example in examples_list[:5]: # Show first 5 examples
690
+ status_parts.append(f"β€’ {example}")
691
+ if len(examples_list) > 5:
692
+ status_parts.append(f"β€’ ... and {len(examples_list) - 5} more")
693
+ else:
694
+ status_parts.append("**Example Prompts:** No example prompts configured")
695
+
696
+ # URL Context if configured
697
+ urls = GROUNDING_URLS
698
+ if isinstance(urls, str):
699
+ try:
700
+ import ast
701
+ urls = ast.literal_eval(urls)
702
+ except:
703
+ urls = []
704
+
705
+ if urls and len(urls) > 0:
706
+ status_parts.append("")
707
+ status_parts.append("**Grounding URLs:**")
708
+ for i, url in enumerate(urls[:5], 1): # Show first 5 URLs
709
+ status_parts.append(f"{i}. {url}")
710
+ if len(urls) > 5:
711
+ status_parts.append(f"... and {len(urls) - 5} more URLs")
712
+
713
+ # System prompt at the end
714
+ status_parts.append("")
715
+ status_parts.append(f"**System Prompt:** {SYSTEM_PROMPT}")
716
+
717
+ # API Key status (minimal, at the end)
718
+ status_parts.append("")
719
+ if not API_KEY_VALID:
720
+ status_parts.append(f"**Note:** API key ({API_KEY_VAR}) not configured in Space secrets")
721
+
722
+ return "\n".join(status_parts)
723
+
724
+ # HuggingFace Authentication Utility
725
+ def verify_hf_token_access():
726
+ """Verify HF_TOKEN has write access to the space"""
727
+ hf_token = os.environ.get("HF_TOKEN")
728
+ space_id = os.environ.get("SPACE_ID")
729
+
730
+ if not hf_token or not space_id:
731
+ return False, "Missing HF_TOKEN or SPACE_ID environment variables"
732
+
733
+ try:
734
+ from huggingface_hub import HfApi
735
+ api = HfApi(token=hf_token)
736
+ # Test access by getting space info
737
+ api.space_info(space_id)
738
+ return True, "Authenticated successfully"
739
+ except Exception as e:
740
+ return False, f"Authentication failed: {str(e)}"
741
+
742
+ # Create interface with access code protection
743
+ # Dynamically set theme based on configuration
744
+ theme_class = getattr(gr.themes, THEME, gr.themes.Default)
745
+ with gr.Blocks(title=SPACE_NAME, theme=theme_class()) as demo:
746
+ # Check if HF_TOKEN is configured to determine configuration panel availability
747
+ HF_TOKEN = os.environ.get("HF_TOKEN", "").strip()
748
+ SPACE_ID = os.environ.get("SPACE_ID", "").strip()
749
+
750
+ # Verify actual HuggingFace API access
751
+ HF_ACCESS_VALID, HF_ACCESS_MESSAGE = verify_hf_token_access()
752
+
753
+ # Always use tabs structure, Configuration tab visible only with HF_TOKEN
754
+ with gr.Tabs() as main_tabs:
755
+ with gr.Tab("Chat U/I"):
756
+ gr.Markdown(f"# {SPACE_NAME}")
757
+ gr.Markdown(SPACE_DESCRIPTION)
758
+
759
+ # Access code section (shown only if ACCESS_CODE is set)
760
+ with gr.Column(visible=(ACCESS_CODE is not None)) as access_section:
761
+ gr.Markdown("### πŸ” Access Required")
762
+ gr.Markdown("Please enter the access code provided by your instructor:")
763
+
764
+ access_input = gr.Textbox(
765
+ label="Access Code",
766
+ placeholder="Enter access code...",
767
+ type="password"
768
+ )
769
+ access_btn = gr.Button("Submit", variant="primary")
770
+ access_error = gr.Markdown(visible=False)
771
+
772
+ # Main chat interface (hidden until access granted)
773
+ with gr.Column(visible=(ACCESS_CODE is None)) as chat_section:
774
+ # Get examples from config
775
+ examples = config.get('examples', [])
776
+ if isinstance(examples, str):
777
+ try:
778
+ import ast
779
+ examples = ast.literal_eval(examples)
780
+ except:
781
+ examples = []
782
+
783
+ # Format examples for multimodal ChatInterface
784
+ formatted_examples = None
785
+ if examples:
786
+ # Check if examples are already formatted correctly
787
+ if examples and isinstance(examples[0], dict) and 'text' in examples[0]:
788
+ # Already formatted as multimodal, use as-is
789
+ formatted_examples = examples
790
+ else:
791
+ # Format as multimodal messages
792
+ formatted_examples = [{"text": example, "files": []} for example in examples]
793
+
794
+ # Configure multimodal textbox if file upload is enabled
795
+ if ENABLE_FILE_UPLOAD:
796
+ textbox = gr.MultimodalTextbox(
797
+ file_count="multiple",
798
+ file_types=None, # Accept all file types
799
+ show_label=False,
800
+ placeholder="Type a message...",
801
+ container=False
802
+ )
803
+ multimodal = True
804
+ else:
805
+ textbox = None
806
+ multimodal = False
807
+
808
+ chat_interface = gr.ChatInterface(
809
+ fn=store_and_generate_response, # Use wrapper function to store history
810
+ title="", # Title already shown above
811
+ description="", # Description already shown above
812
+ examples=formatted_examples,
813
+ type="messages", # Use modern message format for better compatibility
814
+ multimodal=multimodal,
815
+ textbox=textbox
816
+ )
817
+
818
+ # Export functionality
819
+ with gr.Row():
820
+ export_btn = gr.Button("πŸ“₯ Export Conversation", variant="secondary", size="sm")
821
+ export_file = gr.File(label="Download", visible=False)
822
+
823
+ # Connect export functionality
824
+ export_btn.click(
825
+ export_current_conversation,
826
+ outputs=[export_file]
827
+ )
828
+
829
+ # Configuration status
830
+ with gr.Accordion("Configuration", open=False):
831
+ gr.Markdown(get_configuration_status())
832
+
833
+ # Connect access verification within tab context
834
+ if ACCESS_CODE is not None:
835
+ access_btn.click(
836
+ verify_access_code,
837
+ inputs=[access_input],
838
+ outputs=[access_error, chat_section, access_granted]
839
+ )
840
+ access_input.submit(
841
+ verify_access_code,
842
+ inputs=[access_input],
843
+ outputs=[access_error, chat_section, access_granted]
844
+ )
845
+
846
+ # Add Configuration tab (only visible with valid HF_TOKEN)
847
+ with gr.Tab("Configuration", visible=HF_ACCESS_VALID) as config_tab:
848
+ gr.Markdown("## Configuration Management")
849
+
850
+ # Show authentication status
851
+ if HF_ACCESS_VALID:
852
+ gr.Markdown(f"βœ… **Authenticated** - {HF_ACCESS_MESSAGE}")
853
+ gr.Markdown("Configuration changes will be saved to the HuggingFace repository and the Space will restart automatically.")
854
+ faculty_auth_state = gr.State(True)
855
+ else:
856
+ gr.Markdown(f"❌ **Not Available** - {HF_ACCESS_MESSAGE}")
857
+ gr.Markdown("Set HF_TOKEN and SPACE_ID in Space secrets to enable configuration management.")
858
+ faculty_auth_state = gr.State(False)
859
+
860
+ # Configuration editor (visible if HF authentication is valid)
861
+ with gr.Column(visible=HF_ACCESS_VALID) as faculty_config_section:
862
+ gr.Markdown("### Edit Assistant Configuration")
863
+ gr.Markdown("⚠️ **Warning:** Changes will affect all users immediately.")
864
+
865
+ # Load current configuration
866
+ try:
867
+ with open('config.json', 'r') as f:
868
+ current_config = json.load(f)
869
+ except:
870
+ # Use DEFAULT_CONFIG as fallback
871
+ current_config = DEFAULT_CONFIG.copy()
872
+
873
+ # Editable fields
874
+ # System Prompt
875
+ edit_system_prompt = gr.Textbox(
876
+ label="System Prompt",
877
+ value=current_config.get('system_prompt', SYSTEM_PROMPT),
878
+ lines=5
879
+ )
880
+
881
+ # 3. Model Selection
882
+ edit_model = gr.Dropdown(
883
+ label="Model",
884
+ choices=[
885
+ "google/gemini-2.0-flash-001",
886
+ "google/gemma-3-27b-it",
887
+ "anthropic/claude-3.5-sonnet",
888
+ "anthropic/claude-3.5-haiku",
889
+ "openai/gpt-4o-mini-search-preview",
890
+ "openai/gpt-4.1-nano",
891
+ "nvidia/llama-3.1-nemotron-70b-instruct",
892
+ "qwen/qwen3-30b-a3b-instruct-2507"
893
+ ],
894
+ value=current_config.get('model', MODEL)
895
+ )
896
+
897
+ # 4. Example prompts field
898
+ examples_value = current_config.get('examples', [])
899
+ if isinstance(examples_value, list):
900
+ examples_text_value = "\n".join(examples_value)
901
+ else:
902
+ examples_text_value = ""
903
+
904
+ edit_examples = gr.Textbox(
905
+ label="Example Prompts (one per line)",
906
+ value=examples_text_value,
907
+ lines=3,
908
+ placeholder="What can you help me with?\nExplain this concept\nHelp me understand..."
909
+ )
910
+
911
+ # 5. Model Parameters
912
+ with gr.Row():
913
+ edit_temperature = gr.Slider(
914
+ label="Temperature",
915
+ minimum=0,
916
+ maximum=2,
917
+ value=current_config.get('temperature', 0.7),
918
+ step=0.1
919
+ )
920
+ edit_max_tokens = gr.Slider(
921
+ label="Max Tokens",
922
+ minimum=50,
923
+ maximum=4096,
924
+ value=current_config.get('max_tokens', 750),
925
+ step=50
926
+ )
927
+
928
+ # URL Grounding fields
929
+ gr.Markdown("### URL Grounding")
930
+ grounding_urls_value = current_config.get('grounding_urls', [])
931
+ if isinstance(grounding_urls_value, str):
932
+ try:
933
+ import ast
934
+ grounding_urls_value = ast.literal_eval(grounding_urls_value)
935
+ except:
936
+ grounding_urls_value = []
937
+
938
+ # Create 10 URL input fields
939
+ url_fields = []
940
+ for i in range(10):
941
+ url_value = grounding_urls_value[i] if i < len(grounding_urls_value) else ""
942
+ url_field = gr.Textbox(
943
+ label=f"URL {i+1}" + (" (Primary)" if i < 2 else " (Secondary)"),
944
+ value=url_value,
945
+ placeholder="https://..."
946
+ )
947
+ url_fields.append(url_field)
948
+
949
+ # Configuration file upload
950
+ gr.Markdown("### Upload Configuration")
951
+ with gr.Accordion("πŸ“€ Upload Existing Configuration", open=False):
952
+ gr.Markdown("Have a config.json from another deployment? Upload it here to restore all settings.")
953
+ config_upload = gr.File(
954
+ label="Upload Configuration (JSON)",
955
+ file_types=[".json"],
956
+ file_count="single",
957
+ interactive=True
958
+ )
959
+ upload_status = gr.Markdown("", visible=False)
960
+
961
+ config_locked = gr.Checkbox(
962
+ label="Lock Configuration (Prevent further edits)",
963
+ value=current_config.get('locked', False)
964
+ )
965
+
966
+ with gr.Row():
967
+ save_config_btn = gr.Button("Save Configuration", variant="primary")
968
+ reset_config_btn = gr.Button("Reset to Defaults", variant="secondary")
969
+
970
+ config_status = gr.Markdown("")
971
+
972
+ # Apply uploaded configuration function
973
+ def apply_uploaded_config(config_file):
974
+ """Apply uploaded configuration file to all form fields"""
975
+ if config_file is None:
976
+ return [
977
+ gr.update(), # system_prompt
978
+ gr.update(), # model
979
+ gr.update(), # examples
980
+ gr.update(), # temperature
981
+ gr.update(), # max_tokens
982
+ *[gr.update() for _ in range(10)], # url1-url10
983
+ gr.update(value="", visible=False), # upload_status
984
+ ]
985
+
986
+ try:
987
+ # Read and parse the JSON file
988
+ with open(config_file.name, 'r') as f:
989
+ config = json.load(f)
990
+
991
+ # Extract values with defaults
992
+ system_prompt = config.get('system_prompt', SYSTEM_PROMPT)
993
+ model = config.get('model', MODEL)
994
+ temperature = config.get('temperature', 0.7)
995
+ max_tokens = config.get('max_tokens', 750)
996
+
997
+ # Process examples
998
+ examples = config.get('examples', [])
999
+ if isinstance(examples, list):
1000
+ examples_text = '\n'.join(examples)
1001
+ else:
1002
+ examples_text = ""
1003
+
1004
+ # Process grounding URLs
1005
+ grounding_urls = config.get('grounding_urls', [])
1006
+ url_updates = []
1007
+ for i in range(10):
1008
+ if i < len(grounding_urls):
1009
+ url_updates.append(gr.update(value=grounding_urls[i]))
1010
+ else:
1011
+ url_updates.append(gr.update(value=""))
1012
+
1013
+ # Show success message
1014
+ status_msg = f"βœ… Configuration loaded successfully from: {config_file.name.split('/')[-1]}"
1015
+
1016
+ return [
1017
+ gr.update(value=system_prompt), # system_prompt
1018
+ gr.update(value=model), # model
1019
+ gr.update(value=examples_text), # examples
1020
+ gr.update(value=temperature), # temperature
1021
+ gr.update(value=max_tokens), # max_tokens
1022
+ *url_updates, # url1-url10
1023
+ gr.update(value=status_msg, visible=True), # upload_status
1024
+ ]
1025
+
1026
+ except json.JSONDecodeError as e:
1027
+ error_msg = f"❌ Invalid JSON format: {str(e)}"
1028
+ return [
1029
+ gr.update(), # system_prompt
1030
+ gr.update(), # model
1031
+ gr.update(), # examples
1032
+ gr.update(), # temperature
1033
+ gr.update(), # max_tokens
1034
+ *[gr.update() for _ in range(10)], # url1-url10
1035
+ gr.update(value=error_msg, visible=True), # upload_status
1036
+ ]
1037
+ except Exception as e:
1038
+ error_msg = f"❌ Error loading configuration: {str(e)}"
1039
+ return [
1040
+ gr.update(), # system_prompt
1041
+ gr.update(), # model
1042
+ gr.update(), # examples
1043
+ gr.update(), # temperature
1044
+ gr.update(), # max_tokens
1045
+ *[gr.update() for _ in range(10)], # url1-url10
1046
+ gr.update(value=error_msg, visible=True), # upload_status
1047
+ ]
1048
+
1049
+ # Save configuration function
1050
+ def save_configuration(is_authenticated, new_prompt, new_model, new_examples, new_temp, new_tokens, *url_values):
1051
+ if not is_authenticated:
1052
+ return "Not authenticated"
1053
+
1054
+ # Check if configuration is already locked
1055
+ try:
1056
+ with open('config.json', 'r') as f:
1057
+ existing_config = json.load(f)
1058
+ if existing_config.get('locked', False):
1059
+ return "Configuration is locked and cannot be modified"
1060
+ except:
1061
+ pass
1062
+
1063
+ # Load current config to preserve all values
1064
+ try:
1065
+ with open('config.json', 'r') as f:
1066
+ current_full_config = json.load(f)
1067
+ except:
1068
+ # If config.json doesn't exist, use default configuration
1069
+ current_full_config = DEFAULT_CONFIG.copy()
1070
+
1071
+ # Process example prompts
1072
+ examples_list = [ex.strip() for ex in new_examples.split('\n') if ex.strip()]
1073
+
1074
+ # Process URL values - lock_config is the last parameter
1075
+ urls = list(url_values[:-1]) # All but last are URLs
1076
+ lock_config_from_args = url_values[-1] # Last is lock_config
1077
+ # Filter out empty URLs
1078
+ grounding_urls = [url.strip() for url in urls if url.strip()]
1079
+
1080
+ # Create backup before making changes
1081
+ try:
1082
+ # Create backups directory if it doesn't exist
1083
+ os.makedirs('config_backups', exist_ok=True)
1084
+
1085
+ # Create timestamped backup
1086
+ backup_filename = f"config_backups/config_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
1087
+ with open(backup_filename, 'w') as backup_file:
1088
+ json.dump(current_full_config, backup_file, indent=2)
1089
+
1090
+ # Keep only last 10 backups
1091
+ backups = sorted([f for f in os.listdir('config_backups') if f.endswith('.json')])
1092
+ if len(backups) > 10:
1093
+ for old_backup in backups[:-10]:
1094
+ os.remove(os.path.join('config_backups', old_backup))
1095
+ except Exception as backup_error:
1096
+ print(f"Warning: Could not create backup: {backup_error}")
1097
+ # Continue with save even if backup fails
1098
+
1099
+ # Update all editable fields while preserving everything else
1100
+ current_full_config.update({
1101
+ 'system_prompt': new_prompt,
1102
+ 'model': new_model,
1103
+ 'examples': examples_list,
1104
+ 'temperature': new_temp,
1105
+ 'max_tokens': int(new_tokens),
1106
+ 'grounding_urls': grounding_urls,
1107
+ 'locked': lock_config_from_args,
1108
+ 'last_modified': datetime.now().isoformat(),
1109
+ 'last_modified_by': 'faculty'
1110
+ })
1111
+
1112
+ try:
1113
+ with open('config.json', 'w') as f:
1114
+ json.dump(current_full_config, f, indent=2)
1115
+
1116
+ # Optional: Auto-commit to HuggingFace if token is available
1117
+ hf_token = os.environ.get("HF_TOKEN")
1118
+ space_id = os.environ.get("SPACE_ID")
1119
+
1120
+ if hf_token and space_id:
1121
+ try:
1122
+ from huggingface_hub import HfApi, CommitOperationAdd, restart_space
1123
+ api = HfApi(token=hf_token)
1124
+
1125
+ # Create commit operation to upload config.json
1126
+ operations = [
1127
+ CommitOperationAdd(
1128
+ path_or_fileobj="config.json",
1129
+ path_in_repo="config.json"
1130
+ )
1131
+ ]
1132
+
1133
+ # Create commit with updated configuration
1134
+ api.create_commit(
1135
+ repo_id=space_id,
1136
+ operations=operations,
1137
+ commit_message=f"Update configuration by faculty at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
1138
+ commit_description="Faculty configuration update through web interface",
1139
+ repo_type="space",
1140
+ token=hf_token
1141
+ )
1142
+
1143
+ # Automatic restart
1144
+ try:
1145
+ restart_space(space_id, token=hf_token)
1146
+ return f"βœ… Configuration saved and committed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Space is restarting automatically!**\n\nThe page will refresh in about 30 seconds. Your changes will be applied."
1147
+ except Exception as restart_error:
1148
+ print(f"Could not auto-restart: {restart_error}")
1149
+ return f"βœ… Configuration saved and committed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Please restart manually** (auto-restart failed)\n\n1. Go to Settings (βš™οΈ)\n2. Click 'Factory reboot'\n3. Wait ~30 seconds"
1150
+ except Exception as commit_error:
1151
+ print(f"Note: Could not auto-commit to repository: {commit_error}")
1152
+ return f"βœ… Configuration saved locally at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Manual Restart Required**\nFor changes to take effect:\n1. Go to Settings (βš™οΈ)\n2. Click 'Factory reboot'\n3. Wait ~30 seconds for restart"
1153
+ else:
1154
+ return f"βœ… Configuration saved at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Manual Restart Required**\nFor changes to take effect:\n1. Go to Settings (βš™οΈ)\n2. Click 'Factory reboot'\n3. Wait ~30 seconds for restart"
1155
+ except Exception as e:
1156
+ return f"❌ Error saving configuration: {str(e)}"
1157
+
1158
+ # Reset configuration function
1159
+ def reset_configuration(is_authenticated):
1160
+ if not is_authenticated:
1161
+ updates = ["Not authenticated"] + [gr.update() for _ in range(14)] # 1 status + 14 fields (prompt, model, examples, temp, tokens + 10 urls)
1162
+ return tuple(updates)
1163
+
1164
+ # Check if locked
1165
+ try:
1166
+ with open('config.json', 'r') as f:
1167
+ existing_config = json.load(f)
1168
+ if existing_config.get('locked', False):
1169
+ updates = ["Configuration is locked"] + [gr.update() for _ in range(14)]
1170
+ return tuple(updates)
1171
+ except:
1172
+ pass
1173
+
1174
+ # Get default examples as text
1175
+ default_examples = DEFAULT_CONFIG.get('examples', [])
1176
+ if isinstance(default_examples, list):
1177
+ examples_text = "\n".join(default_examples)
1178
+ else:
1179
+ examples_text = ""
1180
+
1181
+ # Get default URLs - parse from JSON string if needed
1182
+ default_urls = DEFAULT_CONFIG.get('grounding_urls', [])
1183
+ if isinstance(default_urls, str):
1184
+ try:
1185
+ import json
1186
+ default_urls = json.loads(default_urls)
1187
+ except:
1188
+ default_urls = []
1189
+ elif not isinstance(default_urls, list):
1190
+ default_urls = []
1191
+
1192
+ # Reset to original default values
1193
+ updates = [
1194
+ "Reset to default values",
1195
+ gr.update(value=DEFAULT_CONFIG.get('system_prompt', SYSTEM_PROMPT)),
1196
+ gr.update(value=DEFAULT_CONFIG.get('model', MODEL)),
1197
+ gr.update(value=examples_text),
1198
+ gr.update(value=DEFAULT_CONFIG.get('temperature', temperature)),
1199
+ gr.update(value=DEFAULT_CONFIG.get('max_tokens', max_tokens))
1200
+ ]
1201
+
1202
+ # Add URL updates
1203
+ for i in range(10):
1204
+ url_value = default_urls[i] if i < len(default_urls) else ""
1205
+ updates.append(gr.update(value=url_value))
1206
+
1207
+ return tuple(updates)
1208
+
1209
+
1210
+ # Connect configuration buttons
1211
+ save_config_btn.click(
1212
+ save_configuration,
1213
+ inputs=[faculty_auth_state, edit_system_prompt, edit_model, edit_examples, edit_temperature, edit_max_tokens] + url_fields + [config_locked],
1214
+ outputs=[config_status]
1215
+ )
1216
+
1217
+ reset_config_btn.click(
1218
+ reset_configuration,
1219
+ inputs=[faculty_auth_state],
1220
+ outputs=[config_status, edit_system_prompt, edit_model, edit_examples, edit_temperature, edit_max_tokens] + url_fields
1221
+ )
1222
+
1223
+ # Connect upload handler
1224
+ config_upload.change(
1225
+ apply_uploaded_config,
1226
+ inputs=[config_upload],
1227
+ outputs=[edit_system_prompt, edit_model, edit_examples, edit_temperature, edit_max_tokens] + url_fields + [upload_status]
1228
+ )
1229
+
1230
+ if __name__ == "__main__":
1231
+ demo.launch()
config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "STEM Adventure Games",
3
+ "description": "Choose your own STEM adventure games",
4
+ "system_prompt": "Transform into an interactive co-learning guide who creates Choose Your Own STEM Adventure games featuring historically significant scientific experiments, opening each session with an eye-catching unicode-styled arcade menu presenting 5-6 adventures drawn from Wikipedia's List of Experiments with brief enticing descriptions, then immersing players in vivid historical moments written in second person (e.g. 'You are Galileo Galilei') that establish the year, location, prevailing beliefs, and tensions between established wisdom and emerging observations, presenting 3-4 numbered decision points per stage that reflect authentic scientific choices ranging from experimental design and measurement approaches to strategic decisions about convincing skeptics or timing publications, with each choice meaningfully different and leading to distinct paths forward, then narrating results with sensory details, colleague reactions, and unexpected observations that teach concepts naturally through unfolding drama rather than lectures, always ending scenes with new branching choices that maintain narrative momentum while reinforcing science as an iterative process of hypothesis, testing, and refinement, offering backtracking options to emphasize how so-called failed experiments provide crucial insights, balancing historical accuracy with engaging gameplay that shows how systematic thinking and creative problem-solving combine in scientific breakthroughs.",
5
+ "model": "qwen/qwen3-30b-a3b-instruct-2507",
6
+ "api_key_var": "API_KEY",
7
+ "temperature": 0.6,
8
+ "max_tokens": 1000,
9
+ "examples": [
10
+ "Initiate adventure!",
11
+ "How do I play?",
12
+ "What's the meaning of this?"
13
+ ],
14
+ "grounding_urls": [
15
+ "https://en.wikipedia.org/wiki/List_of_experiments",
16
+ "https://en.wikipedia.org/wiki/Scientific_method",
17
+ "https://en.wikipedia.org/wiki/List_of_experiments#Biology",
18
+ "https://en.wikipedia.org/wiki/List_of_experiments#Astronomy",
19
+ "https://en.wikipedia.org/wiki/List_of_experiments#Chemistry",
20
+ "https://en.wikipedia.org/wiki/List_of_experiments#Physics",
21
+ "https://en.wikipedia.org/wiki/List_of_experiments#Geology"
22
+ ],
23
+ "enable_dynamic_urls": true,
24
+ "enable_file_upload": true,
25
+ "theme": "Glass"
26
+ }
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=5.39.0
2
+ requests>=2.32.3
3
+ beautifulsoup4>=4.12.3
4
+ python-dotenv>=1.0.0