andevs commited on
Commit
0177236
·
verified ·
1 Parent(s): 2477619

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +713 -101
app.py CHANGED
@@ -1,165 +1,777 @@
1
- from fastapi import FastAPI, HTTPException, UploadFile, File, Form
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import JSONResponse
 
 
 
4
  import json
5
- import hashlib
6
- from datetime import datetime, timedelta
7
  import re
 
 
8
  import logging
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Configure logging
11
- logging.basicConfig(level=logging.INFO)
 
 
 
12
  logger = logging.getLogger(__name__)
13
 
14
- # Initialize FastAPI app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  app = FastAPI(
16
- title="StudyLoop API",
17
- version="1.0.0",
18
- description="AI-powered study question generator"
 
 
 
19
  )
20
 
21
  # CORS middleware
22
  app.add_middleware(
23
  CORSMiddleware,
24
- allow_origins=["*"],
25
  allow_credentials=True,
26
  allow_methods=["*"],
27
  allow_headers=["*"],
28
  )
29
 
30
- # Simple cache
31
- cache = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Health endpoints
34
  @app.get("/")
35
  async def root():
36
- return {"message": "StudyLoop API", "status": "healthy"}
 
 
 
 
 
 
37
 
38
  @app.get("/health")
39
  async def health():
40
- return {"status": "healthy", "timestamp": datetime.now().isoformat()}
41
-
42
- # Generate mock questions
43
- def generate_questions(count=5):
44
- questions = []
45
- for i in range(count):
46
- if i % 2 == 0:
47
- questions.append({
48
- "id": f"q_{i}",
49
- "type": "multiple_choice",
50
- "question": "What is the main topic?",
51
- "options": ["Option A", "Option B", "Option C", "Option D"],
52
- "correct_answer": "Option A",
53
- "explanation": "Based on the text content.",
54
- "difficulty": "easy",
55
- "concept": "Main Topic"
56
- })
57
- else:
58
- questions.append({
59
- "id": f"q_{i}",
60
- "type": "short_answer",
61
- "question": "Explain the key concept.",
62
- "correct_answer": "The key concept is explained in the text.",
63
- "explanation": "Reference the source material.",
64
- "difficulty": "medium",
65
- "concept": "Key Concepts"
66
- })
67
- return questions
68
-
69
- # Main API endpoint
70
- @app.post("/api/generate")
71
- async def generate_questions_endpoint(
72
- text: str = Form(None),
73
- file: UploadFile = File(None),
74
  session_id: str = Form(...),
75
- options: str = Form('{"num_questions": 5}')
 
76
  ):
 
 
 
 
 
 
 
 
 
77
  try:
 
 
 
 
 
 
 
78
  # Parse options
79
  try:
80
- opts = json.loads(options)
81
- num_questions = min(opts.get("num_questions", 5), 10)
82
- except:
83
- num_questions = 5
84
-
85
- content = ""
86
-
87
- # Get content
88
- if text:
89
- content = text
90
- elif file:
91
- file_bytes = await file.read()
92
- if len(file_bytes) > 5 * 1024 * 1024:
93
- raise HTTPException(400, "File too large (max 5MB)")
94
- try:
95
- content = file_bytes.decode('utf-8')
96
- except:
97
- content = file_bytes.decode('latin-1')
98
- else:
99
- raise HTTPException(400, "Provide text or file")
100
 
101
- # Clean content
102
- content = re.sub(r'\s+', ' ', content).strip()
 
103
 
104
- if len(content) < 10:
105
- raise HTTPException(400, "Text too short")
106
 
107
- # Generate questions
108
- questions = generate_questions(num_questions)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  # Extract concepts
111
- concepts = list(set([q.get("concept", "General") for q in questions]))
 
 
 
112
 
 
113
  response = {
114
  "session_id": session_id,
115
  "concepts": concepts,
116
- "questions": questions,
117
  "generated_at": datetime.utcnow().isoformat(),
118
- "question_count": len(questions)
 
 
119
  }
120
 
 
 
 
 
 
121
  return JSONResponse(response)
122
 
123
  except HTTPException:
124
  raise
125
  except Exception as e:
126
- logger.error(f"Error: {e}")
127
- raise HTTPException(500, f"Internal error: {str(e)}")
128
 
129
- # Generate more questions
130
  @app.post("/api/generate-more")
131
- async def generate_more(
132
  session_id: str = Form(...),
133
- concepts: str = Form("[]")
 
134
  ):
 
 
 
 
 
 
 
