Tonic commited on
Commit
545a937
·
unverified ·
1 Parent(s): 0417d4a
Files changed (1) hide show
  1. app.py +7 -23
app.py CHANGED
@@ -41,23 +41,11 @@ with open(f'{model_path}/params.json', 'r') as f:
41
  with open(f'{model_path}/tekken.json', 'r') as f:
42
  tokenizer_config = json.load(f)
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,9 +55,8 @@ 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
 
@@ -88,9 +75,8 @@ def infer(image_url, prompt, progress=gr.Progress(track_tqdm=True)):
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
 
@@ -118,7 +104,6 @@ def compare_images(image1_url, image2_url, prompt, progress=gr.Progress(track_tq
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
 
@@ -138,7 +123,6 @@ def calculate_image_similarity(image1_url, image2_url):
138
 
139
  return similarity
140
 
141
- # Gradio interface
142
  with gr.Blocks() as demo:
143
  gr.Markdown(title)
144
  gr.Markdown("## How it works")
 
41
  with open(f'{model_path}/tekken.json', 'r') as f:
42
  tokenizer_config = json.load(f)
43
 
44
+ llm = LLM(model=repo_id,
45
+ tokenizer_mode="mistral",
46
+ max_model_len=65536,
47
+ max_num_batched_tokens=max_img_per_msg * max_tokens_per_img,
48
+ limit_mm_per_prompt={"image": max_img_per_msg})
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
 
51
  def encode_image(image: Image.Image, image_format="PNG") -> str:
 
55
  im_64 = base64.b64encode(im_bytes).decode("utf-8")
56
  return im_64
57
 
58
+ @spaces.GPU()
59
  def infer(image_url, prompt, progress=gr.Progress(track_tqdm=True)):
 
60
  if llm is None:
61
  return "Error: LLM initialization failed. Please try again later."
62
 
 
75
 
76
  return outputs[0].outputs[0].text
77
 
78
+ @spaces.GPU()
79
  def compare_images(image1_url, image2_url, prompt, progress=gr.Progress(track_tqdm=True)):
 
80
  if llm is None:
81
  return "Error: LLM initialization failed. Please try again later."
82
 
 
104
 
105
  @spaces.GPU(duration=120)
106
  def calculate_image_similarity(image1_url, image2_url):
 
107
  if llm is None:
108
  return "Error: LLM initialization failed. Please try again later."
109
 
 
123
 
124
  return similarity
125
 
 
126
  with gr.Blocks() as demo:
127
  gr.Markdown(title)
128
  gr.Markdown("## How it works")