Spaces:
Running
on
Zero
Running
on
Zero
fixing
Browse files- app.py +16 -6
- config.json +9 -0
app.py
CHANGED
@@ -24,10 +24,11 @@ def load_model_and_processor():
|
|
24 |
print("กำลังโหลดโมเดลและ processor...")
|
25 |
|
26 |
try:
|
|
|
27 |
base_model_path = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
28 |
hub_model_path = "Aekanun/thai-handwriting-llm"
|
29 |
|
30 |
-
# ตั้งค่า BitsAndBytes
|
31 |
bnb_config = BitsAndBytesConfig(
|
32 |
load_in_4bit=True,
|
33 |
bnb_4bit_use_double_quant=True,
|
@@ -35,19 +36,21 @@ def load_model_and_processor():
|
|
35 |
bnb_4bit_compute_dtype=torch.bfloat16
|
36 |
)
|
37 |
|
38 |
-
# โหลด processor
|
|
|
39 |
processor = AutoProcessor.from_pretrained(base_model_path)
|
40 |
|
41 |
-
# โหลดโมเดลจาก Hub
|
42 |
-
print("
|
43 |
model = AutoModelForVision2Seq.from_pretrained(
|
44 |
hub_model_path,
|
45 |
device_map="auto",
|
46 |
torch_dtype=torch.bfloat16,
|
47 |
quantization_config=bnb_config,
|
48 |
-
trust_remote_code=True
|
|
|
49 |
)
|
50 |
-
print("
|
51 |
|
52 |
return True
|
53 |
except Exception as e:
|
@@ -55,13 +58,18 @@ def load_model_and_processor():
|
|
55 |
return False
|
56 |
|
57 |
def process_handwriting(image):
|
|
|
|
|
|
|
58 |
if image is None:
|
59 |
return "กรุณาอัพโหลดรูปภาพ"
|
60 |
|
61 |
try:
|
|
|
62 |
if not isinstance(image, Image.Image):
|
63 |
image = Image.fromarray(image)
|
64 |
|
|
|
65 |
if image.mode != "RGB":
|
66 |
image = image.convert("RGB")
|
67 |
|
@@ -96,8 +104,10 @@ Only return the transcription in Thai language."""
|
|
96 |
except Exception as e:
|
97 |
return f"เกิดข้อผิดพลาด: {str(e)}"
|
98 |
|
|
|
99 |
print("กำลังเริ่มต้นแอปพลิเคชัน...")
|
100 |
if load_model_and_processor():
|
|
|
101 |
demo = gr.Interface(
|
102 |
fn=process_handwriting,
|
103 |
inputs=gr.Image(type="pil", label="อัพโหลดรูปลายมือเขียนภาษาไทย"),
|
|
|
24 |
print("กำลังโหลดโมเดลและ processor...")
|
25 |
|
26 |
try:
|
27 |
+
# กำหนด paths
|
28 |
base_model_path = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
29 |
hub_model_path = "Aekanun/thai-handwriting-llm"
|
30 |
|
31 |
+
# ตั้งค่า BitsAndBytes
|
32 |
bnb_config = BitsAndBytesConfig(
|
33 |
load_in_4bit=True,
|
34 |
bnb_4bit_use_double_quant=True,
|
|
|
36 |
bnb_4bit_compute_dtype=torch.bfloat16
|
37 |
)
|
38 |
|
39 |
+
# โหลด processor จาก base model
|
40 |
+
print("Loading processor...")
|
41 |
processor = AutoProcessor.from_pretrained(base_model_path)
|
42 |
|
43 |
+
# โหลดโมเดลจาก Hub
|
44 |
+
print("Loading model...")
|
45 |
model = AutoModelForVision2Seq.from_pretrained(
|
46 |
hub_model_path,
|
47 |
device_map="auto",
|
48 |
torch_dtype=torch.bfloat16,
|
49 |
quantization_config=bnb_config,
|
50 |
+
trust_remote_code=True,
|
51 |
+
force_download=True # เพิ่มมาเพื่อให้โหลดใหม่
|
52 |
)
|
53 |
+
print("Model loaded successfully!")
|
54 |
|
55 |
return True
|
56 |
except Exception as e:
|
|
|
58 |
return False
|
59 |
|
60 |
def process_handwriting(image):
|
61 |
+
"""ฟังก์ชันสำหรับ Gradio interface"""
|
62 |
+
global model, processor
|
63 |
+
|
64 |
if image is None:
|
65 |
return "กรุณาอัพโหลดรูปภาพ"
|
66 |
|
67 |
try:
|
68 |
+
# Ensure image is in PIL format
|
69 |
if not isinstance(image, Image.Image):
|
70 |
image = Image.fromarray(image)
|
71 |
|
72 |
+
# Convert to RGB if needed
|
73 |
if image.mode != "RGB":
|
74 |
image = image.convert("RGB")
|
75 |
|
|
|
104 |
except Exception as e:
|
105 |
return f"เกิดข้อผิดพลาด: {str(e)}"
|
106 |
|
107 |
+
# Initialize application
|
108 |
print("กำลังเริ่มต้นแอปพลิเคชัน...")
|
109 |
if load_model_and_processor():
|
110 |
+
# Create Gradio interface
|
111 |
demo = gr.Interface(
|
112 |
fn=process_handwriting,
|
113 |
inputs=gr.Image(type="pil", label="อัพโหลดรูปลายมือเขียนภาษาไทย"),
|
config.json
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": ["LlamaForCausalLM"],
|
3 |
+
"model_type": "llama",
|
4 |
+
"tokenizer_class": "PreTrainedTokenizerFast",
|
5 |
+
"model_max_length": 131072,
|
6 |
+
"megatron_core": "megatron.core",
|
7 |
+
"task_type": "CAUSAL_LM",
|
8 |
+
"target_modules": ["q_proj", "v_proj"]
|
9 |
+
}
|