Spaces:
Runtime error
Runtime error
gamin
commited on
Commit
·
5e68f38
1
Parent(s):
2c99f9b
배경 이미지 삽입 시도 1
Browse files
ui05.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# ✅ 로컬 배경 이미지 back.png → Gradio가 읽을 수 있는 경로로 복사
|
| 12 |
+
shutil.copyfile("back.png", "/mnt/data/back.png")
|
| 13 |
+
|
| 14 |
+
# ✅ 모델 로드
|
| 15 |
+
device = "cpu"
|
| 16 |
+
processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
| 17 |
+
blip_model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl").to(device)
|
| 18 |
+
llm_tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-7b-instruct")
|
| 19 |
+
llm_model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-7b-instruct").to(device)
|
| 20 |
+
|
| 21 |
+
# ✅ 기능 정의
|
| 22 |
+
def extract_caption(image):
|
| 23 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 24 |
+
outputs = blip_model.generate(**inputs, max_new_tokens=50)
|
| 25 |
+
return processor.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
def build_prompt(caption):
|
| 28 |
+
return (
|
| 29 |
+
f"Based on the image description: \"{caption}\", write a children's fairytale.\n"
|
| 30 |
+
"The story must:\n"
|
| 31 |
+
"- Start with 'Once upon a time'\n"
|
| 32 |
+
"- Be at least 10 full sentences long\n"
|
| 33 |
+
"- Include named characters, a clear setting, emotions, a challenge, and a resolution\n"
|
| 34 |
+
"- Avoid mentions of babies or unrelated royalty unless relevant\n"
|
| 35 |
+
"Here is the story:\nOnce upon a time"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
def generate_fairytale(image):
|
| 39 |
+
caption = extract_caption(image)
|
| 40 |
+
prompt = build_prompt(caption)
|
| 41 |
+
inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
|
| 42 |
+
output = llm_model.generate(
|
| 43 |
+
**inputs,
|
| 44 |
+
max_new_tokens=500,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
temperature=0.9,
|
| 47 |
+
top_p=0.95,
|
| 48 |
+
pad_token_id=llm_tokenizer.eos_token_id
|
| 49 |
+
)
|
| 50 |
+
result = llm_tokenizer.decode(output[0], skip_special_tokens=True)
|
| 51 |
+
if "Once upon a time" in result:
|
| 52 |
+
story = "Once upon a time" + result.split("Once upon a time", 1)[-1].strip()
|
| 53 |
+
else:
|
| 54 |
+
story = result
|
| 55 |
+
return caption, story
|
| 56 |
+
|
| 57 |
+
# ✅ Gradio UI 구성 + 감성 디자인
|
| 58 |
+
with gr.Blocks(css="""
|
| 59 |
+
body {
|
| 60 |
+
background-image: url('/file=back.png');
|
| 61 |
+
background-size: cover;
|
| 62 |
+
background-position: center;
|
| 63 |
+
background-repeat: no-repeat;
|
| 64 |
+
font-family: 'Comic Sans MS', cursive;
|
| 65 |
+
color: white;
|
| 66 |
+
}
|
| 67 |
+
.gradio-container {
|
| 68 |
+
background-color: rgba(0, 0, 0, 0.4) !important;
|
| 69 |
+
padding: 20px;
|
| 70 |
+
border-radius: 15px;
|
| 71 |
+
}
|
| 72 |
+
button {
|
| 73 |
+
background-color: #ffd700 !important;
|
| 74 |
+
color: #000;
|
| 75 |
+
font-size: 16px;
|
| 76 |
+
border-radius: 10px;
|
| 77 |
+
padding: 10px 20px;
|
| 78 |
+
}
|
| 79 |
+
textarea, input {
|
| 80 |
+
background-color: rgba(255, 255, 255, 0.9) !important;
|
| 81 |
+
color: #000 !important;
|
| 82 |
+
border-radius: 10px;
|
| 83 |
+
}
|
| 84 |
+
h1, h3 {
|
| 85 |
+
text-align: center;
|
| 86 |
+
color: #fffacd;
|
| 87 |
+
}
|
| 88 |
+
""") as demo:
|
| 89 |
+
|
| 90 |
+
gr.HTML("""
|
| 91 |
+
<div style="padding: 20px;">
|
| 92 |
+
<h1>📖 AI Fairytale Creator</h1>
|
| 93 |
+
<h3>Upload a character image and generate a magical children's story ✨</h3>
|
| 94 |
+
</div>
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
+
with gr.Column():
|
| 98 |
+
image_input = gr.Image(type="pil", label="🖼️ Upload your character image")
|
| 99 |
+
generate_button = gr.Button("✨ Create Fairytale")
|
| 100 |
+
caption_output = gr.Textbox(label="📌 Image Description")
|
| 101 |
+
story_output = gr.Textbox(label="📚 Generated Fairytale", lines=20)
|
| 102 |
+
|
| 103 |
+
generate_button.click(fn=generate_fairytale, inputs=image_input, outputs=[caption_output, story_output])
|
| 104 |
+
|
| 105 |
+
# ✅ 실행 (필수: share=True)
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch(share=True)
|