je010112 commited on
Commit
0837084
·
1 Parent(s): 733e3c9

최종 앱

Browse files
Files changed (1) hide show
  1. app.py +112 -34
app.py CHANGED
@@ -1,57 +1,135 @@
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)
 
1
  import gradio as gr
2
+ import shutil
 
3
  import torch
4
+ from transformers import (
5
+ Blip2Processor,
6
+ Blip2ForConditionalGeneration,
7
+ AutoTokenizer,
8
+ AutoModelForCausalLM,
9
+ )
10
 
11
+ # ✅ 모델 로
12
+ device = "cpu"
13
  processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xl")
14
+ blip_model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl").to(device)
15
+ llm_tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct")
16
+ llm_model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-7b-instruct").to(device)
17
 
18
+ # 기능 정의
19
  def extract_caption(image):
20
+ inputs = processor(images=image, return_tensors="pt").to(device)
21
+ outputs = blip_model.generate(**inputs, max_new_tokens=50)
22
+ return processor.tokenizer.decode(outputs[0], skip_special_tokens=True)
 
23
 
24
+ def build_prompt(caption):
 
25
  return (
26
+ f"Based on the image description: \"{caption}\", write a children's fairytale.\n"
27
+ "The story must:\n"
28
+ "- Start with 'Once upon a time'\n"
29
+ "- Be at least 10 full sentences long\n"
30
+ "- Include named characters, a clear setting, emotions, a challenge, and a resolution\n"
31
+ "- Avoid mentions of babies or unrelated royalty unless relevant\n"
32
+ "Here is the story:\nOnce upon a time"
33
  )
34
 
 
35
  def generate_fairytale(image):
36
  caption = extract_caption(image)
37
+ prompt = build_prompt(caption)
38
+ inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
39
+ output = llm_model.generate(
40
  **inputs,
41
+ max_new_tokens=500,
42
  do_sample=True,
43
+ temperature=0.9,
44
+ top_p=0.95,
45
+ pad_token_id=llm_tokenizer.eos_token_id
46
  )
47
+ result = llm_tokenizer.decode(output[0], skip_special_tokens=True)
48
+ if "Once upon a time" in result:
49
+ story = "Once upon a time" + result.split("Once upon a time", 1)[-1].strip()
50
+ else:
51
+ story = result
52
+ return caption, story
53
 
54
+ # Gradio UI 구성 + 감성 디자인
55
+ with gr.Blocks(css="""
56
+ body {
57
+ background-color: white;
58
+ font-family: 'Comic Sans MS', cursive;
59
+ color: #333;
60
+ }
61
 
62
+ /* 전체 그라디오 컨테이너 테두리 제거 */
63
+ .gradio-container {
64
+ background-color: transparent !important;
65
+ padding: 20px;
66
+ border-radius: 15px;
67
+ }
68
 
69
+ /* 민트 테두리 + 흰 배경 */
70
+ .gr-box,
71
+ textarea, input, .input-image, .wrap.svelte-1ipelgc {
72
+ background-color: white !important;
73
+ border: 2px solid #a3e4e0 !important;
74
+ border-radius: 12px !important;
75
+ color: #000 !important;
76
+ }
77
 
78
+ /* 버튼 민트색 */
79
+ button {
80
+ background-color: #a3e4e0 !important;
81
+ color: #000 !important;
82
+ font-size: 16px;
83
+ border-radius: 10px;
84
+ padding: 10px 20px;
85
+ }
86
 
87
+ /* 헤더 타이틀 */
88
+ h1, h3 {
89
+ text-align: center;
90
+ color: #333;
91
+ }
92
 
93
+ /* 타원형 버튼 공통 스타일 */
94
+ .wrap.svelte-1ipelgc button {
95
+ width: 50px !important;
96
+ height: 50px !important;
97
+ border-radius: 50% !important;
98
+ background-size: 60% 60% !important;
99
+ background-repeat: no-repeat !important;
100
+ background-position: center !important;
101
+ background-color: #a3e4e0 !important;
102
+ border: none !important;
103
+ }
104
+
105
+ /* 개별 버튼 아이콘 삽입 */
106
+ .wrap.svelte-1ipelgc button:nth-child(1) {
107
+ background-image: url("/file=스크린샷 2025-07-25 101331.png"); /* 업로드 */
108
+ }
109
+ .wrap.svelte-1ipelgc button:nth-child(2) {
110
+ background-image: url("/file=스크린샷 2025-07-25 101348.png"); /* 카메라 */
111
+ }
112
+ .wrap.svelte-1ipelgc button:nth-child(3) {
113
+ background-image: url("/file=스크린샷 2025-07-25 101356.png"); /* 갤러리 or 샘플 */
114
+ }
115
+ }
116
+ """) as demo:
117
+
118
+ gr.HTML("""
119
+ <div style="padding: 20px;">
120
+ <h1>📖 AI Fairytale Creator</h1>
121
+ <h3>Upload a character image and generate a magical children's story ✨</h3>
122
+ </div>
123
+ """)
124
+
125
+ with gr.Column():
126
+ image_input = gr.Image(type="pil", label="🖼️ Upload your character image")
127
+ generate_button = gr.Button("✨ Create Fairytale")
128
+ caption_output = gr.Textbox(label="📌 Image Description")
129
+ story_output = gr.Textbox(label="📚 Generated Fairytale", lines=20)
130
+
131
+ generate_button.click(fn=generate_fairytale, inputs=image_input, outputs=[caption_output, story_output])
132
+
133
+ # ✅ 실행 (필수: share=True)
134
  if __name__ == "__main__":
135
  demo.launch(share=True)