Spaces:
Running
on
Zero
Running
on
Zero
fix
Browse files- app.py +24 -94
- requirements.txt +8 -7
app.py
CHANGED
@@ -1,103 +1,33 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from transformers import AutoModelForVision2Seq, AutoProcessor, BitsAndBytesConfig
|
4 |
-
from PIL import Image
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
bnb_4bit_quant_type="nf4",
|
21 |
-
bnb_4bit_compute_dtype=torch.bfloat16
|
22 |
-
)
|
23 |
-
|
24 |
-
try:
|
25 |
-
# Load processor from base model
|
26 |
-
processor = AutoProcessor.from_pretrained(base_model_path)
|
27 |
-
|
28 |
-
# Load fine-tuned model
|
29 |
-
model = AutoModelForVision2Seq.from_pretrained(
|
30 |
-
model_path,
|
31 |
-
device_map="auto",
|
32 |
-
torch_dtype=torch.bfloat16,
|
33 |
-
quantization_config=bnb_config
|
34 |
-
)
|
35 |
-
return True
|
36 |
-
except Exception as e:
|
37 |
-
print(f"Error loading model: {str(e)}")
|
38 |
-
return False
|
39 |
-
|
40 |
-
def process_handwriting(image):
|
41 |
-
global model, processor
|
42 |
-
|
43 |
-
if image is None:
|
44 |
-
return "กรุณาอัพโหลดรูปภาพ"
|
45 |
|
46 |
-
try:
|
47 |
-
# Ensure image is in PIL format
|
48 |
-
if not isinstance(image, Image.Image):
|
49 |
-
image = Image.fromarray(image)
|
50 |
-
|
51 |
-
# Prepare prompt and messages
|
52 |
-
prompt = """Transcribe the Thai handwritten text from the provided image.
|
53 |
-
Only return the transcription in Thai language."""
|
54 |
-
messages = [
|
55 |
-
{
|
56 |
-
"role": "user",
|
57 |
-
"content": [
|
58 |
-
{"type": "text", "text": prompt},
|
59 |
-
{"type": "image", "image": image}
|
60 |
-
],
|
61 |
-
}
|
62 |
-
]
|
63 |
-
|
64 |
-
# Process input
|
65 |
-
text = processor.apply_chat_template(messages, tokenize=False)
|
66 |
-
inputs = processor(text=text, images=image, return_tensors="pt")
|
67 |
-
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
68 |
-
|
69 |
-
# Generate output
|
70 |
-
with torch.no_grad():
|
71 |
-
outputs = model.generate(
|
72 |
-
**inputs,
|
73 |
-
max_new_tokens=256,
|
74 |
-
do_sample=False,
|
75 |
-
pad_token_id=processor.tokenizer.pad_token_id
|
76 |
-
)
|
77 |
-
|
78 |
-
# Decode output
|
79 |
-
transcription = processor.decode(outputs[0], skip_special_tokens=True)
|
80 |
-
return transcription
|
81 |
-
|
82 |
-
except Exception as e:
|
83 |
-
return f"เกิดข้อผิดพลาด: {str(e)}"
|
84 |
-
|
85 |
-
# Load model when starting
|
86 |
-
print("กำลังโหลดโมเดล...")
|
87 |
-
model_loaded = load_model_and_processor()
|
88 |
-
|
89 |
-
if model_loaded:
|
90 |
-
# Create Gradio interface
|
91 |
demo = gr.Interface(
|
92 |
fn=process_handwriting,
|
93 |
-
inputs=gr.Image(type="pil", label="
|
94 |
-
outputs=gr.Textbox(label="
|
95 |
-
title="
|
96 |
-
description="
|
97 |
-
examples=[["example1.jpg"], ["example2.jpg"]]
|
98 |
)
|
99 |
|
100 |
-
|
101 |
-
demo.launch(share=True)
|
102 |
-
else:
|
103 |
-
print("ไม่สามารถโหลดโมเดลได้ กรุณาตรวจสอบ log")
|
|
|
1 |
+
import subprocess
|
2 |
+
import sys
|
|
|
|
|
3 |
|
4 |
+
def install_packages():
|
5 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'])
|
6 |
+
with open('requirements.txt') as f:
|
7 |
+
packages = f.read().splitlines()
|
8 |
+
for package in packages:
|
9 |
+
if package and not package.startswith('#'):
|
10 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
|
11 |
|
12 |
+
if __name__ == "__main__":
|
13 |
+
print("Installing required packages...")
|
14 |
+
install_packages()
|
15 |
|
16 |
+
import gradio as gr
|
17 |
+
import torch
|
18 |
+
from transformers import AutoProcessor
|
19 |
|
20 |
+
def process_handwriting(image):
|
21 |
+
if image is None:
|
22 |
+
return "กรุณาอัพโหลดรูปภาพ"
|
23 |
+
return f"ทดสอบระบบ: Torch version: {torch.__version__}, Transformers installed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
demo = gr.Interface(
|
26 |
fn=process_handwriting,
|
27 |
+
inputs=gr.Image(type="pil", label="อัพโหลดรูปภาพ"),
|
28 |
+
outputs=gr.Textbox(label="ผลลัพธ์"),
|
29 |
+
title="Test Installation",
|
30 |
+
description="ทดสอบการติดตั้ง libraries"
|
|
|
31 |
)
|
32 |
|
33 |
+
demo.launch()
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
--
|
2 |
-
|
3 |
-
transformers
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
1 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
2 |
+
huggingface_hub
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
gradio
|
6 |
+
Pillow
|
7 |
+
bitsandbytes
|
8 |
+
accelerate
|