Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,12 @@ import spaces
|
|
| 8 |
import subprocess
|
| 9 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 10 |
|
|
|
|
| 11 |
processor = AutoProcessor.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3")
|
| 12 |
-
|
|
|
|
| 13 |
torch_dtype=torch.bfloat16,
|
|
|
|
| 14 |
trust_remote_code=True).to("cuda")
|
| 15 |
|
| 16 |
BAD_WORDS_IDS = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
|
|
@@ -27,33 +30,31 @@ def model_inference(
|
|
| 27 |
if text == "" and images:
|
| 28 |
gr.Error("Please input a text query along the image(s).")
|
| 29 |
|
| 30 |
-
# Check if the input is related to marketing
|
| 31 |
-
marketing_keywords = ["product", "brand", "advertisement", "marketing", "strategy", "comparison", "analysis", "trend", "audience"]
|
| 32 |
-
if not any(keyword in text.lower() for keyword in marketing_keywords):
|
| 33 |
-
return "Your question is not in the marketing area. Please upload another image or try again."
|
| 34 |
-
|
| 35 |
if isinstance(images, Image.Image):
|
| 36 |
images = [images]
|
| 37 |
|
|
|
|
| 38 |
resulting_messages = [
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
]
|
| 44 |
-
}
|
| 45 |
-
]
|
| 46 |
|
| 47 |
if assistant_prefix:
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
prompt = processor.apply_chat_template(resulting_messages, add_generation_prompt=True)
|
| 51 |
-
inputs = processor(text=prompt, images=images, return_tensors="pt")
|
| 52 |
inputs = {k: v.to("cuda") for k, v in inputs.items()}
|
| 53 |
|
| 54 |
generation_args = {
|
| 55 |
"max_new_tokens": max_new_tokens,
|
| 56 |
"repetition_penalty": repetition_penalty,
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
assert decoding_strategy in [
|
|
@@ -67,6 +68,7 @@ def model_inference(
|
|
| 67 |
generation_args["do_sample"] = True
|
| 68 |
generation_args["top_p"] = top_p
|
| 69 |
|
|
|
|
| 70 |
generation_args.update(inputs)
|
| 71 |
|
| 72 |
# Generate
|
|
@@ -75,10 +77,11 @@ def model_inference(
|
|
| 75 |
generated_texts = processor.batch_decode(generated_ids[:, generation_args["input_ids"].size(1):], skip_special_tokens=True)
|
| 76 |
return generated_texts[0]
|
| 77 |
|
|
|
|
| 78 |
with gr.Blocks(fill_height=True) as demo:
|
| 79 |
-
gr.Markdown("##
|
| 80 |
-
gr.Markdown("
|
| 81 |
-
gr.Markdown("**Disclaimer:**
|
| 82 |
with gr.Column():
|
| 83 |
image_input = gr.Image(label="Upload your Image", type="pil", scale=1)
|
| 84 |
query_input = gr.Textbox(label="Prompt")
|
|
@@ -89,91 +92,93 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 89 |
|
| 90 |
with gr.Accordion(label="Example Inputs and Advanced Generation Parameters"):
|
| 91 |
examples=[
|
| 92 |
-
|
| 93 |
["example_images/ad_analysis.png", "Analyze this advertisement and explain its marketing strategy.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 94 |
["example_images/market_trends.png", "Analyze this marketing chart and explain the market trends it represents.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 95 |
["example_images/social_media_post.png", "Critique this social media post about product quality and suggest improvements.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 96 |
["example_images/brand_comparison.jpg", "Compare these three brands based on their price strategies.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 97 |
-
["example_images/target_audience.jpg", "Analyze this image and suggest a target audience for the product.", None, "Greedy", 0.5, 768, 1.1, 0.85]
|
| 98 |
-
]
|
| 99 |
|
| 100 |
# Hyper-parameters for generation
|
| 101 |
max_new_tokens = gr.Slider(
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
repetition_penalty = gr.Slider(
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
temperature = gr.Slider(
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
top_p = gr.Slider(
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
decoding_strategy = gr.Radio(
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
decoding_strategy.change(
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
|
| 156 |
decoding_strategy.change(
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
decoding_strategy.change(
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
gr.Examples(
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
submit_btn.click(model_inference, inputs=[image_input, query_input, assistant_prefix, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p], outputs=output)
|
| 178 |
|
| 179 |
-
demo.launch(debug=True)
|
|
|
|
| 8 |
import subprocess
|
| 9 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 10 |
|
| 11 |
+
|
| 12 |
processor = AutoProcessor.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3")
|
| 13 |
+
|
| 14 |
+
model = Idefics3ForConditionalGeneration.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3",
|
| 15 |
torch_dtype=torch.bfloat16,
|
| 16 |
+
#_attn_implementation="flash_attention_2",
|
| 17 |
trust_remote_code=True).to("cuda")
|
| 18 |
|
| 19 |
BAD_WORDS_IDS = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
|
|
|
|
| 30 |
if text == "" and images:
|
| 31 |
gr.Error("Please input a text query along the image(s).")
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if isinstance(images, Image.Image):
|
| 34 |
images = [images]
|
| 35 |
|
| 36 |
+
|
| 37 |
resulting_messages = [
|
| 38 |
+
{
|
| 39 |
+
"role": "user",
|
| 40 |
+
"content": [{"type": "image"}] + [
|
| 41 |
+
{"type": "text", "text": text}
|
| 42 |
+
]
|
| 43 |
+
}
|
| 44 |
]
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if assistant_prefix:
|
| 47 |
+
text = f"{assistant_prefix} {text}"
|
| 48 |
+
|
| 49 |
|
| 50 |
prompt = processor.apply_chat_template(resulting_messages, add_generation_prompt=True)
|
| 51 |
+
inputs = processor(text=prompt, images=[images], return_tensors="pt")
|
| 52 |
inputs = {k: v.to("cuda") for k, v in inputs.items()}
|
| 53 |
|
| 54 |
generation_args = {
|
| 55 |
"max_new_tokens": max_new_tokens,
|
| 56 |
"repetition_penalty": repetition_penalty,
|
| 57 |
+
|
| 58 |
}
|
| 59 |
|
| 60 |
assert decoding_strategy in [
|
|
|
|
| 68 |
generation_args["do_sample"] = True
|
| 69 |
generation_args["top_p"] = top_p
|
| 70 |
|
| 71 |
+
|
| 72 |
generation_args.update(inputs)
|
| 73 |
|
| 74 |
# Generate
|
|
|
|
| 77 |
generated_texts = processor.batch_decode(generated_ids[:, generation_args["input_ids"].size(1):], skip_special_tokens=True)
|
| 78 |
return generated_texts[0]
|
| 79 |
|
| 80 |
+
|
| 81 |
with gr.Blocks(fill_height=True) as demo:
|
| 82 |
+
gr.Markdown("## IDEFICS3-Llama πΆ")
|
| 83 |
+
gr.Markdown("Play with [HuggingFaceM4/Idefics3-8B-Llama3](https://huggingface.co/HuggingFaceM4/Idefics3-8B-Llama3) in this demo. To get started, upload an image and text or try one of the examples.")
|
| 84 |
+
gr.Markdown("**Disclaimer:** Idefics3 does not include an RLHF alignment stage, so it may not consistently follow prompts or handle complex tasks. However, this doesn't mean it is incapable of doing so. Adding a prefix to the assistant's response, such as Let's think step for a reasoning question or `<html>` for HTML code generation, can significantly improve the output in practice. You could also play with the parameters such as the temperature in non-greedy mode.")
|
| 85 |
with gr.Column():
|
| 86 |
image_input = gr.Image(label="Upload your Image", type="pil", scale=1)
|
| 87 |
query_input = gr.Textbox(label="Prompt")
|
|
|
|
| 92 |
|
| 93 |
with gr.Accordion(label="Example Inputs and Advanced Generation Parameters"):
|
| 94 |
examples=[
|
| 95 |
+
["example_images/iphone_vs_samsung.jpg", "I want to buy a smartphone with features similar to the one in the photo. Suggest models and provide a comparison.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 96 |
["example_images/ad_analysis.png", "Analyze this advertisement and explain its marketing strategy.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 97 |
["example_images/market_trends.png", "Analyze this marketing chart and explain the market trends it represents.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 98 |
["example_images/social_media_post.png", "Critique this social media post about product quality and suggest improvements.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 99 |
["example_images/brand_comparison.jpg", "Compare these three brands based on their price strategies.", None, "Greedy", 0.5, 768, 1.1, 0.85],
|
| 100 |
+
["example_images/target_audience.jpg", "Analyze this image and suggest a target audience for the product.", None, "Greedy", 0.5, 768, 1.1, 0.85]]
|
|
|
|
| 101 |
|
| 102 |
# Hyper-parameters for generation
|
| 103 |
max_new_tokens = gr.Slider(
|
| 104 |
+
minimum=8,
|
| 105 |
+
maximum=1024,
|
| 106 |
+
value=512,
|
| 107 |
+
step=1,
|
| 108 |
+
interactive=True,
|
| 109 |
+
label="Maximum number of new tokens to generate",
|
| 110 |
+
)
|
| 111 |
repetition_penalty = gr.Slider(
|
| 112 |
+
minimum=0.01,
|
| 113 |
+
maximum=5.0,
|
| 114 |
+
value=1.2,
|
| 115 |
+
step=0.01,
|
| 116 |
+
interactive=True,
|
| 117 |
+
label="Repetition penalty",
|
| 118 |
+
info="1.0 is equivalent to no penalty",
|
| 119 |
+
)
|
| 120 |
temperature = gr.Slider(
|
| 121 |
+
minimum=0.0,
|
| 122 |
+
maximum=5.0,
|
| 123 |
+
value=0.4,
|
| 124 |
+
step=0.1,
|
| 125 |
+
interactive=True,
|
| 126 |
+
label="Sampling temperature",
|
| 127 |
+
info="Higher values will produce more diverse outputs.",
|
| 128 |
+
)
|
| 129 |
top_p = gr.Slider(
|
| 130 |
+
minimum=0.01,
|
| 131 |
+
maximum=0.99,
|
| 132 |
+
value=0.8,
|
| 133 |
+
step=0.01,
|
| 134 |
+
interactive=True,
|
| 135 |
+
label="Top P",
|
| 136 |
+
info="Higher values is equivalent to sampling more low-probability tokens.",
|
| 137 |
+
)
|
| 138 |
decoding_strategy = gr.Radio(
|
| 139 |
+
[
|
| 140 |
+
"Greedy",
|
| 141 |
+
"Top P Sampling",
|
| 142 |
+
],
|
| 143 |
+
value="Greedy",
|
| 144 |
+
label="Decoding strategy",
|
| 145 |
+
interactive=True,
|
| 146 |
+
info="Higher values is equivalent to sampling more low-probability tokens.",
|
| 147 |
+
)
|
| 148 |
decoding_strategy.change(
|
| 149 |
+
fn=lambda selection: gr.Slider(
|
| 150 |
+
visible=(
|
| 151 |
+
selection in ["contrastive_sampling", "beam_sampling", "Top P Sampling", "sampling_top_k"]
|
| 152 |
+
)
|
| 153 |
+
),
|
| 154 |
+
inputs=decoding_strategy,
|
| 155 |
+
outputs=temperature,
|
| 156 |
+
)
|
| 157 |
|
| 158 |
decoding_strategy.change(
|
| 159 |
+
fn=lambda selection: gr.Slider(
|
| 160 |
+
visible=(
|
| 161 |
+
selection in ["contrastive_sampling", "beam_sampling", "Top P Sampling", "sampling_top_k"]
|
| 162 |
+
)
|
| 163 |
+
),
|
| 164 |
+
inputs=decoding_strategy,
|
| 165 |
+
outputs=repetition_penalty,
|
| 166 |
+
)
|
| 167 |
decoding_strategy.change(
|
| 168 |
+
fn=lambda selection: gr.Slider(visible=(selection in ["Top P Sampling"])),
|
| 169 |
+
inputs=decoding_strategy,
|
| 170 |
+
outputs=top_p,
|
| 171 |
+
)
|
| 172 |
gr.Examples(
|
| 173 |
+
examples = examples,
|
| 174 |
+
inputs=[image_input, query_input, assistant_prefix, decoding_strategy, temperature,
|
| 175 |
+
max_new_tokens, repetition_penalty, top_p],
|
| 176 |
+
outputs=output,
|
| 177 |
+
fn=model_inference
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
submit_btn.click(model_inference, inputs = [image_input, query_input, assistant_prefix, decoding_strategy, temperature,
|
| 181 |
+
max_new_tokens, repetition_penalty, top_p], outputs=output)
|
| 182 |
|
|
|
|
| 183 |
|
| 184 |
+
demo.launch(debug=True)
|