Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,118 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Load
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from langdetect import detect
|
| 4 |
+
import docx
|
| 5 |
+
from pypdf import PdfReader
|
| 6 |
+
|
| 7 |
+
# ========================
|
| 8 |
+
# Load models
|
| 9 |
+
# ========================
|
| 10 |
+
|
| 11 |
+
# English summarizer
|
| 12 |
+
summarizer_en = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 13 |
+
|
| 14 |
+
# Vietnamese summarizer
|
| 15 |
+
summarizer_vi = pipeline("summarization", model="VietAI/vit5-base-vietnamese-summarization")
|
| 16 |
+
|
| 17 |
+
# Quiz generator
|
| 18 |
+
quiz_generator = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 19 |
+
|
| 20 |
+
# ========================
|
| 21 |
+
# Read file
|
| 22 |
+
# ========================
|
| 23 |
+
|
| 24 |
+
def extract_text(file):
|
| 25 |
+
if file.name.endswith(".pdf"):
|
| 26 |
+
reader = PdfReader(file)
|
| 27 |
+
text = ""
|
| 28 |
+
for page in reader.pages:
|
| 29 |
+
if page.extract_text():
|
| 30 |
+
text += page.extract_text() + "\n"
|
| 31 |
+
return text
|
| 32 |
+
|
| 33 |
+
elif file.name.endswith(".docx"):
|
| 34 |
+
doc = docx.Document(file)
|
| 35 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
| 36 |
+
|
| 37 |
+
elif file.name.endswith(".txt"):
|
| 38 |
+
return file.read().decode("utf-8")
|
| 39 |
+
|
| 40 |
+
else:
|
| 41 |
+
return "Unsupported file format"
|
| 42 |
+
|
| 43 |
+
# ========================
|
| 44 |
+
# Main AI function
|
| 45 |
+
# ========================
|
| 46 |
+
|
| 47 |
+
def summarize_and_quiz(file, num_questions):
|
| 48 |
+
|
| 49 |
+
text = extract_text(file)
|
| 50 |
+
|
| 51 |
+
if len(text) < 200:
|
| 52 |
+
return "Văn bản quá ngắn / Text too short", ""
|
| 53 |
+
|
| 54 |
+
# --- Detect language ---
|
| 55 |
+
lang = detect(text)
|
| 56 |
+
|
| 57 |
+
# --- Choose summarizer ---
|
| 58 |
+
if lang == "vi":
|
| 59 |
+
summary = summarizer_vi(
|
| 60 |
+
text,
|
| 61 |
+
max_length=200,
|
| 62 |
+
min_length=80,
|
| 63 |
+
do_sample=False
|
| 64 |
+
)[0]["summary_text"]
|
| 65 |
+
|
| 66 |
+
prompt = f"""
|
| 67 |
+
Tạo {num_questions} câu hỏi trắc nghiệm ngắn gọn dựa vào nội dung sau:
|
| 68 |
+
|
| 69 |
+
{summary}
|
| 70 |
+
|
| 71 |
+
Mỗi câu có đáp án đúng.
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
else:
|
| 75 |
+
summary = summarizer_en(
|
| 76 |
+
text,
|
| 77 |
+
max_length=200,
|
| 78 |
+
min_length=80,
|
| 79 |
+
do_sample=False
|
| 80 |
+
)[0]["summary_text"]
|
| 81 |
+
|
| 82 |
+
prompt = f"""
|
| 83 |
+
Create {num_questions} multiple-choice questions based on the following content:
|
| 84 |
+
|
| 85 |
+
{summary}
|
| 86 |
+
|
| 87 |
+
Each question must include the correct answer.
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
quiz = quiz_generator(prompt, max_length=512)[0]["generated_text"]
|
| 91 |
+
|
| 92 |
+
return summary, quiz
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ========================
|
| 96 |
+
# Gradio Interface
|
| 97 |
+
# ========================
|
| 98 |
+
|
| 99 |
+
interface = gr.Interface(
|
| 100 |
+
fn=summarize_and_quiz,
|
| 101 |
+
|
| 102 |
+
inputs=[
|
| 103 |
+
gr.File(label="📄 Upload PDF / DOCX / TXT"),
|
| 104 |
+
gr.Slider(5, 10, value=5, step=1, label="Số câu hỏi Quiz")
|
| 105 |
+
],
|
| 106 |
+
|
| 107 |
+
outputs=[
|
| 108 |
+
gr.Textbox(label="📌 Tóm tắt nội dung", lines=8),
|
| 109 |
+
gr.Textbox(label="📝 Quiz tự động tạo", lines=12)
|
| 110 |
+
],
|
| 111 |
+
|
| 112 |
+
title="📚 AI TÓM TẮT & TẠO QUIZ (VIỆT + ANH)",
|
| 113 |
+
description="""
|
| 114 |
+
AI tự động nhận diện ngôn ngữ và tóm tắt tài liệu (Việt/Anh), sau đó tạo quiz giúp sinh viên ghi nhớ nhanh.
|
| 115 |
+
"""
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
interface.launch()
|