phucndh commited on
Commit
8cda735
·
1 Parent(s): 60a41a0

Start test v1

Browse files
Files changed (1) hide show
  1. app.py +67 -11
app.py CHANGED
@@ -1,18 +1,51 @@
 
1
  import gradio as gr
2
  from transformers import T5ForConditionalGeneration, T5Tokenizer
3
  import torch
 
 
 
4
 
5
  # -------------------------
6
- # PHẦN A: Giao diện Upload file tài liệu (ví dụ xử lý file .txt)
7
  # -------------------------
8
  def extract_text(file_obj):
9
  try:
10
- content = file_obj.read().decode("utf-8")
11
- # Lưu nội dung vào file document.txt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  with open("document.txt", "w", encoding="utf-8") as f:
13
  f.write(content)
14
- # Commit và push file document.txt lên repo
15
- # Lưu ý: Điều này sẽ chỉ chạy khi container có quyền Git; Spaces cho phép bạn commit trực tiếp vào repo
16
  repo = Repository(".", clone_from="phucndh/doc-chat-system")
17
  repo.git_add("document.txt")
18
  repo.git_commit("Lưu file tài liệu mới upload")
@@ -23,16 +56,39 @@ def extract_text(file_obj):
23
 
24
  upload_interface = gr.Interface(
25
  fn=extract_text,
26
- inputs=gr.File(label="Tải lên file tài liệu (.txt)"),
27
  outputs=gr.Textbox(label="Nội dung trích xuất"),
28
  title="Upload & Trích xuất Nội dung Tài liệu"
29
  )
30
 
31
  # -------------------------
32
- # PHẦN B: Giao diện Chat sử dụng hình fine-tuned
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # -------------------------
34
  # Nếu chưa có mô hình fine-tuned, tạm thời dùng mô hình gốc T5-small
35
- model_checkpoint = "t5-small"
36
  model = T5ForConditionalGeneration.from_pretrained(model_checkpoint)
37
  tokenizer = T5Tokenizer.from_pretrained(model_checkpoint)
38
 
@@ -55,11 +111,11 @@ chat_interface = gr.Interface(
55
  )
56
 
57
  # -------------------------
58
- # PHẦN C: Kết hợp giao diện thành ứng dụng đa tab
59
  # -------------------------
60
  demo_tabs = gr.TabbedInterface(
61
- [upload_interface, chat_interface],
62
- ["Upload & Trích xuất", "Chat"]
63
  )
64
 
65
  if __name__ == "__main__":
 
1
+ import os
2
  import gradio as gr
3
  from transformers import T5ForConditionalGeneration, T5Tokenizer
4
  import torch
5
+ from huggingface_hub import Repository
6
+ import PyPDF2
7
+ import docx
8
 
9
  # -------------------------
10
+ # PHẦN A: Xử tài liệu từ file upload
11
  # -------------------------
12
  def extract_text(file_obj):
13
  try:
14
+ # Nếu file_obj là file-like, lưu tạm file
15
+ if hasattr(file_obj, "read"):
16
+ file_path = file_obj.name if hasattr(file_obj, "name") else "uploaded_file"
17
+ # Ghi file upload vào đĩa
18
+ with open(file_path, "wb") as f:
19
+ f.write(file_obj.read())
20
+ else:
21
+ file_path = file_obj # nếu không, coi như đây là file path
22
+
23
+ ext = os.path.splitext(file_path)[1].lower()
24
+ content = ""
25
+ if ext == ".txt":
26
+ with open(file_path, "r", encoding="utf-8") as f:
27
+ content = f.read()
28
+ elif ext == ".pdf":
29
+ with open(file_path, "rb") as f:
30
+ reader = PyPDF2.PdfReader(f)
31
+ for page in reader.pages:
32
+ text = page.extract_text()
33
+ if text:
34
+ content += text + "\n"
35
+ elif ext in [".doc", ".docx"]:
36
+ doc = docx.Document(file_path)
37
+ for para in doc.paragraphs:
38
+ content += para.text + "\n"
39
+ else:
40
+ # Nếu không nhận diện được, cố gắng đọc dưới dạng văn bản
41
+ with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
42
+ content = f.read()
43
+
44
+ # Lưu nội dung vào file document.txt để Colab có thể sử dụng
45
  with open("document.txt", "w", encoding="utf-8") as f:
46
  f.write(content)
47
+
48
+ # Commit push file document.txt lên repo (Spaces cho phép commit trực tiếp)
49
  repo = Repository(".", clone_from="phucndh/doc-chat-system")
50
  repo.git_add("document.txt")
51
  repo.git_commit("Lưu file tài liệu mới upload")
 
56
 
57
  upload_interface = gr.Interface(
58
  fn=extract_text,
59
+ inputs=gr.File(label="Tải lên file tài liệu (.pdf, .doc, .docx, .txt)"),
60
  outputs=gr.Textbox(label="Nội dung trích xuất"),
61
  title="Upload & Trích xuất Nội dung Tài liệu"
62
  )
63
 
64
  # -------------------------
65
+ # PHẦN B: Xử tài liệu từ nhập trực tiếp
66
+ # -------------------------
67
+ def process_text(text_input):
68
+ # Bạn có thể thêm bước xử lý, ví dụ: loại bỏ khoảng trắng thừa
69
+ content = text_input.strip()
70
+ # Lưu nội dung vào file document.txt
71
+ with open("document.txt", "w", encoding="utf-8") as f:
72
+ f.write(content)
73
+ # Commit và push file document.txt lên repo
74
+ repo = Repository(".", clone_from="phucndh/doc-chat-system")
75
+ repo.git_add("document.txt")
76
+ repo.git_commit("Lưu văn bản nhập trực tiếp")
77
+ repo.git_push()
78
+ return content
79
+
80
+ direct_text_interface = gr.Interface(
81
+ fn=process_text,
82
+ inputs=gr.Textbox(lines=10, placeholder="Nhập trực tiếp văn bản tài liệu của bạn ở đây..."),
83
+ outputs=gr.Textbox(label="Nội dung trích xuất"),
84
+ title="Nhập trực tiếp văn bản tài liệu"
85
+ )
86
+
87
+ # -------------------------
88
+ # PHẦN C: Giao diện Chat sử dụng mô hình fine-tuned
89
  # -------------------------
90
  # Nếu chưa có mô hình fine-tuned, tạm thời dùng mô hình gốc T5-small
91
+ model_checkpoint = "t5-small"
92
  model = T5ForConditionalGeneration.from_pretrained(model_checkpoint)
93
  tokenizer = T5Tokenizer.from_pretrained(model_checkpoint)
94
 
 
111
  )
112
 
113
  # -------------------------
114
+ # PHẦN D: Kết hợp giao diện thành ứng dụng đa tab
115
  # -------------------------
116
  demo_tabs = gr.TabbedInterface(
117
+ [upload_interface, direct_text_interface, chat_interface],
118
+ ["Upload & Trích xuất", "Nhập trực tiếp", "Chat"]
119
  )
120
 
121
  if __name__ == "__main__":