Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +69 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
from diffusers import StableDiffusionPipeline
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
def load_fairytale(file_obj):
|
| 9 |
+
data = json.load(file_obj)
|
| 10 |
+
return data['title'], data['content']
|
| 11 |
+
|
| 12 |
+
def generate_grandma_voice(text):
|
| 13 |
+
grandma_text = f"에구구 얘야, 잘 들어보렴. {text.strip()} ... 옛날 옛적 이야기란다~"
|
| 14 |
+
tts = gTTS(text=grandma_text, lang='ko')
|
| 15 |
+
audio_path = "grandma_voice.mp3"
|
| 16 |
+
tts.save(audio_path)
|
| 17 |
+
return audio_path
|
| 18 |
+
|
| 19 |
+
emotion_tokenizer = AutoTokenizer.from_pretrained("monologg/koelectra-base-discriminator")
|
| 20 |
+
emotion_model = AutoModelForSequenceClassification.from_pretrained("monologg/koelectra-base-discriminator")
|
| 21 |
+
|
| 22 |
+
def classify_emotion(text):
|
| 23 |
+
inputs = emotion_tokenizer(text, return_tensors="pt", truncation=True)
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = emotion_model(**inputs)
|
| 26 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 27 |
+
label = torch.argmax(probs).item()
|
| 28 |
+
emotions_ko = ["기쁨", "슬픔", "분노", "불안", "중립"]
|
| 29 |
+
emotions_en = ["joy", "sadness", "anger", "anxiety", "neutral"]
|
| 30 |
+
return emotions_en[label], emotions_ko[label]
|
| 31 |
+
|
| 32 |
+
stable_pipe = StableDiffusionPipeline.from_pretrained(
|
| 33 |
+
"CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16
|
| 34 |
+
)
|
| 35 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
+
stable_pipe = stable_pipe.to(device)
|
| 37 |
+
|
| 38 |
+
def generate_emotion_image(emotion_en):
|
| 39 |
+
prompt = f"A dreamy digital painting that represents the feeling of {emotion_en}"
|
| 40 |
+
image = stable_pipe(prompt).images[0]
|
| 41 |
+
image_path = f"{emotion_en}_image.png"
|
| 42 |
+
image.save(image_path)
|
| 43 |
+
return image_path
|
| 44 |
+
|
| 45 |
+
def run_all(fairytale_file, child_feeling_text):
|
| 46 |
+
title, content = load_fairytale(fairytale_file)
|
| 47 |
+
audio_path = generate_grandma_voice(content[:300])
|
| 48 |
+
emotion_en, emotion_ko = classify_emotion(child_feeling_text)
|
| 49 |
+
image_path = generate_emotion_image(emotion_en)
|
| 50 |
+
return title, audio_path, emotion_ko, image_path
|
| 51 |
+
|
| 52 |
+
demo = gr.Interface(
|
| 53 |
+
fn=run_all,
|
| 54 |
+
inputs=[
|
| 55 |
+
gr.File(label="동화 JSON 파일 업로드"),
|
| 56 |
+
gr.Textbox(label="아이의 감상문")
|
| 57 |
+
],
|
| 58 |
+
outputs=[
|
| 59 |
+
gr.Text(label="동화 제목"),
|
| 60 |
+
gr.Audio(label="할머니 목소리"),
|
| 61 |
+
gr.Text(label="감정 분석 결과 (한국어)"),
|
| 62 |
+
gr.Image(label="감정 표현 이미지")
|
| 63 |
+
],
|
| 64 |
+
title="AI 할머니가 읽어주는 감성 동화책",
|
| 65 |
+
description="동화를 업로드하면 할머니가 읽어주고, 아이 감상문에 맞춰 감정 이미지를 생성합니다."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
diffusers
|
| 4 |
+
gtts
|
| 5 |
+
gradio
|