KarishGupta commited on
Commit
2c837a7
·
1 Parent(s): 0b79131

Implemented Inference Client with "zai-org/GLM-4.5V" model

Browse files
Files changed (2) hide show
  1. gradio_app.py +2 -2
  2. remote_model.py +14 -9
gradio_app.py CHANGED
@@ -1,13 +1,13 @@
1
  import gradio as gr
2
  from local_model import query_local
3
- from remote_model import query_remote, pipe
4
  import time
5
 
6
  def query(image, question, model_name):
7
  if model_name == "Local":
8
  return query_local(image, question)
9
  elif model_name == "Remote":
10
- return query_remote(image, question, pipe)
11
  return "No model selected"
12
 
13
 
 
1
  import gradio as gr
2
  from local_model import query_local
3
+ from remote_model import query_remote, client
4
  import time
5
 
6
  def query(image, question, model_name):
7
  if model_name == "Local":
8
  return query_local(image, question)
9
  elif model_name == "Remote":
10
+ return query_remote(image, question, client)
11
  return "No model selected"
12
 
13
 
remote_model.py CHANGED
@@ -1,32 +1,37 @@
1
  from huggingface_hub import InferenceClient
2
- import huggingface_hub
3
- from consts import BASE_MODEL
4
  from PIL import Image
5
- from transformers import pipeline
6
  import time
 
 
7
 
 
8
 
9
- pipe = pipeline("image-text-to-text", model = BASE_MODEL)
10
 
11
-
12
- def query_remote(image: Image.Image, question: str, pipe):
13
  start_time = time.time()
14
  print("starting remote inference... %s" %(start_time))
 
15
  if not Image:
16
  raise ValueError("Missing image")
17
 
 
 
 
 
 
18
  messages = [
19
  {
20
  "role": "user",
21
  "content": [
22
- {"type": "image", "image": image},
23
  {"type": "text", "text": question}
24
  ]
25
  }
26
  ]
27
 
28
- outputs = pipe(text=messages, return_full_text=False)
29
 
30
  print("remote time %s --- " % (time.time() - start_time))
31
 
32
- return outputs[0]["generated_text"]
 
1
  from huggingface_hub import InferenceClient
 
 
2
  from PIL import Image
 
3
  import time
4
+ import base64
5
+ from io import BytesIO
6
 
7
+ remote_model="zai-org/GLM-4.5V"
8
 
9
+ client = InferenceClient(model=remote_model)
10
 
11
+ def query_remote(image: Image.Image, question: str, client: InferenceClient):
 
12
  start_time = time.time()
13
  print("starting remote inference... %s" %(start_time))
14
+
15
  if not Image:
16
  raise ValueError("Missing image")
17
 
18
+ buffered = BytesIO()
19
+ image.save(buffered, format="JPEG")
20
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
21
+ image_url = f"data:image/jpeg;base64,{img_str}"
22
+
23
  messages = [
24
  {
25
  "role": "user",
26
  "content": [
27
+ {"type": "image_url", "image_url": {"url": image_url}},
28
  {"type": "text", "text": question}
29
  ]
30
  }
31
  ]
32
 
33
+ response = client.chat.completions.create(messages=messages, max_tokens=256)
34
 
35
  print("remote time %s --- " % (time.time() - start_time))
36
 
37
+ return response.choices[0].message.content