Spaces:
Running
on
Zero
Running
on
Zero
rev app
Browse files
app.py
CHANGED
@@ -1,7 +1,53 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
import spaces
|
5 |
|
6 |
+
# Initialize model on CPU
|
7 |
+
model = pipeline(
|
8 |
+
"automatic-speech-recognition",
|
9 |
+
model="Aekanun/whisper-small-hi",
|
10 |
+
device="cpu"
|
11 |
+
)
|
12 |
|
13 |
+
@spaces.GPU
|
14 |
+
def transcribe_speech(audio):
|
15 |
+
"""Speech transcription with GPU support"""
|
16 |
+
try:
|
17 |
+
if audio is None:
|
18 |
+
return "กรุณาบันทึกเสียงก่อน"
|
19 |
+
|
20 |
+
# Move model to GPU
|
21 |
+
model.model = model.model.to("cuda")
|
22 |
+
|
23 |
+
# Make sure input is on the same device as model
|
24 |
+
with torch.cuda.amp.autocast():
|
25 |
+
# Process audio
|
26 |
+
result = model(audio, batch_size=1)
|
27 |
+
|
28 |
+
# Get text result
|
29 |
+
text = result["text"] if isinstance(result, dict) else result
|
30 |
+
|
31 |
+
# Move model back to CPU
|
32 |
+
model.model = model.model.to("cpu")
|
33 |
+
torch.cuda.empty_cache()
|
34 |
+
|
35 |
+
return text
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
# Make sure model is back on CPU in case of error
|
39 |
+
model.model = model.model.to("cpu")
|
40 |
+
torch.cuda.empty_cache()
|
41 |
+
return f"เกิดข้อผิดพลาด: {str(e)}"
|
42 |
+
|
43 |
+
# Create Gradio interface
|
44 |
+
demo = gr.Interface(
|
45 |
+
fn=transcribe_speech,
|
46 |
+
inputs=gr.Audio(type="filepath"), # Simplified Audio component
|
47 |
+
outputs=gr.Textbox(label="ข้อความ"),
|
48 |
+
title="Thai Speech Transcription",
|
49 |
+
description="บันทึกเสียงเพื่อแปลงเป็นข้อความภาษาไทย",
|
50 |
+
)
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
demo.queue().launch(server_name="0.0.0.0")
|