roots / app.py
haepada's picture
Update app.py
ed44b5b verified
raw
history blame
10.7 kB
import gradio as gr
import numpy as np
import librosa
from transformers import pipeline
from datetime import datetime
import os
import requests
# 환경변수에서 토큰 가져오기
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
if not HF_API_TOKEN:
raise ValueError("HF_API_TOKEN not found in environment variables")
# Inference API 설정
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
# AI 모델 초기화
speech_recognizer = pipeline(
"automatic-speech-recognition",
model="kresnik/wav2vec2-large-xlsr-korean"
)
emotion_classifier = pipeline(
"audio-classification",
model="MIT/ast-finetuned-speech-commands-v2"
)
text_analyzer = pipeline(
"sentiment-analysis",
model="nlptown/bert-base-multilingual-uncased-sentiment"
)
korean_sentiment = pipeline(
"text-classification",
model="searle-j/korean_sentiment_analysis"
)
# 유틸리티 함수들
def map_acoustic_to_emotion(features):
"""음향학적 특성을 감정으로 매핑"""
intensity = features["energy"] * 100
if features["energy"] > 0.7:
if features["tempo"] > 120:
emotion = "기쁨/흥분"
else:
emotion = "분노/강조"
elif features["pitch"] > 0.6:
emotion = "놀람/관심"
elif features["energy"] < 0.3:
emotion = "슬픔/우울"
else:
emotion = "평온/중립"
return {
"emotion": emotion,
"intensity": intensity,
"features": features
}
def generate_detailed_prompt(text, voice_emotion, text_sentiment, acoustic_features):
"""프롬프트 생성"""
emotion_colors = {
"기쁨/흥분": "밝은 노랑과 주황색",
"분노/강조": "강렬한 빨강과 검정",
"놀람/관심": "선명한 파랑과 보라",
"슬픔/우울": "어두운 파랑과 회색",
"평온/중립": "부드러운 초록과 베이지"
}
visual_elements = {
"high_energy": "역동적인 붓질과 강한 대비",
"medium_energy": "균형잡힌 구도와 자연스러운 흐름",
"low_energy": "부드러운 그라데이션과 차분한 톤"
}
energy_level = "medium_energy"
if acoustic_features["energy"] > 0.7:
energy_level = "high_energy"
elif acoustic_features["energy"] < 0.3:
energy_level = "low_energy"
prompt = f"한국 전통 민화 스타일의 추상화, {emotion_colors.get(voice_emotion['emotion'], '자연스러운 색상')} 기반. "
prompt += f"{visual_elements[energy_level]}를 통해 감정의 깊이를 표현. "
prompt += f"음성의 {voice_emotion['emotion']} 감정과 텍스트의 {text_sentiment['label']} 감정이 조화를 이루며, "
prompt += f"목소리의 특징(강도:{voice_emotion['intensity']:.1f})을 화면의 동적인 요소로 표현. "
prompt += f"발화 내용 '{text}'의 의미를 은유적 이미지로 담아내기."
return prompt
def generate_image_from_prompt(prompt):
"""이미지 생성"""
print(f"Generating image with prompt: {prompt}")
try:
if not prompt:
return None
response = requests.post(
API_URL,
headers=headers,
json={
"inputs": prompt,
"parameters": {
"negative_prompt": "ugly, blurry, poor quality, distorted",
"num_inference_steps": 30,
"guidance_scale": 7.5
}
}
)
if response.status_code == 200:
return response.content
else:
print(f"Error: {response.status_code}")
print(f"Response: {response.text}")
return None
except Exception as e:
print(f"Error generating image: {str(e)}")
return None
def create_interface():
with gr.Blocks(theme=gr.themes.Soft()) as app:
# 상태 관리
state = gr.State({
"user_name": "",
"reflections": [],
"voice_analysis": None,
"final_prompt": ""
})
# 헤더
header = gr.Markdown("# 디지털 굿판")
user_display = gr.Markdown("")
with gr.Tabs() as tabs:
# 입장
with gr.Tab("입장"):
gr.Markdown("""# 디지털 굿판에 오신 것을 환영합니다""")
name_input = gr.Textbox(label="이름을 알려주세요")
start_btn = gr.Button("여정 시작하기")
# 청신
with gr.Tab("청신"):
with gr.Row():
audio_path = os.path.abspath(os.path.join("assets", "main_music.mp3"))
audio = gr.Audio(
value=audio_path,
type="filepath",
label="온천천의 소리",
interactive=False,
autoplay=True
)
with gr.Column():
reflection_input = gr.Textbox(
label="현재 순간의 감상을 적어주세요",
lines=3
)
save_btn = gr.Button("감상 저장하기")
reflections_display = gr.Dataframe(
headers=["시간", "감상", "감정 분석"],
label="기록된 감상들"
)
# 기원
with gr.Tab("기원"):
gr.Markdown("## 기원 - 목소리로 전하기")
with gr.Row():
with gr.Column():
voice_input = gr.Audio(
label="나누고 싶은 이야기를 들려주세요",
sources=["microphone"],
type="filepath",
interactive=True
)
clear_btn = gr.Button("녹음 지우기")
with gr.Column():
transcribed_text = gr.Textbox(
label="인식된 텍스트",
interactive=False
)
voice_emotion = gr.Textbox(
label="음성 감정 분석",
interactive=False
)
text_emotion = gr.Textbox(
label="텍스트 감정 분석",
interactive=False
)
analyze_btn = gr.Button("분석하기")
# 송신
with gr.Tab("송신"):
gr.Markdown("## 송신 - 시각화 결과")
with gr.Column():
final_prompt = gr.Textbox(
label="생성된 프롬프트",
interactive=False,
lines=3
)
generate_btn = gr.Button("이미지 생성하기")
result_image = gr.Image(
label="생성된 이미지",
type="pil"
)
# 인터페이스 함수들
def start_journey(name):
"""여정 시작"""
return f"# 환영합니다, {name}님의 디지털 굿판", gr.update(selected="청신")
def clear_voice_input():
"""음성 입력 초기화"""
return None
def analyze_voice(audio_path, state):
"""음성 분석"""
if audio_path is None:
return state, "음성을 먼저 녹음해주세요.", "", "", ""
try:
y, sr = librosa.load(audio_path, sr=16000)
acoustic_features = {
"energy": float(np.mean(librosa.feature.rms(y=y))),
"tempo": float(librosa.beat.tempo(y)[0]),
"pitch": float(np.mean(librosa.feature.zero_crossing_rate(y))),
"volume": float(np.mean(np.abs(y)))
}
voice_emotion = map_acoustic_to_emotion(acoustic_features)
transcription = speech_recognizer(y)
text = transcription["text"]
text_sentiment = korean_sentiment(text)[0]
voice_result = f"음성 감정: {voice_emotion['emotion']} (강도: {voice_emotion['intensity']:.2f})"
text_result = f"텍스트 감정: {text_sentiment['label']} ({text_sentiment['score']:.2f})"
prompt = generate_detailed_prompt(text, voice_emotion, text_sentiment, acoustic_features)
return state, text, voice_result, text_result, prompt
except Exception as e:
return state, f"오류 발생: {str(e)}", "", "", ""
def save_reflection(text, state):
"""감상 저장"""
if not text.strip():
return state, state["reflections"]
current_time = datetime.now().strftime("%H:%M:%S")
sentiment = text_analyzer(text)[0]
new_reflection = [current_time, text, f"{sentiment['label']} ({sentiment['score']:.2f})"]
if "reflections" not in state:
state["reflections"] = []
state["reflections"].append(new_reflection)
return state, state["reflections"]
# 이벤트 연결
start_btn.click(
fn=lambda name: (f"# 환영합니다, {name}님의 디지털 굿판", gr.update(selected="청신")),
inputs=[name_input],
outputs=[user_display, tabs]
)
save_btn.click(
fn=save_reflection,
inputs=[reflection_input, state],
outputs=[state, reflections_display]
)
clear_btn.click(
fn=clear_voice_input,
inputs=[],
outputs=[voice_input]
)
analyze_btn.click(
fn=analyze_voice,
inputs=[voice_input, state],
outputs=[state, transcribed_text, voice_emotion, text_emotion, final_prompt]
)
generate_btn.click(
fn=generate_image_from_prompt,
inputs=[final_prompt],
outputs=[result_image]
)
return app
if __name__ == "__main__":
demo = create_interface()
demo.launch(debug=True)