Aekanun commited on
Commit
988c093
1 Parent(s): bd2187c
Files changed (2) hide show
  1. app.py +24 -94
  2. requirements.txt +8 -7
app.py CHANGED
@@ -1,103 +1,33 @@
1
- import gradio as gr
2
- import torch
3
- from transformers import AutoModelForVision2Seq, AutoProcessor, BitsAndBytesConfig
4
- from PIL import Image
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
 
13
- model_path = "Aekanun/thai-handwriting-llm"
14
- base_model_path = "meta-llama/Llama-3.2-11B-Vision-Instruct"
 
15
 
16
- # BitsAndBytes config for 4-bit quantization
17
- bnb_config = BitsAndBytesConfig(
18
- load_in_4bit=True,
19
- bnb_4bit_use_double_quant=True,
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="Thai Handwriting to Text ด้วย LLaMA Vision",
96
- description="อัพโหลดรูปภาพลายมือเขียนภาษาไทยเพื่อแปลงเป็นข้อความ โดยใช้โมเดล LLaMA Vision ที่ fine-tune มาสำหรับภาษาไทย",
97
- examples=[["example1.jpg"], ["example2.jpg"]]
98
  )
99
 
100
- if __name__ == "__main__":
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
- --find-links https://download.pytorch.org/whl/torch_stable.html
2
- torch==2.1.1+cpu
3
- transformers==4.35.2
4
- gradio==4.13.0
5
- Pillow==10.1.0
6
- bitsandbytes==0.41.1
7
- accelerate==0.25.0
 
 
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