Aekanun commited on
Commit
526e16c
·
1 Parent(s): 4fceee3
Files changed (1) hide show
  1. app.py +10 -23
app.py CHANGED
@@ -1,49 +1,36 @@
 
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="บันทึกเสียงเพื่อแปลงเป็นข้อความภาษาไทย",
 
1
+ import spaces
2
  import gradio as gr
3
  from transformers import pipeline
 
 
4
 
5
+ # Initialize model and move to GPU
6
  model = pipeline(
7
  "automatic-speech-recognition",
8
  model="Aekanun/whisper-small-hi",
9
+ device="cuda" # เปลี่ยนเป็น cuda เลย
10
  )
11
 
12
+ @spaces.GPU # GPU function with default 60s duration
13
  def transcribe_speech(audio):
14
+ """Speech transcription function"""
15
  try:
16
  if audio is None:
17
  return "กรุณาบันทึกเสียงก่อน"
18
 
19
+ # Process audio (model is already on GPU)
20
+ result = model(audio, batch_size=1)
21
 
22
+ # Get text result
23
+ text = result["text"] if isinstance(result, dict) else result
 
 
 
 
 
 
 
 
 
24
 
25
  return text
26
 
27
  except Exception as e:
 
 
 
28
  return f"เกิดข้อผิดพลาด: {str(e)}"
29
 
30
  # Create Gradio interface
31
  demo = gr.Interface(
32
  fn=transcribe_speech,
33
+ inputs=gr.Audio(type="filepath"),
34
  outputs=gr.Textbox(label="ข้อความ"),
35
  title="Thai Speech Transcription",
36
  description="บันทึกเสียงเพื่อแปลงเป็นข้อความภาษาไทย",