eoeooe commited on
Commit
1ee2fbb
·
verified ·
1 Parent(s): 48df4d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -4,41 +4,42 @@ import gradio as gr
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
  import torch
6
 
7
- # โหลดโมเดล transformers ภาษาไทย สำหรับ text classification (ตัวอย่างใช้ WangchanBERT)
8
  model_name = "airesearch/wangchanberta-base-att-spm-uncased"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
 
12
- # ฟังก์ชัน OCR + วิเคราะห์ข้อความ
13
  def ocr_and_classify(image):
14
- # OCR อ่านข้อความจากภาพ (ตั้งภาษาไทย + อังกฤษ)
15
  text = pytesseract.image_to_string(image, lang="tha+eng")
16
 
17
  if not text.strip():
18
  return "❌ ไม่พบข้อความในภาพ"
19
 
20
- # เตรียม input สำหรับ transformers
21
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
22
  outputs = model(**inputs)
23
 
24
- # แปลง logits เป็น probabilities และเลือกคลาสที่มีคะแนนสูงสุด
25
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
26
  pred_class_idx = torch.argmax(probs).item()
27
  confidence = probs[0][pred_class_idx].item()
28
 
29
- # สำหรับ WangchanBERT ไม่มี label mapping โดยตรง ต้องมี mapping label เอง (สมมติมี 3 คลาส)
30
- labels = ["คลาส 0", "คลาส 1", "คลาส 2"] # แก้ตามโมเดลที่ใช้จริง
31
-
32
- result = f"ข้อความที่อ่านได้:\n{text}\n\nการจำแนกข้อความ:\n{labels[pred_class_idx]} (ความมั่นใจ {confidence:.2%})"
 
 
 
33
  return result
34
 
35
- # สร้าง Gradio UI
36
  iface = gr.Interface(
37
  fn=ocr_and_classify,
38
  inputs=gr.Image(type="pil", label="อัปโหลดภาพสลิปหรือใบเสร็จ"),
39
  outputs=gr.Textbox(lines=15, label="ผลลัพธ์"),
40
  title="OCR + วิเคราะห์ข้อความสลิป ด้วย pytesseract + Transformers",
41
- description="อ่านข้อความด้วย pytesseract แล้วใช้โมเดล transformers วิเคราะห์ข้อความ"
42
  )
43
 
44
  if __name__ == "__main__":
 
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
  import torch
6
 
7
+ # โหลดโมเดล transformers ภาษาไทย (WangchanBERT) พร้อมตั้ง use_fast=False
8
  model_name = "airesearch/wangchanberta-base-att-spm-uncased"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
10
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
 
 
12
  def ocr_and_classify(image):
13
+ # OCR อ่านข้อความจากภาพ ภาษาไทย+อังกฤษ
14
  text = pytesseract.image_to_string(image, lang="tha+eng")
15
 
16
  if not text.strip():
17
  return "❌ ไม่พบข้อความในภาพ"
18
 
19
+ # Tokenize ข้อความเพื่อส่งเข้าโมเดล
20
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
21
  outputs = model(**inputs)
22
 
23
+ # แปลง logits เป็น probabilities และหา class ที่มีคะแนนสูงสุด
24
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
25
  pred_class_idx = torch.argmax(probs).item()
26
  confidence = probs[0][pred_class_idx].item()
27
 
28
+ # กำหนดชื่อคลาสเอง (ต้องแก้ตามโมเดลจริงที่ใช้)
29
+ labels = ["คลาส 0", "คลาส 1", "คลาส 2"] # ตัวอย่าง
30
+
31
+ result = (
32
+ f"📄 ข้อความที่อ่านได้:\n{text}\n\n"
33
+ f"📝 การจำแนกข้อความ:\n{labels[pred_class_idx]} (ความมั่นใจ {confidence:.2%})"
34
+ )
35
  return result
36
 
 
37
  iface = gr.Interface(
38
  fn=ocr_and_classify,
39
  inputs=gr.Image(type="pil", label="อัปโหลดภาพสลิปหรือใบเสร็จ"),
40
  outputs=gr.Textbox(lines=15, label="ผลลัพธ์"),
41
  title="OCR + วิเคราะห์ข้อความสลิป ด้วย pytesseract + Transformers",
42
+ description="อ่านข้อความจากภาพด้วย pytesseract แล้วใช้โมเดล transformers วิเคราะห์ข้อความ"
43
  )
44
 
45
  if __name__ == "__main__":