Geraldine commited on
Commit
129d16e
·
verified ·
1 Parent(s): f9d87a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -16,19 +16,32 @@ model = Qwen2VLForConditionalGeneration.from_pretrained(
16
  )
17
  processor = AutoProcessor.from_pretrained("./Qwen2-VL-7B-Instruct")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def generate_embeddings(text):
20
  model = SentenceTransformer('./all-MiniLM-L6-v2')
21
  embeddings = model.encode(sentences)
22
  return embeddings
23
 
24
  def describe_image(image_array):
25
- if image_array is None:
26
- raise ValueError("No image provided. Please upload an image before submitting.")
27
- # Convert numpy array to PIL Image
28
- image = Image.fromarray(np.uint8(image_array))
29
- buffered = io.BytesIO()
30
- image.save(buffered, format="PNG") # Change format as needed
31
- img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
32
 
33
  messages = [
34
  {
@@ -46,7 +59,7 @@ def describe_image(image_array):
46
  # Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
47
 
48
  inputs = processor(
49
- text=[text_prompt], images=[f"data:image/png;base64,{img_str}"], padding=True, return_tensors="pt"
50
  )
51
  inputs = inputs.to("cpu")
52
 
 
16
  )
17
  processor = AutoProcessor.from_pretrained("./Qwen2-VL-7B-Instruct")
18
 
19
+ def array_to_image_path(image_array):
20
+ if image_array is None:
21
+ raise ValueError("No image provided. Please upload an image before submitting.")
22
+ # Convert numpy array to PIL Image
23
+ img = Image.fromarray(np.uint8(image_array))
24
+
25
+ # Generate a unique filename using timestamp
26
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
27
+ filename = f"image_{timestamp}.png"
28
+
29
+ # Save the image
30
+ img.save(filename)
31
+
32
+ # Get the full path of the saved image
33
+ full_path = os.path.abspath(filename)
34
+
35
+ return full_path
36
+
37
  def generate_embeddings(text):
38
  model = SentenceTransformer('./all-MiniLM-L6-v2')
39
  embeddings = model.encode(sentences)
40
  return embeddings
41
 
42
  def describe_image(image_array):
43
+ image_path = array_to_image_path(image)
44
+ image = Image.open(image_path)
 
 
 
 
 
45
 
46
  messages = [
47
  {
 
59
  # Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
60
 
61
  inputs = processor(
62
+ text=[text_prompt], images=[image], padding=True, return_tensors="pt"
63
  )
64
  inputs = inputs.to("cpu")
65