Spaces:
Runtime error
Runtime error
gamin
commited on
Commit
·
3576e04
1
Parent(s):
22ccd00
동화 생성 기능 코드 추가 (BLIP2 + base01)
Browse files- blip-image-captioning-base01.py +104 -0
blip-image-captioning-base01.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # 필요한 라이브러리 임포트
|
| 2 |
+
# from transformers import BlipProcessor, BlipForConditionalGeneration # 이미지 → 텍스트 설명 생성용
|
| 3 |
+
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline # 텍스트 생성 (동화 생성)용
|
| 4 |
+
# from PIL import Image # 이미지 처리용
|
| 5 |
+
# import gradio as gr # 웹 인터페이스용
|
| 6 |
+
# import torch # PyTorch (모델 구동)
|
| 7 |
+
|
| 8 |
+
# # 1. BLIP 모델: 이미지 설명 생성용 모델 및 전처리기 로딩
|
| 9 |
+
# caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 10 |
+
# caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
+
|
| 12 |
+
# # 2. KoT5 모델: 설명 → 동화 생성용 한국어 텍스트 생성 모델 로딩
|
| 13 |
+
# story_tokenizer = AutoTokenizer.from_pretrained("paust/pko-t5-base")
|
| 14 |
+
# story_model = AutoModelForSeq2SeqLM.from_pretrained("paust/pko-t5-base")
|
| 15 |
+
|
| 16 |
+
# # Hugging Face pipeline으로 간편한 생성 함수 구성
|
| 17 |
+
# story_generator = pipeline("text2text-generation", model=story_model, tokenizer=story_tokenizer)
|
| 18 |
+
|
| 19 |
+
# # 3. 핵심 함수: 이미지 입력 → 설명 생성 → 동화 생성
|
| 20 |
+
# def generate_fairytale(image):
|
| 21 |
+
# # 이미지 → 설명 생성 (BLIP)
|
| 22 |
+
# inputs = caption_processor(images=image, return_tensors="pt") # 이미지 전처리
|
| 23 |
+
# outputs = caption_model.generate(**inputs) # 설명 생성
|
| 24 |
+
# caption = caption_processor.decode(outputs[0], skip_special_tokens=True) # 설명 텍스트 디코딩
|
| 25 |
+
|
| 26 |
+
# # 프롬프트 구성: 설명을 바탕으로 동화를 써달라고 요청
|
| 27 |
+
# prompt = f"다음 설명을 바탕으로 어린이 동화를 써줘:\n\"{caption}\""
|
| 28 |
+
|
| 29 |
+
# # 설명 → 동화 생성 (KoT5)
|
| 30 |
+
# story = story_generator(prompt, max_length=300, do_sample=True)[0]['generated_text']
|
| 31 |
+
|
| 32 |
+
# # Gradio에 표시할 두 개 결과 반환: 설명, 동화
|
| 33 |
+
# return caption, story
|
| 34 |
+
|
| 35 |
+
# # 4. Gradio 인터페이스 구성
|
| 36 |
+
# interface = gr.Interface(
|
| 37 |
+
# fn=generate_fairytale, # 호출할 함수
|
| 38 |
+
# inputs=gr.Image(type="pil", label="🖼️ 그림을 업로드하세요"), # 이미지 업로드 창
|
| 39 |
+
# outputs=[
|
| 40 |
+
# gr.Textbox(label="📌 이미지 설명"), # 텍스트 설명 출력
|
| 41 |
+
# gr.Textbox(label="📖 생성된 동화") # 생성된 동화 출력
|
| 42 |
+
# ],
|
| 43 |
+
# title="🎨 AI 이미지 동화 생성기", # 앱 제목
|
| 44 |
+
# description="이미지를 업로드하면 설명과 동화를 자동으로 생성해주는 서비스입니다.",
|
| 45 |
+
# theme="soft" # 부드러운 Gradio 테마
|
| 46 |
+
# )
|
| 47 |
+
|
| 48 |
+
# # 5. 웹 앱 실행 (URL 콘솔 출력)
|
| 49 |
+
# interface.launch(share=True, debug=True, inbrowser=True)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 53 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 54 |
+
from PIL import Image
|
| 55 |
+
import gradio as gr
|
| 56 |
+
import torch
|
| 57 |
+
|
| 58 |
+
# 1. BLIP 이미지 캡셔닝 모델
|
| 59 |
+
caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 60 |
+
caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 61 |
+
|
| 62 |
+
# 2. 영어 동화 생성 모델 (Flan-T5)
|
| 63 |
+
story_tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
|
| 64 |
+
story_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
| 65 |
+
story_generator = pipeline("text2text-generation", model=story_model, tokenizer=story_tokenizer)
|
| 66 |
+
|
| 67 |
+
# 3. 메인 함수
|
| 68 |
+
def generate_fairytale(image):
|
| 69 |
+
# (1) 이미지 → 캡션 생성
|
| 70 |
+
inputs = caption_processor(images=image, return_tensors="pt").to(caption_model.device)
|
| 71 |
+
outputs = caption_model.generate(**inputs)
|
| 72 |
+
caption = caption_processor.decode(outputs[0], skip_special_tokens=True).strip()
|
| 73 |
+
|
| 74 |
+
# (2) 캡션이 너무 짧으면 보정
|
| 75 |
+
if len(caption.split()) < 5:
|
| 76 |
+
caption += ". They appear to be magical characters from a fantasy world."
|
| 77 |
+
|
| 78 |
+
# (3) 영어 프롬프트 구성
|
| 79 |
+
prompt = (
|
| 80 |
+
f"Write a short and magical fairytale for children based on the description below.\n"
|
| 81 |
+
f"Description: \"{caption}\"\n"
|
| 82 |
+
f"Story:"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# (4) 동화 생성
|
| 86 |
+
story = story_generator(prompt, max_length=300, do_sample=True)[0]['generated_text']
|
| 87 |
+
|
| 88 |
+
return caption, story
|
| 89 |
+
|
| 90 |
+
# 4. Gradio UI
|
| 91 |
+
interface = gr.Interface(
|
| 92 |
+
fn=generate_fairytale,
|
| 93 |
+
inputs=gr.Image(type="pil", label="🖼️ Upload an Image"),
|
| 94 |
+
outputs=[
|
| 95 |
+
gr.Textbox(label="📌 Image Description"),
|
| 96 |
+
gr.Textbox(label="📖 Generated Fairytale")
|
| 97 |
+
],
|
| 98 |
+
title="🧚 AI Fairytale Generator from Image",
|
| 99 |
+
description="Upload an image, and this AI will create a magical story based on it!",
|
| 100 |
+
theme="soft"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# 5. 앱 실행
|
| 104 |
+
interface.launch(share=True, debug=True, inbrowser=True)
|