seeafrica commited on
Commit
b1bfa34
·
verified ·
1 Parent(s): 8598440

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -32
app.py CHANGED
@@ -1,25 +1,10 @@
1
  import os
2
  import gradio as gr
3
  from gtts import gTTS
4
- from transformers import pipeline, AutoProcessor, WhisperForConditionalGeneration
5
  from openai import OpenAI
6
 
7
  client = OpenAI()
8
 
9
- # Initialize the processor and model separately for better control
10
- processor = AutoProcessor.from_pretrained("seeafricatz/kiaziboraasr")
11
- model = WhisperForConditionalGeneration.from_pretrained("seeafricatz/kiaziboraasr")
12
-
13
- pipe = pipeline(
14
- "automatic-speech-recognition",
15
- model=model,
16
- tokenizer=processor.tokenizer,
17
- feature_extractor=processor.feature_extractor,
18
- chunk_length_s=30,
19
- return_timestamps=False,
20
- generate_kwargs={"language": "<|swahili|>", "task": "transcribe"}
21
- )
22
-
23
  def transcribe(audio):
24
  try:
25
  if audio is None:
@@ -30,23 +15,13 @@ def transcribe(audio):
30
  if not os.path.exists(audio_path):
31
  return "Audio file not found"
32
 
33
- result = pipe(
34
- audio_path,
35
- return_timestamps=False,
36
- generate_kwargs={
37
- "language": "<|swahili|>",
38
- "task": "transcribe",
39
- "num_beams": 5,
40
- "temperature": 0
41
- }
42
- )
43
-
44
- if isinstance(result, dict) and "text" in result:
45
- return result["text"]
46
- elif isinstance(result, str):
47
- return result
48
- else:
49
- return "Error in transcription format"
50
 
51
  except Exception as e:
52
  print(f"Transcription error: {str(e)}")
 
1
  import os
2
  import gradio as gr
3
  from gtts import gTTS
 
4
  from openai import OpenAI
5
 
6
  client = OpenAI()
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def transcribe(audio):
9
  try:
10
  if audio is None:
 
15
  if not os.path.exists(audio_path):
16
  return "Audio file not found"
17
 
18
+ with open(audio_path, "rb") as audio_file:
19
+ transcription = client.audio.transcriptions.create(
20
+ model="whisper-1",
21
+ file=audio_file
22
+ )
23
+
24
+ return transcription.text
 
 
 
 
 
 
 
 
 
 
25
 
26
  except Exception as e:
27
  print(f"Transcription error: {str(e)}")