Spaces:
Sleeping
Sleeping
Update app.py
Browse files38 unmodified lines3940414242434445464748495031 unmodified lines82838485868788899091928793949596979899100101102103104941051061071081091101111121131141151161171181041191201211221231241251262 unmodified lines12913013113213313413513613711813813914014114214314414514614714814915015112615215315415538 unmodified lines chunks.append(' '.join(words[i:i+chunk_size])) return chunks
def generate_mcqs_from_chunk(chunk: str, api_key: str) -> List[List[str]]:def generate_mcqs_from_chunk(chunk: str, api_key: str, chunk_number: int = 1) -> List[List[str]]: """Generate MCQs from a text chunk using Gemini API""" print(f"\n=== PROCESSING CHUNK {chunk_number} ===") print(f"Chunk length: {len(chunk)} characters") print(f"Chunk preview: {chunk[:200]}...") models_to_try = [ 'gemini-2.0-flash-exp', 'gemini-1.5-flash',31 unmodified lines for model_name in models_to_try: try: print(f"Trying model: {model_name}") model = genai.GenerativeModel(model_name) response = model.generate_content(prompt) if response.text: print(f"✅ Successfully used model: {model_name}") break except Exception as e: print(f"Error with {model_name}: {e}") print(f"❌ Error with {model_name}: {e}") continue if response and response.text: output = response.text.strip() print(f"\n--- RAW AI RESPONSE FOR CHUNK {chunk_number} ---") print(output) print("--- END RAW RESPONSE ---\n") lines = [line.strip() for line in output.splitlines() if line.strip()] print(f"Total non-empty lines in response: {len(lines)}") for line in lines: for idx, line in enumerate(lines): print(f"Processing line {idx + 1}: {line[:100]}...") # Skip any header lines that might still appear if ('Question' in line and 'OptionA' in line and 'OptionB' in line) or line.startswith('Question,'): print(f"❌ Skipped header line: {line[:50]}...") continue # Skip empty lines or lines that don't look like MCQs if not line or line.count(',') < 5: print(f"❌ Skipped invalid line (comma count: {line.count(',')}): {line[:50]}...") continue # Parse CSV line using proper CSV parsing import csv try: # Use StringIO to parse the line as CSV csv_reader = csv.reader([line]) parts = next(csv_reader) print(f"Parsed parts: {len(parts)} fields") # Ensure we have exactly 6 parts and the question is not empty if len(parts) >= 6 and parts[0].strip() and not parts[0].lower().startswith('question'):2 unmodified lines # Validate that correct answer is A, B, C, or D if cleaned_parts[5].upper() in ['A', 'B', 'C', 'D']: mcq_data.append(cleaned_parts) print(f"✅ Added MCQ: {cleaned_parts[0][:50]}... (Answer: {cleaned_parts[5]})") else: print(f"❌ Invalid answer format: {cleaned_parts[5]}") else: print(f"❌ Invalid parts count or empty question. Parts: {len(parts)}, First part: '{parts[0] if parts else 'N/A'}'") except csv.Error: except csv.Error as e: print(f"❌ CSV parsing error: {e}") # Fallback to simple split if CSV parsing fails parts = line.split(',') if len(parts) >= 6 and parts[0].strip() and not parts[0].lower().startswith('question'): cleaned_parts = [part.strip().strip('"') for part in parts[:6]] if cleaned_parts[5].upper() in ['A', 'B', 'C', 'D']: mcq_data.append(cleaned_parts) print(f"✅ Added MCQ (fallback): {cleaned_parts[0][:50]}...") else: print(f"❌ Invalid answer format (fallback): {cleaned_parts[5]}") else: print(f"❌ No response received for chunk {chunk_number}") print(f"Generated {len(mcq_data)} MCQs from chunk") print(f"Generated {len(mcq_data)} MCQs from chunk {chunk_number}") return mcq_data
def process_pdf_to_mcqs(pdf_file, api_key: str, chunk_size: int = 1500, progress=gr.Progress()) -> Tuple[str, str]:app.py+32-6