Spaces:
Running
Running
Commit
·
497b535
1
Parent(s):
f835f68
update the app, remove the failing model
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
from haystack.components.generators import
|
| 5 |
-
from haystack.components.builders.
|
|
|
|
| 6 |
from haystack import Pipeline
|
| 7 |
from haystack.utils import Secret
|
| 8 |
from image_captioner import ImageCaptioner
|
|
@@ -17,25 +18,29 @@ description = """
|
|
| 17 |
|
| 18 |
It uses [Salesforce/blip-image-captioning-base](https://huggingface.co/Salesforce/blip-image-captioning-base) model for image-to-text caption generation task.
|
| 19 |
|
| 20 |
-
For Instagrammable captions,
|
| 21 |
|
| 22 |
-
Built by [Bilge Yucel](https://twitter.com/bilgeycl) using [Haystack
|
| 23 |
"""
|
| 24 |
|
| 25 |
-
prompt_template =
|
| 26 |
You will receive a descriptive text of a photo.
|
| 27 |
Try to generate a nice Instagram caption with a phrase rhyming with the text. Include emojis in the caption.
|
| 28 |
-
|
|
|
|
| 29 |
Descriptive text: {{caption}};
|
| 30 |
Instagram Caption:
|
| 31 |
-
"""
|
| 32 |
|
| 33 |
hf_api_key = os.environ["HF_API_KEY"]
|
| 34 |
|
| 35 |
def generate_caption(image_file_path, model_name):
|
| 36 |
image_to_text = ImageCaptioner(model_name="Salesforce/blip-image-captioning-base")
|
| 37 |
-
prompt_builder =
|
| 38 |
-
generator =
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
captioning_pipeline = Pipeline()
|
| 41 |
captioning_pipeline.add_component("image_to_text", image_to_text)
|
|
@@ -46,7 +51,7 @@ def generate_caption(image_file_path, model_name):
|
|
| 46 |
captioning_pipeline.connect("prompt_builder", "generator")
|
| 47 |
|
| 48 |
result = captioning_pipeline.run({"image_to_text":{"image_file_path":image_file_path}})
|
| 49 |
-
return result["generator"]["replies"][0]
|
| 50 |
|
| 51 |
with gr.Blocks(theme="soft") as demo:
|
| 52 |
gr.Markdown(value=description)
|
|
@@ -54,8 +59,8 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 54 |
image = gr.Image(type="filepath")
|
| 55 |
with gr.Column():
|
| 56 |
model_name = gr.Dropdown(
|
| 57 |
-
["
|
| 58 |
-
value="
|
| 59 |
label="Choose your model!"
|
| 60 |
)
|
| 61 |
gr.Examples(["./whale.png", "./rainbow.jpeg", "./selfie.png"], inputs=image, label="Click on any example")
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
from haystack.components.generators.chat import HuggingFaceAPIChatGenerator
|
| 5 |
+
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
|
| 6 |
+
from haystack.dataclasses import ChatMessage
|
| 7 |
from haystack import Pipeline
|
| 8 |
from haystack.utils import Secret
|
| 9 |
from image_captioner import ImageCaptioner
|
|
|
|
| 18 |
|
| 19 |
It uses [Salesforce/blip-image-captioning-base](https://huggingface.co/Salesforce/blip-image-captioning-base) model for image-to-text caption generation task.
|
| 20 |
|
| 21 |
+
For Instagrammable captions, try different text-to-text models to see how they react to the same prompt.
|
| 22 |
|
| 23 |
+
Built by [Bilge Yucel](https://twitter.com/bilgeycl) using [Haystack](https://github.com/deepset-ai/haystack) 💙
|
| 24 |
"""
|
| 25 |
|
| 26 |
+
prompt_template =[ChatMessage.from_user("""
|
| 27 |
You will receive a descriptive text of a photo.
|
| 28 |
Try to generate a nice Instagram caption with a phrase rhyming with the text. Include emojis in the caption.
|
| 29 |
+
Just return one option without alternatives. Don't use hashtags.
|
| 30 |
+
|
| 31 |
Descriptive text: {{caption}};
|
| 32 |
Instagram Caption:
|
| 33 |
+
""")]
|
| 34 |
|
| 35 |
hf_api_key = os.environ["HF_API_KEY"]
|
| 36 |
|
| 37 |
def generate_caption(image_file_path, model_name):
|
| 38 |
image_to_text = ImageCaptioner(model_name="Salesforce/blip-image-captioning-base")
|
| 39 |
+
prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables="*")
|
| 40 |
+
generator = HuggingFaceAPIChatGenerator(
|
| 41 |
+
api_type="serverless_inference_api",
|
| 42 |
+
api_params={"model": model_name},
|
| 43 |
+
token=Secret.from_token(hf_api_key))
|
| 44 |
|
| 45 |
captioning_pipeline = Pipeline()
|
| 46 |
captioning_pipeline.add_component("image_to_text", image_to_text)
|
|
|
|
| 51 |
captioning_pipeline.connect("prompt_builder", "generator")
|
| 52 |
|
| 53 |
result = captioning_pipeline.run({"image_to_text":{"image_file_path":image_file_path}})
|
| 54 |
+
return result["generator"]["replies"][0].text
|
| 55 |
|
| 56 |
with gr.Blocks(theme="soft") as demo:
|
| 57 |
gr.Markdown(value=description)
|
|
|
|
| 59 |
image = gr.Image(type="filepath")
|
| 60 |
with gr.Column():
|
| 61 |
model_name = gr.Dropdown(
|
| 62 |
+
["deepseek-ai/DeepSeek-V3.1-Terminus", "meta-llama/Llama-3.3-70B-Instruct", "openai/gpt-oss-20b", "Qwen/Qwen3-4B-Instruct-2507"],
|
| 63 |
+
value="deepseek-ai/DeepSeek-V3.1-Terminus",
|
| 64 |
label="Choose your model!"
|
| 65 |
)
|
| 66 |
gr.Examples(["./whale.png", "./rainbow.jpeg", "./selfie.png"], inputs=image, label="Click on any example")
|