luna-cry / interface.py
Chinnatip Taemkaeo
finished text
050929c
import os
import shutil
import gradio as gr
from transformers import pipeline
# ✅ ตรวจสอบว่า ffmpeg พร้อมใช้งานหรือไม่
ffmpeg_path = shutil.which("ffmpeg")
if ffmpeg_path:
print(f"✅ ffmpeg found at: {ffmpeg_path}")
else:
print("❌ ffmpeg NOT found! Audio processing will fail.")
# ✅ Debug Log: ตรวจสอบว่าโค้ดถึงขั้นตอนไหน
print("🚀 Application Starting...")
# ตั้งค่าพื้นที่เก็บโมเดล
os.environ["HF_HOME"] = "/app/cache"
print(f"✅ HF_HOME set to: {os.environ.get('HF_HOME')}")
try:
# ✅ Debug Log: ตรวจสอบว่าระบบเริ่มโหลดโมเดลไหม
print("🔄 Initializing model pipeline...")
classifier = pipeline("audio-classification",
model="AmeerHesham/distilhubert-finetuned-baby_cry",
device=-1)
print("✅ Model loaded successfully!")
except Exception as e:
print(f"❌ Error loading model: {str(e)}")
# ฟังก์ชันสำหรับรัน Gradio
def classify_audio(audio_path):
try:
result = classifier(audio_path)
return result
except Exception as e:
return {"error": str(e)}
# ✅ Debug Log: ตรวจสอบว่า Gradio interface เริ่มทำงานไหม
print("🔄 Launching Gradio interface...")
iface = gr.Interface(
fn=classify_audio,
inputs=gr.Audio(type="filepath"),
outputs="json",
title="👶 AI วิเคราะห์เสียงทารก",
description="กดอัดเสียงน้อง หรืออัพโหลดไฟล์เสียง (.wav) จากนั้นกด submit เพื่อให้ AI วิเคราะห์อารมณ์ที่น้องร้องได้เลย",
flagging_dir="/app/flagged"
)
# ✅ ใช้ server_name="0.0.0.0" และ server_port=7860 เพื่อให้ Hugging Face Spaces เห็น UI
print("🚀 Gradio is running...")
iface.launch(server_name="0.0.0.0", server_port=7860)