Update app.py
Browse files
app.py
CHANGED
|
@@ -20,14 +20,16 @@ quiz_generator = pipeline(
|
|
| 20 |
|
| 21 |
def extract_text(file):
|
| 22 |
if file.name.endswith(".pdf"):
|
| 23 |
-
reader = PdfReader(file)
|
| 24 |
text = ""
|
| 25 |
for page in reader.pages:
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
return text
|
| 28 |
|
| 29 |
elif file.name.endswith(".docx"):
|
| 30 |
-
doc = docx.Document(file)
|
| 31 |
return "\n".join([para.text for para in doc.paragraphs])
|
| 32 |
|
| 33 |
elif file.name.endswith(".txt"):
|
|
@@ -36,27 +38,33 @@ def extract_text(file):
|
|
| 36 |
else:
|
| 37 |
return "Định dạng file chưa hỗ trợ."
|
| 38 |
|
|
|
|
| 39 |
# ========================
|
| 40 |
# AI Functions
|
| 41 |
# ========================
|
| 42 |
|
| 43 |
def summarize_and_quiz(file, num_questions):
|
|
|
|
| 44 |
|
| 45 |
text = extract_text(file)
|
|
|
|
| 46 |
|
| 47 |
if len(text) < 100:
|
| 48 |
-
return "Văn bản quá ngắn.", ""
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# --- TẠO QUIZ ---
|
| 54 |
prompt = f"""
|
| 55 |
Tạo {num_questions} câu hỏi trắc nghiệm dựa trên nội dung sau:
|
| 56 |
|
| 57 |
{summary}
|
| 58 |
|
| 59 |
-
|
| 60 |
"""
|
| 61 |
|
| 62 |
quiz = quiz_generator(prompt, max_length=512)[0]["generated_text"]
|
|
@@ -64,6 +72,7 @@ def summarize_and_quiz(file, num_questions):
|
|
| 64 |
return summary, quiz
|
| 65 |
|
| 66 |
|
|
|
|
| 67 |
# ========================
|
| 68 |
# Gradio Interface
|
| 69 |
# ========================
|
|
|
|
| 20 |
|
| 21 |
def extract_text(file):
|
| 22 |
if file.name.endswith(".pdf"):
|
| 23 |
+
reader = PdfReader(file.name)
|
| 24 |
text = ""
|
| 25 |
for page in reader.pages:
|
| 26 |
+
page_text = page.extract_text()
|
| 27 |
+
if page_text:
|
| 28 |
+
text += page_text + "\n"
|
| 29 |
return text
|
| 30 |
|
| 31 |
elif file.name.endswith(".docx"):
|
| 32 |
+
doc = docx.Document(file.name)
|
| 33 |
return "\n".join([para.text for para in doc.paragraphs])
|
| 34 |
|
| 35 |
elif file.name.endswith(".txt"):
|
|
|
|
| 38 |
else:
|
| 39 |
return "Định dạng file chưa hỗ trợ."
|
| 40 |
|
| 41 |
+
|
| 42 |
# ========================
|
| 43 |
# AI Functions
|
| 44 |
# ========================
|
| 45 |
|
| 46 |
def summarize_and_quiz(file, num_questions):
|
| 47 |
+
num_questions = int(num_questions)
|
| 48 |
|
| 49 |
text = extract_text(file)
|
| 50 |
+
text = text[:3000]
|
| 51 |
|
| 52 |
if len(text) < 100:
|
| 53 |
+
return "Văn bản quá ngắn hoặc không đọc được.", ""
|
| 54 |
|
| 55 |
+
summary = summarizer(
|
| 56 |
+
text,
|
| 57 |
+
max_length=200,
|
| 58 |
+
min_length=80,
|
| 59 |
+
do_sample=False
|
| 60 |
+
)[0]['summary_text']
|
| 61 |
|
|
|
|
| 62 |
prompt = f"""
|
| 63 |
Tạo {num_questions} câu hỏi trắc nghiệm dựa trên nội dung sau:
|
| 64 |
|
| 65 |
{summary}
|
| 66 |
|
| 67 |
+
Mỗi câu có 4 lựa chọn A,B,C,D và ghi rõ đáp án đúng.
|
| 68 |
"""
|
| 69 |
|
| 70 |
quiz = quiz_generator(prompt, max_length=512)[0]["generated_text"]
|
|
|
|
| 72 |
return summary, quiz
|
| 73 |
|
| 74 |
|
| 75 |
+
|
| 76 |
# ========================
|
| 77 |
# Gradio Interface
|
| 78 |
# ========================
|