- app.py +33 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
5 |
+
|
6 |
+
story_gen = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-alpha",
|
7 |
+
max_new_tokens=300, do_sample=True, temperature=0.9)
|
8 |
+
|
9 |
+
def image_to_story(image):
|
10 |
+
caption_result = captioner(image)
|
11 |
+
caption = caption_result[0]["generated_text"]
|
12 |
+
|
13 |
+
prompt = f"Write a short and imaginative story based on the following image description:\n\"{caption}\""
|
14 |
+
|
15 |
+
story_result = story_gen(prompt)
|
16 |
+
story = story_result[0]["generated_text"].replace(prompt, "").strip()
|
17 |
+
|
18 |
+
return caption, story
|
19 |
+
|
20 |
+
demo = gr.Interface(
|
21 |
+
fn=image_to_story,
|
22 |
+
inputs=gr.Image(type="pil"),
|
23 |
+
outputs=[
|
24 |
+
gr.Textbox(label="Image Description (BLIP Caption)"),
|
25 |
+
gr.Textbox(label="Generated Story")
|
26 |
+
],
|
27 |
+
title="🖼️→📖 Image to Story Generator",
|
28 |
+
description="Upload an image to generate a descriptive caption and a short creative story",
|
29 |
+
flagging_mode="never"
|
30 |
+
)
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers>=4.37.0
|
2 |
+
torch
|
3 |
+
gradio>=4.0.0
|
4 |
+
hf_xet
|