Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
-
import subprocess
|
| 3 |
import openai
|
| 4 |
import gradio as gr
|
| 5 |
from gtts import gTTS
|
| 6 |
-
import
|
| 7 |
-
import librosa
|
| 8 |
-
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 9 |
-
|
| 10 |
-
# Load the processor and model
|
| 11 |
-
processor = AutoProcessor.from_pretrained("lyimo/whisper-small-sw2")
|
| 12 |
-
model = AutoModelForSpeechSeq2Seq.from_pretrained("lyimo/whisper-small-sw2")
|
| 13 |
|
| 14 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
|
|
|
|
|
|
| 16 |
def transcribe(audio):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
with torch.no_grad():
|
| 20 |
-
outputs = model.generate(inputs["input_features"], max_length=512, num_beams=4, early_stopping=True)
|
| 21 |
-
transcription = [processor.decode(ids) for ids in outputs]
|
| 22 |
-
return transcription[0]
|
| 23 |
|
| 24 |
def generate_response(transcribed_text):
|
| 25 |
response = openai.ChatCompletion.create(
|
| 26 |
model="gpt-3.5-turbo",
|
| 27 |
messages=[
|
| 28 |
-
{"role": "system", "content": "
|
| 29 |
{"role": "user", "content": "Mambo vipi?"},
|
| 30 |
{"role": "assistant", "content": "Salama je una swali lolote kuhusu viazi lishe?"},
|
| 31 |
{"role": "user", "content": "nini maana ya Viazi lishe?"},
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
from gtts import gTTS
|
| 5 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
+
pipe = pipeline(model="lyimo/whisper-small-sw2") # Use your custom ASR model for transcription
|
| 10 |
+
|
| 11 |
def transcribe(audio):
|
| 12 |
+
text = pipe(audio)["text"]
|
| 13 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def generate_response(transcribed_text):
|
| 16 |
response = openai.ChatCompletion.create(
|
| 17 |
model="gpt-3.5-turbo",
|
| 18 |
messages=[
|
| 19 |
+
{"role": "system", "content": "All your answers should be in swahili only, users undertands swahili only so here we start... Wewe ni mtaalamu wa viazi lishe na utajibu maswali yote kwa kiswahili tu!"},
|
| 20 |
{"role": "user", "content": "Mambo vipi?"},
|
| 21 |
{"role": "assistant", "content": "Salama je una swali lolote kuhusu viazi lishe?"},
|
| 22 |
{"role": "user", "content": "nini maana ya Viazi lishe?"},
|