Spaces:
Runtime error
Runtime error
Fix app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import warnings
|
| 3 |
import torch
|
| 4 |
import gc
|
| 5 |
-
from transformers import AutoModelForVision2Seq, AutoProcessor
|
| 6 |
from PIL import Image
|
| 7 |
import gradio as gr
|
| 8 |
from huggingface_hub import login
|
|
@@ -17,115 +17,106 @@ processor = None
|
|
| 17 |
|
| 18 |
# Clear CUDA cache
|
| 19 |
if torch.cuda.is_available():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
print("เคลียร์ CUDA cache เรียบร้อยแล้ว")
|
| 23 |
|
| 24 |
# Login to Hugging Face Hub
|
| 25 |
if 'HUGGING_FACE_HUB_TOKEN' in os.environ:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
else:
|
| 29 |
-
|
| 30 |
|
| 31 |
def load_model_and_processor():
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
bnb_4bit_use_double_quant=True,
|
| 44 |
-
bnb_4bit_quant_type="nf4",
|
| 45 |
-
bnb_4bit_compute_dtype=torch.bfloat16
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
print("โหลดโมเดลสำเร็จ!")
|
| 63 |
-
return True
|
| 64 |
-
except Exception as e:
|
| 65 |
-
print(f"เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
|
| 66 |
-
return False
|
| 67 |
|
| 68 |
def process_handwriting(image):
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
Only return the transcription in Thai language."""
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
# Initialize application
|
| 116 |
print("กำลังเริ่มต้นแอปพลิเคชัน...")
|
| 117 |
if load_model_and_processor():
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
else:
|
| 131 |
-
|
|
|
|
| 2 |
import warnings
|
| 3 |
import torch
|
| 4 |
import gc
|
| 5 |
+
from transformers import AutoModelForVision2Seq, AutoProcessor
|
| 6 |
from PIL import Image
|
| 7 |
import gradio as gr
|
| 8 |
from huggingface_hub import login
|
|
|
|
| 17 |
|
| 18 |
# Clear CUDA cache
|
| 19 |
if torch.cuda.is_available():
|
| 20 |
+
torch.cuda.empty_cache()
|
| 21 |
+
gc.collect()
|
| 22 |
print("เคลียร์ CUDA cache เรียบร้อยแล้ว")
|
| 23 |
|
| 24 |
# Login to Hugging Face Hub
|
| 25 |
if 'HUGGING_FACE_HUB_TOKEN' in os.environ:
|
| 26 |
+
print("กำลังเข้าสู่ระบบ Hugging Face Hub...")
|
| 27 |
+
login(token=os.environ['HUGGING_FACE_HUB_TOKEN'])
|
| 28 |
else:
|
| 29 |
+
print("คำเตือน: ไม่พบ HUGGING_FACE_HUB_TOKEN")
|
| 30 |
|
| 31 |
def load_model_and_processor():
|
| 32 |
+
"""โหลดโมเดลและ processor"""
|
| 33 |
+
global model, processor
|
| 34 |
+
print("กำลังโหลดโมเดลและ processor...")
|
| 35 |
+
try:
|
| 36 |
+
# Model paths
|
| 37 |
+
base_model_path = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
| 38 |
+
hub_model_path = "Aekanun/thai-handwriting-llm"
|
| 39 |
|
| 40 |
+
# Load processor from base model
|
| 41 |
+
print("กำลังโหลด processor...")
|
| 42 |
+
processor = AutoProcessor.from_pretrained(base_model_path, use_auth_token=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Load model from Hub
|
| 45 |
+
print("กำลังโหลดโมเดลจาก Hub...")
|
| 46 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 47 |
+
hub_model_path,
|
| 48 |
+
device_map="auto",
|
| 49 |
+
torch_dtype=torch.bfloat16,
|
| 50 |
+
trust_remote_code=True,
|
| 51 |
+
use_auth_token=True
|
| 52 |
+
)
|
| 53 |
+
print("โหลดโมเดลสำเร็จ!")
|
| 54 |
+
return True
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
|
| 57 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def process_handwriting(image):
|
| 60 |
+
"""ฟังก์ชันสำหรับ Gradio interface"""
|
| 61 |
+
global model, processor
|
| 62 |
+
|
| 63 |
+
if image is None:
|
| 64 |
+
return "กรุณาอัพโหลดรูปภาพ"
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
# Ensure image is in PIL format
|
| 68 |
+
if not isinstance(image, Image.Image):
|
| 69 |
+
image = Image.fromarray(image)
|
| 70 |
+
|
| 71 |
+
# Create prompt
|
| 72 |
+
prompt = """Transcribe the Thai handwritten text from the provided image.
|
| 73 |
Only return the transcription in Thai language."""
|
| 74 |
+
|
| 75 |
+
# Create model inputs
|
| 76 |
+
messages = [
|
| 77 |
+
{
|
| 78 |
+
"role": "user",
|
| 79 |
+
"content": [
|
| 80 |
+
{"type": "text", "text": prompt},
|
| 81 |
+
{"type": "image", "image": image}
|
| 82 |
+
],
|
| 83 |
+
}
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
# Process with model
|
| 87 |
+
text = processor.apply_chat_template(messages, tokenize=False)
|
| 88 |
+
inputs = processor(text=text, images=image, return_tensors="pt")
|
| 89 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 90 |
+
|
| 91 |
+
# Generate
|
| 92 |
+
with torch.no_grad():
|
| 93 |
+
outputs = model.generate(
|
| 94 |
+
**inputs,
|
| 95 |
+
max_new_tokens=256,
|
| 96 |
+
do_sample=False,
|
| 97 |
+
pad_token_id=processor.tokenizer.pad_token_id
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Decode output
|
| 101 |
+
transcription = processor.decode(outputs[0], skip_special_tokens=True)
|
| 102 |
+
return transcription.strip()
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"เกิดข้อผิดพลาด: {str(e)}"
|
| 105 |
|
| 106 |
# Initialize application
|
| 107 |
print("กำลังเริ่มต้นแอปพลิเคชัน...")
|
| 108 |
if load_model_and_processor():
|
| 109 |
+
# Create Gradio interface
|
| 110 |
+
demo = gr.Interface(
|
| 111 |
+
fn=process_handwriting,
|
| 112 |
+
inputs=gr.Image(type="pil", label="อัพโหลดรูปลายมือเขียนภาษาไทย"),
|
| 113 |
+
outputs=gr.Textbox(label="ข้อความที่แปลงได้"),
|
| 114 |
+
title="Thai Handwriting Recognition",
|
| 115 |
+
description="อัพโหลดรูปภาพลายมือเขียนภาษาไทยเพื่อแปลงเป็นข้อความ",
|
| 116 |
+
examples=[["example1.jpg"], ["example2.jpg"]]
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
demo.launch()
|
| 121 |
else:
|
| 122 |
+
print("ไม่สามารถเริ่มต้นแอปพลิเคชันได้")
|