Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,9 @@ import pandas as pd
|
|
| 3 |
from pdf2image import convert_from_path
|
| 4 |
import pytesseract
|
| 5 |
import google.generativeai as genai
|
| 6 |
-
|
| 7 |
|
| 8 |
-
# Function: Extract text from PDF
|
| 9 |
def extract_text_from_pdf(pdf_file):
|
| 10 |
pages = convert_from_path(pdf_file)
|
| 11 |
all_text = ""
|
|
@@ -70,7 +70,7 @@ def generate_mcqs(text, api_key):
|
|
| 70 |
return None, None
|
| 71 |
|
| 72 |
df = pd.DataFrame(filtered_mcq_data, columns=["Question", "OptionA", "OptionB", "OptionC", "OptionD", "CorrectAnswer"])
|
| 73 |
-
return df, df.head(10).
|
| 74 |
|
| 75 |
# Gradio pipeline
|
| 76 |
def process_pdf(pdf_file, api_key):
|
|
@@ -84,12 +84,11 @@ def process_pdf(pdf_file, api_key):
|
|
| 84 |
if df is None:
|
| 85 |
return "❌ No valid MCQs generated.", None
|
| 86 |
|
| 87 |
-
# Save to Excel
|
| 88 |
-
|
| 89 |
-
df.to_excel(
|
| 90 |
-
output_file.seek(0)
|
| 91 |
|
| 92 |
-
return preview,
|
| 93 |
except Exception as e:
|
| 94 |
return f"Error: {str(e)}", None
|
| 95 |
|
|
|
|
| 3 |
from pdf2image import convert_from_path
|
| 4 |
import pytesseract
|
| 5 |
import google.generativeai as genai
|
| 6 |
+
import tempfile
|
| 7 |
|
| 8 |
+
# Function: Extract text from PDF using OCR
|
| 9 |
def extract_text_from_pdf(pdf_file):
|
| 10 |
pages = convert_from_path(pdf_file)
|
| 11 |
all_text = ""
|
|
|
|
| 70 |
return None, None
|
| 71 |
|
| 72 |
df = pd.DataFrame(filtered_mcq_data, columns=["Question", "OptionA", "OptionB", "OptionC", "OptionD", "CorrectAnswer"])
|
| 73 |
+
return df, df.head(10).to_string(index=False) # Preview as plain table
|
| 74 |
|
| 75 |
# Gradio pipeline
|
| 76 |
def process_pdf(pdf_file, api_key):
|
|
|
|
| 84 |
if df is None:
|
| 85 |
return "❌ No valid MCQs generated.", None
|
| 86 |
|
| 87 |
+
# Save to a temporary Excel file
|
| 88 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 89 |
+
df.to_excel(tmp_file.name, index=False)
|
|
|
|
| 90 |
|
| 91 |
+
return preview, tmp_file.name
|
| 92 |
except Exception as e:
|
| 93 |
return f"Error: {str(e)}", None
|
| 94 |
|