roots / app.py
haepada's picture
Update app.py
7ec9920 verified
raw
history blame
2.93 kB
import gradio as gr
import numpy as np
import librosa
from transformers import pipeline
from datetime import datetime
# 모델 초기화
text_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
def create_interface():
with gr.Blocks(theme=gr.themes.Soft()) as app:
# 상태 관리
state = gr.State({
"stage": "intro",
"reflections": [],
"user_name": ""
})
# 헤더
gr.Markdown("# 디지털 굿판")
# 입장 화면
with gr.Tab("입장"):
name_input = gr.Textbox(label="이름을 알려주세요")
start_button = gr.Button("여정 시작하기")
# 청신 화면
with gr.Tab("청신"):
# 음악 플레이어
audio_player = gr.Audio(
value="assets/main_music.mp3", # 음악 파일 경로
type="filepath",
label="온천천의 소리"
)
# 감상 입력
reflection_text = gr.Textbox(
label="현재 순간의 감상을 적어주세요",
lines=3
)
save_button = gr.Button("감상 저장")
reflections_display = gr.Dataframe(
headers=["시간", "감상", "감정"],
label="기록된 감상들"
)
# 기원 화면
with gr.Tab("기원"):
voice_input = gr.Audio(
label="나누고 싶은 이야기를 들려주세요",
sources=["microphone"],
type="filepath"
)
analysis_output = gr.JSON(label="분석 결과")
# 송신 화면
with gr.Tab("송신"):
final_prompt = gr.Textbox(label="생성된 프롬프트")
# 함수 정의
def save_reflection(text, state):
if not text.strip():
return state, None
try:
# 현재 시간
current_time = datetime.now().strftime("%H:%M:%S")
# 감정 분석
sentiment = text_analyzer(text)[0]
# 새로운 감상 추가
new_reflection = [current_time, text, sentiment["label"]]
state["reflections"].append(new_reflection)
return state, state["reflections"]
except Exception as e:
return state, f"오류 발생: {str(e)}"
# 이벤트 연결
save_button.click(
fn=save_reflection,
inputs=[reflection_text, state],
outputs=[state, reflections_display]
)
return app
# 앱 실행
if __name__ == "__main__":
interface = create_interface()
interface.launch()