Update app.py
Browse files
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 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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)}")
|