Jey813 commited on
Commit
7fb3a4f
ยท
verified ยท
1 Parent(s): f552cf5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
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()