Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,31 @@ import os
|
|
| 2 |
import subprocess
|
| 3 |
import openai
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
-
import os
|
| 8 |
-
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def generate_response(transcribed_text):
|
| 19 |
response = openai.ChatCompletion.create(
|
| 20 |
model="gpt-3.5-turbo",
|
| 21 |
messages=[
|
| 22 |
-
{"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!"},
|
| 23 |
{"role": "user", "content": "Mambo vipi?"},
|
| 24 |
{"role": "assistant", "content": """Salama je una swali lolote kuhusu viazi lishe?"""},
|
| 25 |
{"role": "user", "content": "nini maana ya Viazi lishe?"},
|
|
@@ -37,32 +44,28 @@ def generate_response(transcribed_text):
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
| 40 |
-
|
| 41 |
def inference(text):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
def process_audio_and_respond(audio):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
demo = gr.Interface(
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
demo.launch()
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import openai
|
| 4 |
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
from gtts import gTTS
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
+
API_URL = "https://api-inference.huggingface.co/models/lyimo/whisper-small-sw2"
|
| 12 |
+
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
|
| 13 |
|
| 14 |
+
def query(filename):
|
| 15 |
+
with open(filename, "rb") as f:
|
| 16 |
+
data = f.read()
|
| 17 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
| 18 |
+
return response.json()
|
| 19 |
|
| 20 |
|
| 21 |
+
def transcribe(audio):
|
| 22 |
+
output = query(audio)
|
| 23 |
+
return output["text"]
|
| 24 |
+
|
| 25 |
def generate_response(transcribed_text):
|
| 26 |
response = openai.ChatCompletion.create(
|
| 27 |
model="gpt-3.5-turbo",
|
| 28 |
messages=[
|
| 29 |
+
{"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!"},
|
| 30 |
{"role": "user", "content": "Mambo vipi?"},
|
| 31 |
{"role": "assistant", "content": """Salama je una swali lolote kuhusu viazi lishe?"""},
|
| 32 |
{"role": "user", "content": "nini maana ya Viazi lishe?"},
|
|
|
|
| 44 |
)
|
| 45 |
return response['choices'][0]['message']['content']
|
| 46 |
|
|
|
|
| 47 |
def inference(text):
|
| 48 |
+
output_file = "tts_output.wav"
|
| 49 |
+
tts = gTTS(text, lang="sw")
|
| 50 |
+
tts.save(output_file)
|
| 51 |
+
return output_file
|
|
|
|
| 52 |
|
| 53 |
def process_audio_and_respond(audio):
|
| 54 |
+
text = transcribe(audio)
|
| 55 |
+
response_text = generate_response(text)
|
| 56 |
+
output_file = inference(response_text)
|
| 57 |
+
return response_text, output_file
|
|
|
|
| 58 |
|
| 59 |
demo = gr.Interface(
|
| 60 |
+
process_audio_and_respond,
|
| 61 |
+
gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
|
| 62 |
+
[gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"), gr.outputs.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
|
| 63 |
+
title="Mtaalamu wa Viazi Lishe",
|
| 64 |
+
description="Uliza Mtaalamu wetu swali lolote Kuhusu viazi Lishe",
|
| 65 |
+
theme="compact",
|
| 66 |
+
layout="vertical",
|
| 67 |
+
allow_flagging=False,
|
| 68 |
+
live=True,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
demo.launch()
|
|
|