zianrahmad commited on
Commit
d5a1ffe
·
verified ·
1 Parent(s): d3f5598

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ # Load model Whisper medium fine-tuned Indonesia (paling direkomendasikan untuk bahasa Indo)
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+ pipe = pipeline(
8
+ "automatic-speech-recognition",
9
+ model="cahya/whisper-medium-id",
10
+ device=device,
11
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
12
+ )
13
+
14
+ # Force bahasa Indonesia biar lebih akurat
15
+ pipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language="id", task="transcribe")
16
+
17
+ def transcribe(audio):
18
+ if audio is None:
19
+ return "Silakan rekam suara atau upload file audio dulu ya!"
20
+ try:
21
+ text = pipe(audio, chunk_length_s=30, max_new_tokens=256)["text"]
22
+ return text.strip().capitalize() + "."
23
+ except Exception as e:
24
+ return f"Error: {str(e)} (coba audio lebih pendek)"
25
+
26
+ # Interface Gradio yang bagus
27
+ with gr.Blocks(title="Transkripsi Suara Bahasa Indonesia 🇮🇩") as demo:
28
+ gr.Markdown("# Demo Transkripsi Suara ke Teks Bahasa Indonesia")
29
+ gr.Markdown("Rekam via mikrofon atau upload audio (.wav/.mp3) → langsung jadi teks akurat! Model: [cahya/whisper-medium-id](https://huggingface.co/cahya/whisper-medium-id) – fine-tuned khusus Indo.")
30
+
31
+ with gr.Row():
32
+ with gr.Column(scale=1):
33
+ audio_input = gr.Audio(
34
+ sources=["microphone", "upload"],
35
+ type="filepath",
36
+ label="Rekam atau Upload Audio"
37
+ )
38
+ with gr.Column(scale=2):
39
+ text_output = gr.Textbox(label="Hasil Transkripsi", lines=10, placeholder="Hasil akan muncul di sini...")
40
+
41
+ btn = gr.Button("Transkripsi Sekarang!", variant="primary", size="lg")
42
+ btn.click(fn=transcribe, inputs=audio_input, outputs=text_output)
43
+
44
+ gr.Markdown("### Tips biar akurat:")
45
+ gr.Markdown("- Audio pendek (<60 detik) lebih cepat & bagus hasilnya.")
46
+ gr.Markdown("- Bicara jelas, coba contoh: 'Halo, apa kabar? Hari ini saya mau ke pasar beli sayuran segar.'")
47
+ gr.Markdown("- Kalau Space lambat, tunggu build selesai atau upgrade ke ZeroGPU (gratis).")
48
+
49
+ demo.launch()