Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
def generate_questions(pdf_file):
|
| 7 |
+
# Step 1: Extract text
|
| 8 |
+
reader = PdfReader(pdf_file.name)
|
| 9 |
+
text = ""
|
| 10 |
+
for page in reader.pages:
|
| 11 |
+
text += page.extract_text() + "\n"
|
| 12 |
+
|
| 13 |
+
# Step 2: Hugging Face QG model
|
| 14 |
+
qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-qg-prepend")
|
| 15 |
+
questions = qg_pipeline(f"generate questions: {text[:2000]}") # Demo: first 2000 chars
|
| 16 |
+
|
| 17 |
+
# Step 3: Build XML
|
| 18 |
+
generated_questions = []
|
| 19 |
+
for q in questions:
|
| 20 |
+
generated_questions.append({
|
| 21 |
+
"questiontext": q['generated_text'],
|
| 22 |
+
"questiontype": "single_select",
|
| 23 |
+
"marks": 10,
|
| 24 |
+
"options": [
|
| 25 |
+
{"optiontext": "Answer1", "score": "10"},
|
| 26 |
+
{"optiontext": "Answer2", "score": "0"}
|
| 27 |
+
]
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
questiondata_json = json.dumps({
|
| 31 |
+
"title": "Certification Title",
|
| 32 |
+
"totalmarks": "50",
|
| 33 |
+
"time": "20",
|
| 34 |
+
"cutoff": "35",
|
| 35 |
+
"failurl": "",
|
| 36 |
+
"passurl": "",
|
| 37 |
+
"sendpassemail": True,
|
| 38 |
+
"questions": json.dumps({"questions": generated_questions}),
|
| 39 |
+
"maxattempts": 3
|
| 40 |
+
})
|
| 41 |
+
|
| 42 |
+
xml_output = f'<questiondata><![CDATA[{questiondata_json}]]></questiondata>'
|
| 43 |
+
return xml_output
|
| 44 |
+
|
| 45 |
+
# Gradio UI
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=generate_questions,
|
| 48 |
+
inputs=gr.File(file_types=['.pdf']),
|
| 49 |
+
outputs=gr.Textbox(label="Generated XML")
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
iface.launch()
|