eoeooe commited on
Commit
6b549b4
·
verified ·
1 Parent(s): 1ee2fbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -4,42 +4,46 @@ import gradio as gr
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__":
 
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
  import torch
6
 
7
+ # โมเดลนี้โหลดด้จริง + พร้อมใช้งาน
8
+ model_name = "thainlp/bert-base-thai-snips"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
 
12
+ # Label map จากโมเดล (SNIPS dataset มี intent เช่น สั่งอาหาร, ตรวจอากาศ)
13
+ labels = [
14
+ "AddToPlaylist", "BookRestaurant", "GetWeather",
15
+ "PlayMusic", "RateBook", "SearchCreativeWork",
16
+ "SearchScreeningEvent"
17
+ ]
18
+
19
  def ocr_and_classify(image):
20
+ # OCR อ่านข้อความไทย + อังกฤษ
21
  text = pytesseract.image_to_string(image, lang="tha+eng")
22
 
23
  if not text.strip():
24
  return "❌ ไม่พบข้อความในภาพ"
25
+
26
+ # วิเคราะห์ข้อความด้วย BERT
27
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
28
  outputs = model(**inputs)
29
+
 
30
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
31
  pred_class_idx = torch.argmax(probs).item()
32
  confidence = probs[0][pred_class_idx].item()
33
 
 
 
 
34
  result = (
35
  f"📄 ข้อความที่อ่านได้:\n{text}\n\n"
36
  f"📝 การจำแนกข้อความ:\n{labels[pred_class_idx]} (ความมั่นใจ {confidence:.2%})"
37
  )
38
  return result
39
 
40
+ # Gradio UI
41
  iface = gr.Interface(
42
  fn=ocr_and_classify,
43
+ inputs=gr.Image(type="pil", label="อัปโหลดภาพ"),
44
  outputs=gr.Textbox(lines=15, label="ผลลัพธ์"),
45
+ title="OCR + วิเคราะห์ข้อความด้วย BERT (Thai)",
46
+ description="อ่านข้อความจากภาพด้วย pytesseract แล้ววิเคราะห์้วย BERT ที่รองรับภษาไทย"
47
  )
48
 
49
  if __name__ == "__main__":