Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, set_seed
|
| 3 |
from gtts import gTTS
|
| 4 |
-
import uuid
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
generator = pipeline(
|
| 8 |
set_seed(42)
|
| 9 |
|
|
|
|
| 10 |
def chat_with_bot(prompt):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, set_seed
|
| 3 |
from gtts import gTTS
|
| 4 |
+
import uuid
|
| 5 |
+
import os
|
| 6 |
+
import speech_recognition as sr
|
| 7 |
|
| 8 |
+
# Inisialisasi model Bahasa Indonesia
|
| 9 |
+
generator = pipeline("text-generation", model="flax-community/gpt2-small-indonesian")
|
| 10 |
set_seed(42)
|
| 11 |
|
| 12 |
+
# Fungsi chatbot: teks masuk β jawaban + TTS
|
| 13 |
def chat_with_bot(prompt):
|
| 14 |
+
result = generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
| 15 |
+
response = result[len(prompt):].strip()
|
| 16 |
+
|
| 17 |
+
# Buat audio dari jawaban
|
| 18 |
+
audio_path = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 19 |
+
tts = gTTS(response, lang="id")
|
| 20 |
+
tts.save(audio_path)
|
| 21 |
+
|
| 22 |
+
return response, audio_path
|
| 23 |
+
|
| 24 |
+
# Fungsi untuk input teks atau suara
|
| 25 |
+
def transcribe_and_respond(text_input, audio_input):
|
| 26 |
+
if audio_input:
|
| 27 |
+
recognizer = sr.Recognizer()
|
| 28 |
+
with sr.AudioFile(audio_input) as source:
|
| 29 |
+
audio_data = recognizer.record(source)
|
| 30 |
+
try:
|
| 31 |
+
text_input = recognizer.recognize_google(audio_data, language="id-ID")
|
| 32 |
+
except:
|
| 33 |
+
return "β οΈ Gagal mengenali suara.", None
|
| 34 |
+
|
| 35 |
+
if not text_input.strip():
|
| 36 |
+
return "β οΈ Tidak ada input yang diproses.", None
|
| 37 |
+
|
| 38 |
+
return chat_with_bot(text_input)
|
| 39 |
+
|
| 40 |
+
# Gradio UI dengan tampilan mobile-friendly
|
| 41 |
+
with gr.Blocks(css="""
|
| 42 |
+
.gradio-container {
|
| 43 |
+
max-width: 600px !important;
|
| 44 |
+
margin: auto;
|
| 45 |
+
padding: 16px;
|
| 46 |
+
font-family: 'Segoe UI', sans-serif;
|
| 47 |
+
}
|
| 48 |
+
h1, h2 {
|
| 49 |
+
text-align: center;
|
| 50 |
+
}
|
| 51 |
+
textarea, input {
|
| 52 |
+
font-size: 16px !important;
|
| 53 |
+
}
|
| 54 |
+
@media screen and (max-width: 600px) {
|
| 55 |
+
.gradio-container {
|
| 56 |
+
padding: 12px;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
""") as demo:
|
| 60 |
+
gr.Markdown("""
|
| 61 |
+
<h1>π€ Chatbot Bahasa Indonesia</h1>
|
| 62 |
+
<p style='text-align:center;'>Tulis pertanyaan atau unggah suara Anda. Chatbot akan menjawab dan membacakan hasilnya!</p>
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
with gr.Column():
|
| 66 |
+
text_input = gr.Textbox(label="π¬ Masukkan Teks (opsional)", placeholder="Contoh: Apa itu AI?", lines=2)
|
| 67 |
+
audio_input = gr.Audio(label="π€ Upload Suara (opsional)", type="filepath")
|
| 68 |
+
submit_btn = gr.Button("π Kirim")
|
| 69 |
+
|
| 70 |
+
with gr.Column():
|
| 71 |
+
text_output = gr.Textbox(label="π§ Jawaban", lines=3)
|
| 72 |
+
audio_output = gr.Audio(label="π Jawaban Audio")
|
| 73 |
+
|
| 74 |
+
submit_btn.click(fn=transcribe_and_respond, inputs=[text_input, audio_input], outputs=[text_output, audio_output])
|
| 75 |
+
|
| 76 |
+
demo.launch()
|