Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,4 +73,88 @@ def transcribe_audio(audio_file):
|
|
| 73 |
None,
|
| 74 |
None
|
| 75 |
)
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
None,
|
| 74 |
None
|
| 75 |
)
|
| 76 |
+
|
| 77 |
+
duration = get_audio_duration(audio_file)
|
| 78 |
+
if duration is None:
|
| 79 |
+
return "β Gagal membaca durasi audio.", None, None, None, None
|
| 80 |
+
|
| 81 |
+
if duration < 1.5:
|
| 82 |
+
return f"β οΈ Audio terlalu pendek ({duration} detik).", None, None, None, None
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
result = model.transcribe(audio_file)
|
| 86 |
+
raw_text = result["text"]
|
| 87 |
+
language = result["language"]
|
| 88 |
+
|
| 89 |
+
# Validasi transkripsi
|
| 90 |
+
is_valid, validated_text_or_msg = validate_transcription(raw_text)
|
| 91 |
+
if not is_valid:
|
| 92 |
+
return (
|
| 93 |
+
f"β οΈ Transkripsi tidak valid: {validated_text_or_msg}",
|
| 94 |
+
raw_text,
|
| 95 |
+
None,
|
| 96 |
+
validated_text_or_msg,
|
| 97 |
+
"tidak terdeteksi"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Simpan ke file
|
| 101 |
+
file_id = str(uuid.uuid4())[:8]
|
| 102 |
+
output_path = f"/tmp/transcript_{file_id}.txt"
|
| 103 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 104 |
+
f.write(validated_text_or_msg)
|
| 105 |
+
|
| 106 |
+
# Analisis emosi
|
| 107 |
+
emotion = detect_emotion(validated_text_or_msg)
|
| 108 |
+
|
| 109 |
+
# Respons chatbot
|
| 110 |
+
response = generate_response(validated_text_or_msg, emotion)
|
| 111 |
+
|
| 112 |
+
return (
|
| 113 |
+
f"β
Transkripsi selesai. Bahasa: **{language}**, Durasi: **{duration} detik**",
|
| 114 |
+
validated_text_or_msg,
|
| 115 |
+
output_path,
|
| 116 |
+
response,
|
| 117 |
+
f"π§ Emosi terdeteksi: **{emotion}**"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
return f"β Gagal transkripsi: {str(e)}", None, None, None, None
|
| 122 |
+
|
| 123 |
+
# UI dengan tema khas Indonesia
|
| 124 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 125 |
+
gr.Markdown("## π£οΈ Chatbot Suara Empatik untuk Skrining Awal Depresi")
|
| 126 |
+
gr.Markdown(
|
| 127 |
+
"""
|
| 128 |
+
Rekam suara langsung dari mikrofon. Chatbot akan mengenali isi percakapan,
|
| 129 |
+
menganalisis emosi, dan memberikan respons empatik.
|
| 130 |
+
Cocok untuk skrining awal kondisi psikologis secara anonim.
|
| 131 |
+
"""
|
| 132 |
+
)
|
| 133 |
+
gr.Markdown(
|
| 134 |
+
"β οΈ *Percakapan ini bersifat anonim dan hanya untuk keperluan skrining awal. "
|
| 135 |
+
"Tidak menggantikan diagnosis profesional.*"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
with gr.Row():
|
| 139 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="ποΈ Rekam Suara")
|
| 140 |
+
file_output = gr.File(label="π Unduh Transkripsi")
|
| 141 |
+
|
| 142 |
+
with gr.Row():
|
| 143 |
+
transcript_output = gr.Textbox(label="π Hasil Transkripsi", lines=4)
|
| 144 |
+
response_output = gr.Textbox(label="π¬ Respons Chatbot", lines=3)
|
| 145 |
+
|
| 146 |
+
emotion_output = gr.Markdown()
|
| 147 |
+
status_output = gr.Markdown()
|
| 148 |
+
|
| 149 |
+
def chatbot_ui(audio):
|
| 150 |
+
return transcribe_audio(audio)
|
| 151 |
+
|
| 152 |
+
audio_input.change(fn=chatbot_ui, inputs=audio_input, outputs=[
|
| 153 |
+
status_output,
|
| 154 |
+
transcript_output,
|
| 155 |
+
file_output,
|
| 156 |
+
response_output,
|
| 157 |
+
emotion_output
|
| 158 |
+
])
|
| 159 |
+
|
| 160 |
+
demo.launch()
|