Spaces:
Runtime error
Runtime error
fixed and run app.py
Browse files- app.py +91 -35
- app.py.origi +41 -0
app.py
CHANGED
|
@@ -1,41 +1,97 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'huggingface_hub'])
|
| 9 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'transformers'])
|
| 10 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch', '--index-url', 'https://download.pytorch.org/whl/cpu'])
|
| 11 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio'])
|
| 12 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'Pillow'])
|
| 13 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'bitsandbytes'])
|
| 14 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'accelerate'])
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
demo = gr.Interface(
|
| 31 |
-
fn=process_handwriting,
|
| 32 |
-
inputs=gr.Image(type="pil", label="อัพโหลดรูปภาพ"),
|
| 33 |
-
outputs=gr.Textbox(label="ผลลัพธ์"),
|
| 34 |
-
title="Test Installation",
|
| 35 |
-
description="ทดสอบการติดตั้ง libraries"
|
| 36 |
)
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForVision2Seq, AutoProcessor, BitsAndBytesConfig
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
# Global variables for model and processor
|
| 7 |
+
model = None
|
| 8 |
+
processor = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def load_model_and_processor():
|
| 11 |
+
global model, processor
|
| 12 |
try:
|
| 13 |
+
model_path = "Aekanun/thai-handwriting-llm"
|
| 14 |
+
base_model_path = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
| 15 |
+
|
| 16 |
+
print("Loading processor...")
|
| 17 |
+
processor = AutoProcessor.from_pretrained(base_model_path)
|
| 18 |
+
|
| 19 |
+
print("Loading model...")
|
| 20 |
+
bnb_config = BitsAndBytesConfig(
|
| 21 |
+
load_in_4bit=True,
|
| 22 |
+
bnb_4bit_use_double_quant=True,
|
| 23 |
+
bnb_4bit_quant_type="nf4",
|
| 24 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 28 |
+
model_path,
|
| 29 |
+
device_map="auto",
|
| 30 |
+
torch_dtype=torch.bfloat16,
|
| 31 |
+
quantization_config=bnb_config
|
| 32 |
+
)
|
| 33 |
+
return True
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error loading model: {str(e)}")
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
+
def process_handwriting(image):
|
| 39 |
+
global model, processor
|
| 40 |
+
|
| 41 |
+
if image is None:
|
| 42 |
+
return "กรุณาอัพโหลดรูปภาพ"
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
if not isinstance(image, Image.Image):
|
| 46 |
+
image = Image.fromarray(image)
|
| 47 |
+
|
| 48 |
+
prompt = """Transcribe the Thai handwritten text from the provided image.
|
| 49 |
+
Only return the transcription in Thai language."""
|
| 50 |
+
|
| 51 |
+
messages = [
|
| 52 |
+
{
|
| 53 |
+
"role": "user",
|
| 54 |
+
"content": [
|
| 55 |
+
{"type": "text", "text": prompt},
|
| 56 |
+
{"type": "image", "image": image}
|
| 57 |
+
],
|
| 58 |
+
}
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
text = processor.apply_chat_template(messages, tokenize=False)
|
| 62 |
+
inputs = processor(text=text, images=image, return_tensors="pt")
|
| 63 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 64 |
+
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
outputs = model.generate(
|
| 67 |
+
**inputs,
|
| 68 |
+
max_new_tokens=256,
|
| 69 |
+
do_sample=False,
|
| 70 |
+
pad_token_id=processor.tokenizer.pad_token_id
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
transcription = processor.decode(outputs[0], skip_special_tokens=True)
|
| 74 |
+
return transcription
|
| 75 |
+
|
| 76 |
except Exception as e:
|
| 77 |
+
return f"เกิดข้อผิดพลาด: {str(e)}"
|
| 78 |
+
|
| 79 |
+
# Initialize application
|
| 80 |
+
print("Initializing application...")
|
| 81 |
+
model_loaded = load_model_and_processor()
|
| 82 |
+
|
| 83 |
+
if model_loaded:
|
| 84 |
+
print("Creating Gradio interface...")
|
| 85 |
+
demo = gr.Interface(
|
| 86 |
+
fn=process_handwriting,
|
| 87 |
+
inputs=gr.Image(type="pil", label="อัพโหลดรูปลายมือเขียนภาษาไทย"),
|
| 88 |
+
outputs=gr.Textbox(label="ข้อความที่แปลงได้"),
|
| 89 |
+
title="Thai Handwriting to Text",
|
| 90 |
+
description="อัพโหลดรูปภาพลายมือเขียนภาษาไทยเพื่อแปลงเป็นข้อความ"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
print("Launching application...")
|
| 95 |
+
demo.launch()
|
| 96 |
+
else:
|
| 97 |
+
print("Failed to load model and processor. Please check the logs.")
|
app.py.origi
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
def install_packages():
|
| 6 |
+
print("Installing packages...")
|
| 7 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'])
|
| 8 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'huggingface_hub'])
|
| 9 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'transformers'])
|
| 10 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch', '--index-url', 'https://download.pytorch.org/whl/cpu'])
|
| 11 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio'])
|
| 12 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'Pillow'])
|
| 13 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'bitsandbytes'])
|
| 14 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'accelerate'])
|
| 15 |
+
|
| 16 |
+
if __name__ == "__main__":
|
| 17 |
+
try:
|
| 18 |
+
install_packages()
|
| 19 |
+
print("Package installation completed")
|
| 20 |
+
|
| 21 |
+
import gradio as gr
|
| 22 |
+
import torch
|
| 23 |
+
from transformers import AutoProcessor
|
| 24 |
+
|
| 25 |
+
def process_handwriting(image):
|
| 26 |
+
if image is None:
|
| 27 |
+
return "กรุณาอัพโหลดรูปภาพ"
|
| 28 |
+
return f"ทดสอบระบบ: Torch version: {torch.__version__}, Transformers installed"
|
| 29 |
+
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=process_handwriting,
|
| 32 |
+
inputs=gr.Image(type="pil", label="อัพโหลดรูปภาพ"),
|
| 33 |
+
outputs=gr.Textbox(label="ผลลัพธ์"),
|
| 34 |
+
title="Test Installation",
|
| 35 |
+
description="ทดสอบการติดตั้ง libraries"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error occurred: {str(e)}")
|
| 41 |
+
raise e
|