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