Tonic commited on
Commit
0417d4a
·
unverified ·
1 Parent(s): 5d2fa6c
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -43,15 +43,21 @@ with open(f'{model_path}/tekken.json', 'r') as f:
43
 
44
  llm = None
45
 
46
- @spaces.GPU()
47
  def initialize_llm():
48
  global llm
49
  if llm is None:
50
- llm = LLM(model=repo_id,
51
- tokenizer_mode="mistral",
52
- max_model_len=65536,
53
- max_num_batched_tokens=max_img_per_msg * max_tokens_per_img,
54
- limit_mm_per_prompt={"image": max_img_per_msg})
 
 
 
 
 
 
55
 
56
 
57
  def encode_image(image: Image.Image, image_format="PNG") -> str:
@@ -61,9 +67,12 @@ def encode_image(image: Image.Image, image_format="PNG") -> str:
61
  im_64 = base64.b64encode(im_bytes).decode("utf-8")
62
  return im_64
63
 
64
- @spaces.GPU()
65
  def infer(image_url, prompt, progress=gr.Progress(track_tqdm=True)):
66
  initialize_llm()
 
 
 
67
  image = Image.open(BytesIO(requests.get(image_url).content))
68
  image = image.resize((3844, 2408))
69
  new_image_url = f"data:image/png;base64,{encode_image(image, image_format='PNG')}"
@@ -79,9 +88,12 @@ def infer(image_url, prompt, progress=gr.Progress(track_tqdm=True)):
79
 
80
  return outputs[0].outputs[0].text
81
 
82
- @spaces.GPU()
83
  def compare_images(image1_url, image2_url, prompt, progress=gr.Progress(track_tqdm=True)):
84
  initialize_llm()
 
 
 
85
  image1 = Image.open(BytesIO(requests.get(image1_url).content))
86
  image2 = Image.open(BytesIO(requests.get(image2_url).content))
87
  image1 = image1.resize((3844, 2408))
@@ -104,10 +116,12 @@ def compare_images(image1_url, image2_url, prompt, progress=gr.Progress(track_tq
104
 
105
  return outputs[0].outputs[0].text
106
 
107
- @spaces.GPU()
108
  def calculate_image_similarity(image1_url, image2_url):
109
  initialize_llm()
110
- # Load and preprocess images
 
 
111
  image1 = Image.open(BytesIO(requests.get(image1_url).content)).convert('RGB')
112
  image2 = Image.open(BytesIO(requests.get(image2_url).content)).convert('RGB')
113
  image1 = image1.resize((224, 224)) # Resize to match model input size
 
43
 
44
  llm = None
45
 
46
+ @spaces.GPU(duration=120)
47
  def initialize_llm():
48
  global llm
49
  if llm is None:
50
+ try:
51
+ llm = LLM(model=repo_id,
52
+ tokenizer_mode="mistral",
53
+ max_model_len=65536,
54
+ max_num_batched_tokens=max_img_per_msg * max_tokens_per_img,
55
+ limit_mm_per_prompt={"image": max_img_per_msg},
56
+ dtype="float16",
57
+ device="cuda" if torch.cuda.is_available() else "cpu")
58
+ except Exception as e:
59
+ print(f"Error initializing LLM: {e}")
60
+ llm = None
61
 
62
 
63
  def encode_image(image: Image.Image, image_format="PNG") -> str:
 
67
  im_64 = base64.b64encode(im_bytes).decode("utf-8")
68
  return im_64
69
 
70
+ @spaces.GPU(duration=120)
71
  def infer(image_url, prompt, progress=gr.Progress(track_tqdm=True)):
72
  initialize_llm()
73
+ if llm is None:
74
+ return "Error: LLM initialization failed. Please try again later."
75
+
76
  image = Image.open(BytesIO(requests.get(image_url).content))
77
  image = image.resize((3844, 2408))
78
  new_image_url = f"data:image/png;base64,{encode_image(image, image_format='PNG')}"
 
88
 
89
  return outputs[0].outputs[0].text
90
 
91
+ @spaces.GPU(duration=120)
92
  def compare_images(image1_url, image2_url, prompt, progress=gr.Progress(track_tqdm=True)):
93
  initialize_llm()
94
+ if llm is None:
95
+ return "Error: LLM initialization failed. Please try again later."
96
+
97
  image1 = Image.open(BytesIO(requests.get(image1_url).content))
98
  image2 = Image.open(BytesIO(requests.get(image2_url).content))
99
  image1 = image1.resize((3844, 2408))
 
116
 
117
  return outputs[0].outputs[0].text
118
 
119
+ @spaces.GPU(duration=120)
120
  def calculate_image_similarity(image1_url, image2_url):
121
  initialize_llm()
122
+ if llm is None:
123
+ return "Error: LLM initialization failed. Please try again later."
124
+
125
  image1 = Image.open(BytesIO(requests.get(image1_url).content)).convert('RGB')
126
  image2 = Image.open(BytesIO(requests.get(image2_url).content)).convert('RGB')
127
  image1 = image1.resize((224, 224)) # Resize to match model input size