Aekanun commited on
Commit
755a19b
·
1 Parent(s): 25d528b
Files changed (1) hide show
  1. app.py +3 -12
app.py CHANGED
@@ -7,7 +7,7 @@ import spaces
7
  model = pipeline(
8
  "automatic-speech-recognition",
9
  model="Aekanun/whisper-small-hi",
10
- device="cpu",
11
  torch_dtype=torch.float16
12
  )
13
 
@@ -18,30 +18,21 @@ def transcribe_speech(audio):
18
  if audio is None:
19
  return "กรุณาบันทึกเสียงก่อน"
20
 
21
- # Move model to GPU with float16
22
- model.model = model.model.to("cuda").half()
23
-
24
  with torch.amp.autocast('cuda'):
25
  # Process audio with chunk_length_s
26
  result = model(
27
  audio,
28
  batch_size=1,
29
- chunk_length_s=30 # แบ่งเสียงเป็นช่วงละ 30 วินาที
30
  )
31
 
32
  # Get text result
33
  text = result["text"] if isinstance(result, dict) else result
34
 
35
- # Move model back to CPU
36
- model.model = model.model.to("cpu")
37
- torch.cuda.empty_cache()
38
-
39
  return text
40
 
41
  except Exception as e:
42
- # Make sure model is back on CPU in case of error
43
- model.model = model.model.to("cpu")
44
- torch.cuda.empty_cache()
45
  return f"เกิดข้อผิดพลาด: {str(e)}"
46
 
47
  # Create Gradio interface
 
7
  model = pipeline(
8
  "automatic-speech-recognition",
9
  model="Aekanun/whisper-small-hi",
10
+ device="cuda", # เปลี่ยนจาก cpu เป็น cuda
11
  torch_dtype=torch.float16
12
  )
13
 
 
18
  if audio is None:
19
  return "กรุณาบันทึกเสียงก่อน"
20
 
21
+ # ไม่ต้องย้ายโมเดลเพราะอยู่บน GPU อยู่แล้ว
 
 
22
  with torch.amp.autocast('cuda'):
23
  # Process audio with chunk_length_s
24
  result = model(
25
  audio,
26
  batch_size=1,
27
+ chunk_length_s=30
28
  )
29
 
30
  # Get text result
31
  text = result["text"] if isinstance(result, dict) else result
32
 
 
 
 
 
33
  return text
34
 
35
  except Exception as e:
 
 
 
36
  return f"เกิดข้อผิดพลาด: {str(e)}"
37
 
38
  # Create Gradio interface