Wewoo commited on
Commit
fbcebb2
·
verified ·
1 Parent(s): 9616f23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -51
app.py CHANGED
@@ -1,78 +1,68 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import docx
4
- from pypdf import PdfReader
5
 
6
  # ========================
7
- # Load models
8
  # ========================
9
 
10
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
 
 
11
 
12
  quiz_generator = pipeline(
13
  "text2text-generation",
14
- model="google/flan-t5-base"
15
  )
16
 
17
- # ========================
18
- # Read file
19
- # ========================
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"):
36
- return file.read().decode("utf-8")
37
-
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"]
 
 
 
 
 
 
71
 
72
  return summary, quiz
73
 
74
 
75
-
76
  # ========================
77
  # Gradio Interface
78
  # ========================
@@ -81,19 +71,26 @@ interface = gr.Interface(
81
  fn=summarize_and_quiz,
82
 
83
  inputs=[
84
- gr.File(label="Upload PDF / Word / Text"),
 
 
 
 
85
  gr.Slider(5, 10, value=5, step=1, label="Số câu hỏi (Quiz)")
86
  ],
87
 
88
  outputs=[
89
- gr.Textbox(label="📌 Nội dung tóm tắt"),
90
- gr.Textbox(label="📝 Câu hỏi ôn tập (Quiz)", lines=10)
91
  ],
92
 
93
  title="📚 AI TÓM TẮT & TẠO QUIZ CHO SINH VIÊN",
94
  description="""
95
- Ứng dụng AI tóm tắt văn bản học thuật và tự động tạo câu hỏi ôn tập (5–10 câu)
96
- không làm mất nội dung chính.
 
 
 
97
  """
98
  )
99
 
 
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
  # ========================
5
+ # Load models (tối ưu cho HF Free + đa ngôn ngữ)
6
  # ========================
7
 
8
+ summarizer = pipeline(
9
+ "summarization",
10
+ model="csebuetnlp/mT5_multilingual_XLSum"
11
+ )
12
 
13
  quiz_generator = pipeline(
14
  "text2text-generation",
15
+ model="google/mt5-small"
16
  )
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # ========================
19
  # AI Functions
20
  # ========================
21
 
22
+ def summarize_and_quiz(text, num_questions):
 
23
 
24
+ num_questions = int(num_questions)
 
25
 
26
+ if not text or len(text) < 100:
27
+ return "Văn bản quá ngắn. Hãy nhập ít nhất 100 ký tự.", ""
28
 
29
+ # Cắt bớt để tránh quá giới hạn model
30
+ text = text[:3000]
 
 
 
 
31
 
32
+ # --- TÓM TẮT ---
33
+ try:
34
+ summary = summarizer(
35
+ text,
36
+ max_length=200,
37
+ min_length=80,
38
+ do_sample=False
39
+ )[0]['summary_text']
40
+ except:
41
+ summary = "Có lỗi xảy ra trong quá trình tóm tắt."
42
+
43
+ # --- TẠO QUIZ ---
44
  prompt = f"""
45
+ Hãy tạo {num_questions} câu hỏi trắc nghiệm dựa trên nội dung sau:
46
 
47
  {summary}
48
 
49
+ Yêu cầu:
50
+ - Mỗi câu có 4 lựa chọn: A, B, C, D
51
+ - Có đáp án đúng
52
+ - Ngắn gọn, rõ ràng
53
  """
54
 
55
+ try:
56
+ quiz = quiz_generator(
57
+ prompt,
58
+ max_length=512
59
+ )[0]["generated_text"]
60
+ except:
61
+ quiz = "Có lỗi xảy ra trong quá trình tạo câu hỏi."
62
 
63
  return summary, quiz
64
 
65
 
 
66
  # ========================
67
  # Gradio Interface
68
  # ========================
 
71
  fn=summarize_and_quiz,
72
 
73
  inputs=[
74
+ gr.Textbox(
75
+ label="📄 Nhập văn bản (Tiếng Việt hoặc Tiếng Anh)",
76
+ placeholder="Dán nội dung bài học / tài liệu / văn bản cần tóm tắt vào đây...",
77
+ lines=15
78
+ ),
79
  gr.Slider(5, 10, value=5, step=1, label="Số câu hỏi (Quiz)")
80
  ],
81
 
82
  outputs=[
83
+ gr.Textbox(label="📌 Nội dung tóm tắt", lines=8),
84
+ gr.Textbox(label="📝 Câu hỏi ôn tập (Quiz)", lines=12)
85
  ],
86
 
87
  title="📚 AI TÓM TẮT & TẠO QUIZ CHO SINH VIÊN",
88
  description="""
89
+ Hỗ trợ: Tiếng Việt + Tiếng Anh
90
+ Tóm tắt không làm mất ý chính
91
+ ✅ Tạo 5–10 câu hỏi ôn tập ngay lập tức
92
+
93
+ Chỉ cần dán nội dung vào và nhấn Submit.
94
  """
95
  )
96