Antonio0616 commited on
Commit
ce09f00
·
verified ·
1 Parent(s): bc9c73c

Upload 2 files

Browse files
Files changed (2) hide show
  1. 10_fairy_tale_book.py +57 -0
  2. Requirements.txt +4 -0
10_fairy_tale_book.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
3
+ from PIL import Image
4
+ import torch
5
+
6
+ # ✅ 모델 로딩
7
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xl")
8
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl").to("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+ # 📌 Step 1: 이미지 설명 추출 (이미지 → 캡션)
11
+ def extract_caption(image):
12
+ inputs = processor(images=image, return_tensors="pt").to(model.device)
13
+ outputs = model.generate(**inputs, max_new_tokens=50)
14
+ caption = processor.tokenizer.decode(outputs[0], skip_special_tokens=True)
15
+ return caption
16
+
17
+ # 📌 Step 2: 캡션 기반 동화 프롬프트 생성
18
+ def build_prompt_from_caption(caption):
19
+ return (
20
+ f"Write a magical and fun children's fairytale based on this description: \"{caption}\". "
21
+ "Start with 'Once upon a time' and continue for at least 7 sentences. "
22
+ "Include characters, emotions, the setting, and a twist. Make it feel like a real story."
23
+ )
24
+
25
+ # 📌 Step 3: 동화 생성 (캡션 + 프롬프트 → 텍스트)
26
+ def generate_fairytale(image):
27
+ caption = extract_caption(image)
28
+ prompt = build_prompt_from_caption(caption)
29
+ inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
30
+ outputs = model.generate(
31
+ **inputs,
32
+ max_new_tokens=400,
33
+ do_sample=True,
34
+ temperature=0.95,
35
+ top_p=0.9,
36
+ )
37
+ story = processor.tokenizer.decode(outputs[0], skip_special_tokens=True)
38
+ return story
39
+
40
+ # 🌐 Gradio UI
41
+ with gr.Blocks() as demo:
42
+ gr.Markdown("## 🧚‍♂️ 이미지 기반 AI 동화 생성기\n사진을 업로드하면 동화로 바꿔드립니다!")
43
+
44
+ with gr.Row():
45
+ image_input = gr.Image(type="pil", label="📸 이미지 업로드")
46
+
47
+ with gr.Row():
48
+ generate_button = gr.Button("✨ 동화 만들기")
49
+
50
+ with gr.Row():
51
+ output_text = gr.Textbox(label="📖 생성된 동화", lines=10)
52
+
53
+ generate_button.click(fn=generate_fairytale, inputs=[image_input], outputs=[output_text])
54
+
55
+ # 실행
56
+ if __name__ == "__main__":
57
+ demo.launch(share=True)
Requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ pillow