135
  try:
136
- # Generate 3 more questions
137
- questions = generate_questions(3)
138
- for q in questions:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  q["is_followup"] = True
 
140
 
141
  return {
142
  "session_id": session_id,
143
  "questions": questions,
144
- "generated_at": datetime.utcnow().isoformat()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  }
 
146
  except Exception as e:
147
- raise HTTPException(500, str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
- # API info
150
- @app.get("/api/info")
151
- async def api_info():
 
152
  return {
153
- "name": "StudyLoop API",
154
- "version": "1.0.0",
155
- "endpoints": [
156
- {"path": "/api/generate", "method": "POST"},
157
- {"path": "/api/generate-more", "method": "POST"},
158
- {"path": "/health", "method": "GET"},
159
- {"path": "/api/info", "method": "GET"}
160
- ]
 
 
 
 
 
 
161
  }
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  if __name__ == "__main__":
164
  import uvicorn
165
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ StudyLoop AI Backend - Optimized for Hugging Face Spaces
3
+ High-performance, reliable, with intelligent fallbacks
4
+ """
5
+
6
+ import os
7
  import json
 
 
8
  import re
9
+ import hashlib
10
+ import asyncio
11
  import logging
12
+ import time
13
+ from typing import List, Dict, Optional, Any
14
+ from datetime import datetime, timedelta
15
+ from contextlib import asynccontextmanager
16
+
17
+ # FastAPI
18
+ from fastapi import FastAPI, HTTPException, UploadFile, File, Form, BackgroundTasks
19
+ from fastapi.middleware.cors import CORSMiddleware
20
+ from fastapi.responses import JSONResponse, StreamingResponse
21
+ from fastapi.staticfiles import StaticFiles
22
+ from pydantic import BaseModel, Field
23
+
24
+ # Hugging Face
25
+ from huggingface_hub import InferenceClient
26
+ import torch
27
+ from transformers import pipeline, AutoTokenizer
28
 
29
  # Configure logging
30
+ logging.basicConfig(
31
+ level=logging.INFO,
32
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
33
+ )
34
  logger = logging.getLogger(__name__)
35
 
36
+ # ==================== CONFIGURATION ====================
37
+ class Config:
38
+ # Hugging Face Models (Free tier optimized)
39
+ MODELS = {
40
+ "primary": "microsoft/phi-2", # Fast, good quality
41
+ "fallback": "google/flan-t5-small", # Reliable fallback
42
+ "summarizer": "facebook/bart-large-cnn", # Good for explanations
43
+ "concept_extractor": "microsoft/phi-2" # Multi-purpose
44
+ }
45
+
46
+ # Rate limiting
47
+ REQUESTS_PER_HOUR = 50
48
+ MAX_TOKENS_PER_REQUEST = 1500
49
+
50
+ # Content limits
51
+ MAX_CONTENT_LENGTH = 50000
52
+ MIN_CONTENT_LENGTH = 100
53
+ MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
54
+
55
+ # Generation settings
56
+ DEFAULT_QUESTIONS = 8
57
+ MAX_QUESTIONS = 20
58
+ QUESTION_TYPES = ["multiple_choice", "short_answer", "true_false", "fill_blank"]
59
+
60
+ config = Config()
61
+
62
+ # ==================== AI ENGINE ====================
63
+ class AIEngine:
64
+ """Intelligent AI engine with multiple fallbacks"""
65
+
66
+ def __init__(self):
67
+ self.client = InferenceClient()
68
+ self.tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
69
+ self.cache = {}
70
+ self.model_health = {}
71
+ self.initialize_models()
72
+
73
+ def initialize_models(self):
74
+ """Initialize models and check availability"""
75
+ try:
76
+ # Test primary model
77
+ test_response = self.client.text_generation(
78
+ "Hello",
79
+ model=config.MODELS["primary"],
80
+ max_new_tokens=10
81
+ )
82
+ self.model_health[config.MODELS["primary"]] = "healthy"
83
+ logger.info(f"Primary model {config.MODELS['primary']} loaded successfully")
84
+ except Exception as e:
85
+ logger.warning(f"Primary model failed: {e}")
86
+ self.model_health[config.MODELS["primary"]] = "unhealthy"
87
+
88
+ # Always set fallback as healthy
89
+ self.model_health[config.MODELS["fallback"]] = "healthy"
90
+
91
+ def estimate_tokens(self, text: str) -> int:
92
+ """Estimate token count for text"""
93
+ return len(self.tokenizer.encode(text))
94
+
95
+ def get_healthy_model(self) -> str:
96
+ """Get the healthiest available model"""
97
+ for model, health in self.model_health.items():
98
+ if health == "healthy":
99
+ return model
100
+ return config.MODELS["fallback"] # Always fallback
101
+
102
+ async def generate_with_retry(self, prompt: str, model: str = None, max_retries: int = 3) -> str:
103
+ """Generate text with retry logic"""
104
+ model = model or self.get_healthy_model()
105
+
106
+ for attempt in range(max_retries):
107
+ try:
108
+ # Check cache first
109
+ cache_key = f"{model}:{hashlib.md5(prompt.encode()).hexdigest()}"
110
+ if cache_key in self.cache:
111
+ return self.cache[cache_key]
112
+
113
+ logger.info(f"Generating with {model} (attempt {attempt + 1})")
114
+
115
+ response = await asyncio.to_thread(
116
+ self.client.text_generation,
117
+ prompt,
118
+ model=model,
119
+ max_new_tokens=800,
120
+ temperature=0.7,
121
+ top_p=0.9,
122
+ repetition_penalty=1.1,
123
+ do_sample=True,
124
+ seed=42
125
+ )
126
+
127
+ # Cache successful response
128
+ self.cache[cache_key] = response
129
+ self.model_health[model] = "healthy"
130
+
131
+ return response
132
+
133
+ except Exception as e:
134
+ logger.warning(f"Attempt {attempt + 1} failed for {model}: {e}")
135
+ self.model_health[model] = "unhealthy"
136
+
137
+ if attempt == max_retries - 1:
138
+ raise
139
+
140
+ # Exponential backoff
141
+ await asyncio.sleep(2 ** attempt)
142
+
143
+ # Try next healthy model
144
+ model = self.get_healthy_model()
145
+
146
+ def extract_json_from_text(self, text: str) -> Dict:
147
+ """Extract JSON from AI response"""
148
+ try:
149
+ # Find JSON in response
150
+ json_match = re.search(r'\{.*\}', text, re.DOTALL)
151
+ if json_match:
152
+ json_str = json_match.group()
153
+ return json.loads(json_str)
154
+ except json.JSONDecodeError:
155
+ pass
156
+
157
+ # Fallback: create basic structure
158
+ return {"questions": [], "concepts": []}
159
+
160
+ def create_question_prompt(self, content: str, num_questions: int, question_types: List[str]) -> str:
161
+ """Create optimized prompt for question generation"""
162
+ content_preview = content[:1500] # Limit content length
163
+
164
+ return f"""You are an expert educational AI creating study questions.
165
+
166
+ CONTENT TO ANALYZE:
167
+ {content_preview}
168
+
169
+ INSTRUCTIONS:
170
+ 1. Create {num_questions} diverse questions ({', '.join(question_types)})
171
+ 2. Base questions STRICTLY on the provided content
172
+ 3. For multiple choice: 4 options, mark correct with [CORRECT]
173
+ 4. Include difficulty (1=easy, 2=medium, 3=hard)
174
+ 5. For short answers: provide sample ideal answer
175
+ 6. Return valid JSON format
176
+
177
+ EXAMPLE FORMAT:
178
+ {{
179
+ "concepts": ["Main Topic", "Key Idea"],
180
+ "questions": [
181
+ {{
182
+ "type": "multiple_choice",
183
+ "question": "What is the main topic?",
184
+ "options": ["Option A", "Option B [CORRECT]", "Option C", "Option D"],
185
+ "correct_answer": "Option B",
186
+ "explanation": "The text clearly states...",
187
+ "difficulty": 1,
188
+ "concept": "Main Topic"
189
+ }}
190
+ ]
191
+ }}
192
+
193
+ Now generate questions based on the provided content:"""
194
+
195
+ # ==================== CACHE MANAGEMENT ====================
196
+ class SmartCache:
197
+ """Intelligent caching system"""
198
+
199
+ def __init__(self, max_size=1000):
200
+ self.cache = {}
201
+ self.max_size = max_size
202
+ self.hits = 0
203
+ self.misses = 0
204
+
205
+ def get(self, key: str):
206
+ if key in self.cache:
207
+ data, expiry = self.cache[key]
208
+ if datetime.now() < expiry:
209
+ self.hits += 1
210
+ return data
211
+ del self.cache[key]
212
+ self.misses += 1
213
+ return None
214
+
215
+ def set(self, key: str, data: Any, ttl: int = 3600):
216
+ if len(self.cache) >= self.max_size:
217
+ # Remove oldest 10% of items
218
+ to_remove = int(self.max_size * 0.1)
219
+ oldest_keys = sorted(self.cache.keys(),
220
+ key=lambda k: self.cache[k][1])[:to_remove]
221
+ for k in oldest_keys:
222
+ del self.cache[k]
223
+
224
+ self.cache[key] = (data, datetime.now() + timedelta(seconds=ttl))
225
+
226
+ def stats(self):
227
+ total = self.hits + self.misses
228
+ hit_rate = (self.hits / total * 100) if total > 0 else 0
229
+ return {
230
+ "hits": self.hits,
231
+ "misses": self.misses,
232
+ "hit_rate": f"{hit_rate:.1f}%",
233
+ "size": len(self.cache)
234
+ }
235
+
236
+ # ==================== VALIDATION ====================
237
+ class ContentValidator:
238
+ """Validate and clean content"""
239
+
240
+ @staticmethod
241
+ def validate_content(text: str) -> Dict[str, Any]:
242
+ """Validate content for quality and safety"""
243
+
244
+ # Check length
245
+ if len(text) < config.MIN_CONTENT_LENGTH:
246
+ return {"valid": False, "error": f"Content too short (min {config.MIN_CONTENT_LENGTH} chars)"}
247
+
248
+ if len(text) > config.MAX_CONTENT_LENGTH:
249
+ return {"valid": False, "error": f"Content too long (max {config.MAX_CONTENT_LENGTH} chars)"}
250
+
251
+ # Check for spam/abuse
252
+ spam_patterns = [
253
+ r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
254
+ r'\b(?:viagra|cialis|casino|poker|bitcoin)\b',
255
+ r'\b\d{10,}\b', # Long numbers
256
+ ]
257
+
258
+ for pattern in spam_patterns:
259
+ if re.search(pattern, text, re.IGNORECASE):
260
+ return {"valid": False, "error": "Content contains prohibited material"}
261
+
262
+ # Calculate readability metrics
263
+ words = text.split()
264
+ sentences = re.split(r'[.!?]+', text)
265
+
266
+ metrics = {
267
+ "word_count": len(words),
268
+ "sentence_count": len([s for s in sentences if s.strip()]),
269
+ "avg_sentence_length": len(words) / max(len(sentences), 1),
270
+ "unique_words": len(set(word.lower() for word in words))
271
+ }
272
+
273
+ # Check if content is meaningful
274
+ if metrics["unique_words"] / max(metrics["word_count"], 1) < 0.3:
275
+ return {"valid": False, "error": "Content appears repetitive or low quality"}
276
+
277
+ return {"valid": True, "metrics": metrics}
278
+
279
+ @staticmethod
280
+ def clean_text(text: str) -> str:
281
+ """Clean and normalize text"""
282
+ # Remove extra whitespace
283
+ text = re.sub(r'\s+', ' ', text)
284
+ # Remove non-printable characters
285
+ text = ''.join(char for char in text if char.isprintable() or char in '\n\t\r')
286
+ # Normalize line endings
287
+ text = text.replace('\r\n', '\n').replace('\r', '\n')
288
+ return text.strip()
289
+
290
+ # ==================== RATE LIMITING ====================
291
+ class RateLimiter:
292
+ """Simple rate limiter"""
293
+
294
+ def __init__(self):
295
+ self.requests = {}
296
+
297
+ def check_limit(self, ip: str, limit: int = config.REQUESTS_PER_HOUR) -> bool:
298
+ now = datetime.now()
299
+ hour_key = f"{ip}:{now.strftime('%Y-%m-%d-%H')}"
300
+
301
+ if hour_key not in self.requests:
302
+ self.requests[hour_key] = []
303
+
304
+ # Clean old requests
305
+ self.requests[hour_key] = [
306
+ req_time for req_time in self.requests[hour_key]
307
+ if now - req_time < timedelta(hours=1)
308
+ ]
309
+
310
+ if len(self.requests[hour_key]) >= limit:
311
+ return False
312
+
313
+ self.requests[hour_key].append(now)
314
+ return True
315
+
316
+ # ==================== FASTAPI APP ====================
317
+ @asynccontextmanager
318
+ async def lifespan(app: FastAPI):
319
+ """Lifespan context manager"""
320
+ logger.info("🚀 Starting StudyLoop AI Backend...")
321
+
322
+ # Initialize AI Engine
323
+ app.state.ai_engine = AIEngine()
324
+ app.state.cache = SmartCache()
325
+ app.state.validator = ContentValidator()
326
+ app.state.rate_limiter = RateLimiter()
327
+
328
+ logger.info("✅ AI Engine initialized")
329
+ logger.info(f"📊 Using models: {config.MODELS}")
330
+
331
+ yield
332
+
333
+ logger.info("🛑 Shutting down StudyLoop AI Backend...")
334
+
335
+ # Create FastAPI app
336
  app = FastAPI(
337
+ title="StudyLoop AI",
338
+ version="2.0",
339
+ description="Intelligent study question generator powered by Hugging Face",
340
+ docs_url="/docs" if os.getenv("ENVIRONMENT") == "development" else None,
341
+ redoc_url=None,
342
+ lifespan=lifespan
343
  )
344
 
345
  # CORS middleware
346
  app.add_middleware(
347
  CORSMiddleware,
348
+ allow_origins=["*"], # Configure properly in production
349
  allow_credentials=True,
350
  allow_methods=["*"],
351
  allow_headers=["*"],
352
  )
353
 
354
+ # ==================== MODELS ====================
355
+ class GenerateRequest(BaseModel):
356
+ text: str = Field(..., min_length=config.MIN_CONTENT_LENGTH, max_length=config.MAX_CONTENT_LENGTH)
357
+ session_id: str
358
+ options: Optional[Dict[str, Any]] = None
359
+
360
+ class Question(BaseModel):
361
+ id: str
362
+ type: str
363
+ question: str
364
+ correct_answer: str
365
+ explanation: str
366
+ difficulty: int = Field(ge=1, le=3)
367
+ concept: str
368
+
369
+ class GenerateResponse(BaseModel):
370
+ session_id: str
371
+ concepts: List[str]
372
+ questions: List[Question]
373
+ generated_at: str
374
+ processing_time: float
375
+ is_cached: bool = False
376
 
377
+ # ==================== HEALTH ENDPOINTS ====================
378
  @app.get("/")
379
  async def root():
380
+ return {
381
+ "service": "StudyLoop AI",
382
+ "version": "2.0",
383
+ "status": "operational",
384
+ "ai_models": config.MODELS,
385
+ "timestamp": datetime.now().isoformat()
386
+ }
387
 
388
  @app.get("/health")
389
  async def health():
390
+ return {
391
+ "status": "healthy",
392
+ "ai_engine": "operational",
393
+ "cache_stats": app.state.cache.stats(),
394
+ "timestamp": datetime.now().isoformat()
395
+ }
396
+
397
+ @app.get("/models")
398
+ async def list_models():
399
+ return {
400
+ "available_models": config.MODELS,
401
+ "model_health": app.state.ai_engine.model_health,
402
+ "primary_model": config.MODELS["primary"]
403
+ }
404
+
405
+ # ==================== MAIN API ENDPOINTS ====================
406
+ @app.post("/api/generate", response_model=GenerateResponse)
407
+ async def generate_questions(
408
+ text: str = Form(...),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
  session_id: str = Form(...),
410
+ options: str = Form('{"num_questions": 8}'),
411
+ background_tasks: BackgroundTasks = None
412
  ):
413
+ """
414
+ Generate study questions from text content
415
+
416
+ - **text**: The study material to analyze
417
+ - **session_id**: Unique session identifier
418
+ - **options**: JSON string with generation options
419
+ """
420
+ start_time = time.time()
421
+
422
  try:
423
+ # Rate limiting (using session_id as proxy for IP)
424
+ if not app.state.rate_limiter.check_limit(session_id):
425
+ raise HTTPException(
426
+ status_code=429,
427
+ detail=f"Rate limit exceeded. Max {config.REQUESTS_PER_HOUR} requests per hour."
428
+ )
429
+
430
  # Parse options
431
  try:
432
+ options_dict = json.loads(options)
433
+ num_questions = min(
434
+ options_dict.get("num_questions", config.DEFAULT_QUESTIONS),
435
+ config.MAX_QUESTIONS
436
+ )
437
+ question_types = options_dict.get("question_types", ["multiple_choice", "short_answer"])
438
+ except json.JSONDecodeError:
439
+ num_questions = config.DEFAULT_QUESTIONS
440
+ question_types = ["multiple_choice", "short_answer"]
 
 
 
 
 
 
 
 
 
 
 
441
 
442
+ # Clean and validate content
443
+ clean_text = app.state.validator.clean_text(text)
444
+ validation = app.state.validator.validate_content(clean_text)
445
 
446
+ if not validation["valid"]:
447
+ raise HTTPException(status_code=400, detail=validation["error"])
448
 
449
+ # Create cache key
450
+ content_hash = hashlib.md5(clean_text.encode()).hexdigest()
451
+ cache_key = f"generate:{content_hash}:{num_questions}:{','.join(sorted(question_types))}"
452
+
453
+ # Check cache
454
+ cached = app.state.cache.get(cache_key)
455
+ if cached:
456
+ logger.info(f"Cache hit for {content_hash}")
457
+ response = cached.copy()
458
+ response["is_cached"] = True
459
+ response["processing_time"] = time.time() - start_time
460
+ return JSONResponse(response)
461
+
462
+ logger.info(f"Generating {num_questions} questions...")
463
+
464
+ # Create prompt
465
+ prompt = app.state.ai_engine.create_question_prompt(
466
+ clean_text, num_questions, question_types
467
+ )
468
+
469
+ # Generate with AI
470
+ ai_response = await app.state.ai_engine.generate_with_retry(prompt)
471
+
472
+ # Parse AI response
473
+ parsed_data = app.state.ai_engine.extract_json_from_text(ai_response)
474
+
475
+ # Validate and format questions
476
+ questions = []
477
+ for i, q in enumerate(parsed_data.get("questions", [])):
478
+ try:
479
+ # Clean multiple choice options
480
+ if q.get("type") == "multiple_choice":
481
+ options = q.get("options", [])
482
+ cleaned_options = []
483
+ correct_answer = None
484
+
485
+ for opt in options:
486
+ # Remove [CORRECT] marker and clean
487
+ opt_text = re.sub(r'\s*\[CORRECT\]\s*', '', opt).strip()
488
+ cleaned_options.append(opt_text)
489
+
490
+ if "[CORRECT]" in opt:
491
+ correct_answer = opt_text
492
+
493
+ # If no correct answer marked, use first
494
+ if not correct_answer and cleaned_options:
495
+ correct_answer = cleaned_options[0]
496
+
497
+ q["options"] = cleaned_options
498
+ q["correct_answer"] = correct_answer or "No correct answer specified"
499
+
500
+ # Generate ID if not present
501
+ if "id" not in q:
502
+ q["id"] = f"q_{hashlib.md5(q.get('question', '').encode()).hexdigest()[:8]}"
503
+
504
+ # Ensure explanation exists
505
+ if "explanation" not in q:
506
+ q["explanation"] = "Based on the source material."
507
+
508
+ # Ensure concept exists
509
+ if "concept" not in q:
510
+ q["concept"] = parsed_data.get("concepts", ["General"])[0] if parsed_data.get("concepts") else "General"
511
+
512
+ questions.append(q)
513
+
514
+ except Exception as e:
515
+ logger.warning(f"Failed to parse question {i}: {e}")
516
+ continue
517
+
518
+ # Ensure we have enough questions
519
+ if len(questions) < num_questions:
520
+ # Create fallback questions
521
+ remaining = num_questions - len(questions)
522
+ for i in range(remaining):
523
+ questions.append({
524
+ "id": f"fallback_{i}",
525
+ "type": "short_answer",
526
+ "question": f"Summarize the main idea of the text.",
527
+ "correct_answer": "The text discusses important concepts that form the foundation of the topic.",
528
+ "explanation": "This is a general summary question based on the provided material.",
529
+ "difficulty": 2,
530
+ "concept": "Summary",
531
+ "is_fallback": True
532
+ })
533
 
534
  # Extract concepts
535
+ concepts = parsed_data.get("concepts", [])
536
+ if not concepts:
537
+ concepts = list(set([q.get("concept", "General") for q in questions]))
538
+ concepts = concepts[:7] # Limit to 7 concepts
539
 
540
+ # Prepare response
541
  response = {
542
  "session_id": session_id,
543
  "concepts": concepts,
544
+ "questions": questions[:num_questions],
545
  "generated_at": datetime.utcnow().isoformat(),
546
+ "processing_time": round(time.time() - start_time, 2),
547
+ "is_cached": False,
548
+ "content_metrics": validation.get("metrics")
549
  }
550
 
551
+ # Cache response
552
+ app.state.cache.set(cache_key, response, ttl=7200) # 2 hours
553
+
554
+ logger.info(f"Generated {len(questions)} questions in {response['processing_time']}s")
555
+
556
  return JSONResponse(response)
557
 
558
  except HTTPException:
559
  raise
560
  except Exception as e:
561
+ logger.error(f"Error generating questions: {e}", exc_info=True)
562
+ raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
563
 
 
564
  @app.post("/api/generate-more")
565
+ async def generate_more_questions(
566
  session_id: str = Form(...),
567
+ concepts: str = Form("[]"),
568
+ focus_areas: str = Form("[]")
569
  ):
570
+ """
571
+ Generate additional follow-up questions
572
+
573
+ - **session_id**: Existing session ID
574
+ - **concepts**: JSON array of concepts to focus on
575
+ - **focus_areas**: JSON array of specific areas to target
576
+ """
577
  try:
578
+ # Parse parameters
579
+ try:
580
+ concepts_list = json.loads(concepts)
581
+ focus_areas_list = json.loads(focus_areas)
582
+ except json.JSONDecodeError:
583
+ concepts_list = []
584
+ focus_areas_list = []
585
+
586
+ # Create prompt for follow-up questions
587
+ concepts_text = ", ".join(concepts_list[:3]) if concepts_list else "general concepts"
588
+
589
+ prompt = f"""Create 3-5 follow-up questions that test deeper understanding.
590
+
591
+ Focus areas: {concepts_text}
592
+ Specific topics: {', '.join(focus_areas_list[:2]) if focus_areas_list else 'any'}
593
+
594
+ Make these questions:
595
+ 1. Application-based (not just recall)
596
+ 2. Require critical thinking
597
+ 3. Connect multiple concepts
598
+ 4. Are challenging but fair
599
+
600
+ Return JSON with questions array."""
601
+
602
+ # Generate questions
603
+ ai_response = await app.state.ai_engine.generate_with_retry(prompt)
604
+ parsed_data = app.state.ai_engine.extract_json_from_text(ai_response)
605
+
606
+ questions = []
607
+ for i, q in enumerate(parsed_data.get("questions", [])):
608
+ q["id"] = f"followup_{i}_{hashlib.md5(str(time.time()).encode()).hexdigest()[:6]}"
609
  q["is_followup"] = True
610
+ questions.append(q)
611
 
612
  return {
613
  "session_id": session_id,
614
  "questions": questions,
615
+ "generated_at": datetime.utcnow().isoformat(),
616
+ "question_count": len(questions)
617
+ }
618
+
619
+ except Exception as e:
620
+ logger.error(f"Error generating more questions: {e}")
621
+ raise HTTPException(status_code=500, detail=str(e))
622
+
623
+ @app.post("/api/validate")
624
+ async def validate_content_endpoint(text: str = Form(...)):
625
+ """Validate content before processing"""
626
+ clean_text = app.state.validator.clean_text(text)
627
+ validation = app.state.validator.validate_content(clean_text)
628
+
629
+ # Estimate question count
630
+ word_count = len(clean_text.split())
631
+ estimated_questions = min(
632
+ config.MAX_QUESTIONS,
633
+ max(3, word_count // 150) # Rough estimate: 1 question per 150 words
634
+ )
635
+
636
+ return {
637
+ **validation,
638
+ "estimated_questions": estimated_questions,
639
+ "clean_text_preview": clean_text[:500] + ("..." if len(clean_text) > 500 else "")
640
+ }
641
+
642
+ @app.post("/api/analyze-concepts")
643
+ async def analyze_concepts(text: str = Form(...)):
644
+ """Analyze text and extract key concepts"""
645
+ prompt = f"""Extract the 5-7 most important concepts from this text:
646
+
647
+ {text[:2000]}
648
+
649
+ Return as JSON array of concept strings."""
650
+
651
+ try:
652
+ ai_response = await app.state.ai_engine.generate_with_retry(prompt, max_tokens=200)
653
+
654
+ # Extract concepts
655
+ concepts_match = re.findall(r'"([^"]+)"', ai_response)
656
+ if not concepts_match:
657
+ concepts_match = re.findall(r"'([^']+)'", ai_response)
658
+
659
+ concepts = list(set(concepts_match))[:7]
660
+
661
+ if not concepts:
662
+ # Fallback: extract significant words
663
+ words = re.findall(r'\b[A-Z][a-z]+\b', text)
664
+ from collections import Counter
665
+ word_counts = Counter(words)
666
+ concepts = [word for word, _ in word_counts.most_common(5)]
667
+
668
+ return {
669
+ "concepts": concepts,
670
+ "concept_count": len(concepts),
671
+ "analyzed_at": datetime.utcnow().isoformat()
672
  }
673
+
674
  except Exception as e:
675
+ logger.error(f"Error analyzing concepts: {e}")
676
+ raise HTTPException(status_code=500, detail=str(e))
677
+
678
+ # ==================== STREAMING ENDPOINT ====================
679
+ @app.post("/api/generate-stream")
680
+ async def generate_stream(text: str = Form(...)):
681
+ """Stream questions as they're generated (experimental)"""
682
+
683
+ async def generate():
684
+ # Generate in chunks
685
+ chunks = app.state.validator.clean_text(text).split('. ')
686
+
687
+ for i, chunk in enumerate(chunks[:5]): # Limit to 5 chunks
688
+ if not chunk.strip():
689
+ continue
690
+
691
+ prompt = f"Create 1-2 questions about: {chunk}"
692
+
693
+ try:
694
+ ai_response = await app.state.ai_engine.generate_with_retry(prompt, max_tokens=300)
695
+ parsed = app.state.ai_engine.extract_json_from_text(ai_response)
696
+
697
+ for q in parsed.get("questions", []):
698
+ q["chunk_index"] = i
699
+ q["total_chunks"] = min(5, len(chunks))
700
+ yield json.dumps({"type": "question", "data": q}) + "\n"
701
+
702
+ except Exception as e:
703
+ yield json.dumps({"type": "error", "message": str(e)}) + "\n"
704
+
705
+ await asyncio.sleep(0.5) # Small delay between chunks
706
+
707
+ yield json.dumps({"type": "complete", "message": "Generation complete"}) + "\n"
708
+
709
+ return StreamingResponse(generate(), media_type="application/x-ndjson")
710
 
711
+ # ==================== ADMIN ENDPOINTS ====================
712
+ @app.get("/admin/stats")
713
+ async def admin_stats():
714
+ """Get admin statistics"""
715
  return {
716
+ "cache_stats": app.state.cache.stats(),
717
+ "ai_engine": {
718
+ "model_health": app.state.ai_engine.model_health,
719
+ "cache_size": len(app.state.ai_engine.cache)
720
+ },
721
+ "rate_limits": {
722
+ "requests_per_hour": config.REQUESTS_PER_HOUR,
723
+ "active_sessions": len(app.state.rate_limiter.requests)
724
+ },
725
+ "system": {
726
+ "timestamp": datetime.now().isoformat(),
727
+ "python_version": os.sys.version,
728
+ "environment": os.getenv("ENVIRONMENT", "production")
729
+ }
730
  }
731
 
732
+ @app.post("/admin/clear-cache")
733
+ async def clear_cache():
734
+ """Clear all caches (admin only)"""
735
+ app.state.cache = SmartCache()
736
+ app.state.ai_engine.cache = {}
737
+ return {"message": "Cache cleared successfully"}
738
+
739
+ # ==================== ERROR HANDLERS ====================
740
+ @app.exception_handler(HTTPException)
741
+ async def http_exception_handler(request, exc):
742
+ return JSONResponse(
743
+ status_code=exc.status_code,
744
+ content={
745
+ "error": exc.detail,
746
+ "status_code": exc.status_code,
747
+ "timestamp": datetime.now().isoformat(),
748
+ "path": request.url.path
749
+ }
750
+ )
751
+
752
+ @app.exception_handler(Exception)
753
+ async def general_exception_handler(request, exc):
754
+ logger.error(f"Unhandled exception: {exc}", exc_info=True)
755
+ return JSONResponse(
756
+ status_code=500,
757
+ content={
758
+ "error": "Internal server error",
759
+ "timestamp": datetime.now().isoformat()
760
+ }
761
+ )
762
+
763
+ # ==================== MAIN EXECUTION ====================
764
  if __name__ == "__main__":
765
  import uvicorn
766
+
767
+ port = int(os.getenv("PORT", 7860)) # Hugging Face Spaces uses 7860
768
+ host = os.getenv("HOST", "0.0.0.0")
769
+
770
+ logger.info(f"Starting server on {host}:{port}")
771
+ uvicorn.run(
772
+ "app:app",
773
+ host=host,
774
+ port=port,
775
+ reload=os.getenv("ENVIRONMENT") == "development",
776
+ log_level="info"
777
+ )