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