Aekanun commited on
Commit
8ed9cb3
·
1 Parent(s): a111e87
Files changed (1) hide show
  1. app.py +42 -32
app.py CHANGED
@@ -38,34 +38,41 @@ def model_context():
38
  _tokenizer = None
39
  _model = None
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def decorator(func):
42
  def wrapper(*args, **kwargs):
43
- nonlocal _tokenizer, _model
44
- if _tokenizer is None:
45
- print("กำลังโหลด tokenizer...")
46
- from unsloth import FastVisionModel
47
- base_model, _tokenizer = FastVisionModel.from_pretrained(
48
- "unsloth/Llama-3.2-11B-Vision-Instruct",
49
- use_gradient_checkpointing = "unsloth"
50
- )
51
- print("โหลด tokenizer สำเร็จ")
52
-
53
- if _model is None:
54
- print("กำลังโหลดโมเดล fine-tuned...")
55
- from transformers import AutoModelForVision2Seq
56
- _model = AutoModelForVision2Seq.from_pretrained(
57
- "Aekanun/Llama-3.2-11B-Vision-Instruct-XRay",
58
- load_in_4bit=True,
59
- torch_dtype=torch.float16
60
- ).to('cuda')
61
- FastVisionModel.for_inference(_model)
62
- print("โหลดโมเดลสำเร็จ!")
63
-
64
  return func(_model, _tokenizer, *args, **kwargs)
65
  return wrapper
66
- return decorator
67
 
68
- @model_context()
 
 
 
 
69
  @spaces.GPU(duration=30)
70
  def process_image(_model, _tokenizer, image):
71
  if image is None:
@@ -107,12 +114,15 @@ def process_image(_model, _tokenizer, image):
107
  return f"เกิดข้อผิดพลาด: {str(e)}"
108
 
109
  print("กำลังเริ่มต้นแอปพลิเคชัน...")
110
- demo = gr.Interface(
111
- fn=process_image,
112
- inputs=gr.Image(type="pil"),
113
- outputs=gr.Textbox(),
114
- title="Medical Vision Analysis"
115
- )
116
-
117
- if __name__ == "__main__":
118
- demo.launch()
 
 
 
 
38
  _tokenizer = None
39
  _model = None
40
 
41
+ def init_models():
42
+ nonlocal _tokenizer, _model
43
+ try:
44
+ print("กำลังโหลด tokenizer...")
45
+ from unsloth import FastVisionModel
46
+ base_model, _tokenizer = FastVisionModel.from_pretrained(
47
+ "unsloth/Llama-3.2-11B-Vision-Instruct",
48
+ use_gradient_checkpointing = "unsloth"
49
+ )
50
+ print("โหลด tokenizer สำเร็จ")
51
+
52
+ print("กำลังโหลดโมเดล fine-tuned...")
53
+ from transformers import AutoModelForVision2Seq
54
+ _model = AutoModelForVision2Seq.from_pretrained(
55
+ "Aekanun/Llama-3.2-11B-Vision-Instruct-XRay",
56
+ load_in_4bit=True,
57
+ torch_dtype=torch.float16
58
+ ).to('cuda')
59
+ FastVisionModel.for_inference(_model)
60
+ print("โหลดโมเดลสำเร็จ!")
61
+ return True
62
+ except Exception as e:
63
+ print(f"เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
64
+ return False
65
+
66
  def decorator(func):
67
  def wrapper(*args, **kwargs):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  return func(_model, _tokenizer, *args, **kwargs)
69
  return wrapper
 
70
 
71
+ return init_models, decorator
72
+
73
+ init_models, model_decorator = model_context()
74
+
75
+ @model_decorator
76
  @spaces.GPU(duration=30)
77
  def process_image(_model, _tokenizer, image):
78
  if image is None:
 
114
  return f"เกิดข้อผิดพลาด: {str(e)}"
115
 
116
  print("กำลังเริ่มต้นแอปพลิเคชัน...")
117
+ if init_models():
118
+ demo = gr.Interface(
119
+ fn=process_image,
120
+ inputs=gr.Image(type="pil"),
121
+ outputs=gr.Textbox(),
122
+ title="Medical Vision Analysis"
123
+ )
124
+
125
+ if __name__ == "__main__":
126
+ demo.launch()
127
+ else:
128
+ print("ไม่สามารถเริ่มต้นแอปพลิเคชันได้")