File size: 1,554 Bytes
e564366
87e57f0
2eb0277
 
e564366
24fa852
87e57f0
 
 
755a19b
25d528b
87e57f0
e564366
2eb0277
87e57f0
2eb0277
87e57f0
 
 
 
755a19b
2eb0277
25d528b
 
 
 
755a19b
25d528b
2eb0277
 
 
 
87e57f0
 
 
 
 
 
 
 
526e16c
87e57f0
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from transformers import pipeline
import torch
import spaces

# Initialize model on CPU with float16
model = pipeline(
    "automatic-speech-recognition",
    model="Aekanun/whisper-small-hi",
    device="cuda",  # เปลี่ยนจาก cpu เป็น cuda
    torch_dtype=torch.float16
)

@spaces.GPU
def transcribe_speech(audio):
    """Speech transcription with GPU support"""
    try:
        if audio is None:
            return "กรุณาบันทึกเสียงก่อน"
            
        # ไม่ต้องย้ายโมเดลเพราะอยู่บน GPU อยู่แล้ว
        with torch.amp.autocast('cuda'):
            # Process audio with chunk_length_s
            result = model(
                audio, 
                batch_size=1,
                chunk_length_s=30
            )
            
            # Get text result
            text = result["text"] if isinstance(result, dict) else result
            
        return text
        
    except Exception as e:
        return f"เกิดข้อผิดพลาด: {str(e)}"

# Create Gradio interface
demo = gr.Interface(
    fn=transcribe_speech,
    inputs=gr.Audio(type="filepath"),
    outputs=gr.Textbox(label="ข้อความ"),
    title="Thai Speech Transcription",
    description="บันทึกเสียงเพื่อแปลงเป็นข้อความภาษาไทย",
)

if __name__ == "__main__":
    demo.queue().launch(server_name="0.0.0.0")