Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
# Device ์ค์
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
print(f"๐ Using device: {device}")
|
| 10 |
+
|
| 11 |
+
# ๋ชจ๋ธ ๋ก๋ฉ
|
| 12 |
+
print("๐ฆ Loading BLIP model...")
|
| 13 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 14 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
| 15 |
+
print("โ
Model loaded successfully!")
|
| 16 |
+
|
| 17 |
+
# ํํน ํ
ํ๋ฆฟ
|
| 18 |
+
def generate_hook_title(caption):
|
| 19 |
+
templates = [
|
| 20 |
+
f"You won't believe this: {caption}",
|
| 21 |
+
f"This is what happens when {caption.lower()}",
|
| 22 |
+
f"{caption}? Now that's a twist!",
|
| 23 |
+
f"{caption} โ but itโs not what you think!",
|
| 24 |
+
f"When {caption.lower()}, something unexpected happens ๐ฎ",
|
| 25 |
+
]
|
| 26 |
+
return random.choice(templates)
|
| 27 |
+
|
| 28 |
+
# ์ฒ๋ฆฌ ํจ์
|
| 29 |
+
def process_image(image):
|
| 30 |
+
inputs = processor(image, return_tensors="pt").to(device)
|
| 31 |
+
outputs = model.generate(**inputs)
|
| 32 |
+
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
+
hook_title = generate_hook_title(caption)
|
| 34 |
+
return hook_title
|
| 35 |
+
|
| 36 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 37 |
+
demo = gr.Interface(
|
| 38 |
+
fn=process_image,
|
| 39 |
+
inputs=gr.Image(type="pil", label="๐ธ Upload your YouTube thumbnail"),
|
| 40 |
+
outputs=gr.Textbox(label="๐ฅ Catchy English Title"),
|
| 41 |
+
title="๐ฌ YouTube Thumbnail Hook Title Generator",
|
| 42 |
+
description="Upload a thumbnail image and get a catchy, AI-generated English title!"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|