update app
Browse files
app.py
CHANGED
@@ -1,115 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
4 |
-
from qwen_vl_utils import process_vision_info
|
5 |
-
import torch
|
6 |
from PIL import Image
|
7 |
-
import subprocess
|
8 |
-
from datetime import datetime
|
9 |
import numpy as np
|
10 |
import os
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# models = {
|
16 |
-
# "Qwen/Qwen2-VL-7B-Instruct": AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True, torch_dtype="auto", _attn_implementation="flash_attention_2").cuda().eval()
|
17 |
|
18 |
|
19 |
-
# }
|
20 |
def array_to_image_path(image_array):
|
21 |
if image_array is None:
|
22 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
23 |
-
# Convert numpy array to PIL Image
|
24 |
-
img = Image.fromarray(np.uint8(image_array))
|
25 |
|
26 |
-
|
27 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
28 |
filename = f"image_{timestamp}.png"
|
29 |
-
|
30 |
-
# Save the image
|
31 |
img.save(filename)
|
32 |
-
|
33 |
-
# Get the full path of the saved image
|
34 |
full_path = os.path.abspath(filename)
|
35 |
-
|
36 |
return full_path
|
37 |
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
)
|
43 |
-
.cpu()
|
44 |
-
.eval()
|
45 |
-
}
|
46 |
-
|
47 |
-
processors = {
|
48 |
-
"Qwen/Qwen2-VL-7B-Instruct": AutoProcessor.from_pretrained(
|
49 |
-
"Qwen/Qwen2-VL-7B-Instruct", trust_remote_code=True
|
50 |
-
)
|
51 |
-
}
|
52 |
-
|
53 |
-
DESCRIPTION = "WordLift Product Description Generation - [Qwen2-VL-7B Demo](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct)"
|
54 |
-
|
55 |
-
kwargs = {}
|
56 |
-
kwargs["torch_dtype"] = torch.bfloat16
|
57 |
-
|
58 |
-
user_prompt = "<|user|>\n"
|
59 |
-
assistant_prompt = "<|assistant|>\n"
|
60 |
-
prompt_suffix = "<|end|>\n"
|
61 |
-
|
62 |
-
|
63 |
-
@spaces.GPU
|
64 |
-
def run_example(image, text_input=None, model_id="Qwen/Qwen2-VL-7B-Instruct"):
|
65 |
image_path = array_to_image_path(image)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
"type": "image",
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
# Preparation for inference
|
87 |
-
text = processor.apply_chat_template(
|
88 |
-
messages, tokenize=False, add_generation_prompt=True
|
89 |
-
)
|
90 |
-
image_inputs, video_inputs = process_vision_info(messages)
|
91 |
-
inputs = processor(
|
92 |
-
text=[text],
|
93 |
-
images=image_inputs,
|
94 |
-
videos=video_inputs,
|
95 |
-
padding=True,
|
96 |
-
return_tensors="pt",
|
97 |
-
)
|
98 |
-
inputs = inputs.to("cpu")
|
99 |
-
|
100 |
-
# Inference: Generation of the output
|
101 |
-
generated_ids = model.generate(**inputs, max_new_tokens=1024)
|
102 |
-
generated_ids_trimmed = [
|
103 |
-
out_ids[len(in_ids) :]
|
104 |
-
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
105 |
-
]
|
106 |
-
output_text = processor.batch_decode(
|
107 |
-
generated_ids_trimmed,
|
108 |
-
skip_special_tokens=True,
|
109 |
-
clean_up_tokenization_spaces=False,
|
110 |
)
|
111 |
|
112 |
-
return
|
|
|
113 |
|
114 |
|
115 |
css = """
|
@@ -121,23 +60,18 @@ css = """
|
|
121 |
"""
|
122 |
|
123 |
with gr.Blocks(css=css) as demo:
|
124 |
-
gr.Markdown(
|
125 |
with gr.Tab(label="WordLift Product Description Generation"):
|
126 |
with gr.Row():
|
127 |
with gr.Column():
|
128 |
input_img = gr.Image(label="Input Picture")
|
129 |
-
|
130 |
-
choices=list(models.keys()),
|
131 |
-
label="Model",
|
132 |
-
value="Qwen/Qwen2-VL-7B-Instruct",
|
133 |
-
)
|
134 |
-
text_input = gr.Textbox(label="Question")
|
135 |
submit_btn = gr.Button(value="Submit")
|
136 |
with gr.Column():
|
137 |
output_text = gr.Textbox(label="Output Text")
|
138 |
|
139 |
submit_btn.click(
|
140 |
-
|
141 |
)
|
142 |
|
143 |
demo.queue(api_open=False)
|
|
|
1 |
import gradio as gr
|
2 |
+
from openai import OpenAI
|
|
|
|
|
|
|
3 |
from PIL import Image
|
|
|
|
|
4 |
import numpy as np
|
5 |
import os
|
6 |
+
from datetime import datetime
|
7 |
|
8 |
+
# Initialize OpenAI client
|
9 |
+
client = OpenAI()
|
|
|
|
|
|
|
10 |
|
11 |
|
|
|
12 |
def array_to_image_path(image_array):
|
13 |
if image_array is None:
|
14 |
raise ValueError("No image provided. Please upload an image before submitting.")
|
|
|
|
|
15 |
|
16 |
+
img = Image.fromarray(np.uint8(image_array))
|
17 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
18 |
filename = f"image_{timestamp}.png"
|
|
|
|
|
19 |
img.save(filename)
|
|
|
|
|
20 |
full_path = os.path.abspath(filename)
|
|
|
21 |
return full_path
|
22 |
|
23 |
|
24 |
+
# Function to generate product description using OpenAI API
|
25 |
+
def generate_product_description(image, text_input=None):
|
26 |
+
# Convert the image to a path (optional, could directly send the image as a URL if available)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
image_path = array_to_image_path(image)
|
28 |
|
29 |
+
# Assuming the image is hosted online, replace the path with the URL.
|
30 |
+
# In practice, you'd need a public URL to share the image with the API.
|
31 |
+
image_url = "https://example.com/" + os.path.basename(image_path)
|
32 |
+
|
33 |
+
# API request
|
34 |
+
completion = client.chat.completions.create(
|
35 |
+
model="gpt-4o",
|
36 |
+
messages=[
|
37 |
+
{
|
38 |
+
"role": "user",
|
39 |
+
"content": [
|
40 |
+
{"type": "text", "text": text_input or "What's in this image?"},
|
41 |
+
{
|
42 |
+
"type": "image_url",
|
43 |
+
"image_url": {"url": image_url},
|
44 |
+
},
|
45 |
+
],
|
46 |
+
}
|
47 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
)
|
49 |
|
50 |
+
# Extract and return the generated message
|
51 |
+
return completion.choices[0].message
|
52 |
|
53 |
|
54 |
css = """
|
|
|
60 |
"""
|
61 |
|
62 |
with gr.Blocks(css=css) as demo:
|
63 |
+
gr.Markdown("WordLift Product Description Generation - [GPT-4o-mini Demo]")
|
64 |
with gr.Tab(label="WordLift Product Description Generation"):
|
65 |
with gr.Row():
|
66 |
with gr.Column():
|
67 |
input_img = gr.Image(label="Input Picture")
|
68 |
+
text_input = gr.Textbox(label="Additional Instructions (Optional)")
|
|
|
|
|
|
|
|
|
|
|
69 |
submit_btn = gr.Button(value="Submit")
|
70 |
with gr.Column():
|
71 |
output_text = gr.Textbox(label="Output Text")
|
72 |
|
73 |
submit_btn.click(
|
74 |
+
generate_product_description, [input_img, text_input], [output_text]
|
75 |
)
|
76 |
|
77 |
demo.queue(api_open=False)
|