hakiV1 / app.py
seeafrica's picture
Update app.py
5a66f90
import os
import openai
import gradio as gr
from gtts import gTTS
from transformers import pipeline
openai.api_key = os.getenv("OPENAI_API_KEY")
pipe = pipeline(model="seeafricatz/kiaziboraasr")
def transcribe(audio):
text = pipe(audio)["text"]
return text
def generate_response(transcribed_text):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "All your answers should be in Swahili only, users understand Swahili only so here we start... Wewe ni mtaalamu wa haki za ardhi za wanawake nchini Kongo na utajibu maswali yote kwa Kiswahili tu!"
},
{
"role": "user",
"content": "Mambo vipi?"
},
{
"role": "assistant",
"content": "Salama, je una swali lolote kuhusu haki za ardhi za wanawake nchini Kongo?"
},
{
"role": "user",
"content": "nini maana ya haki za ardhi za wanawake?"
},
{
"role": "assistant",
"content": "Haki za ardhi za wanawake zinamaanisha haki za wanawake kumiliki, kutumia, na kudhibiti ardhi. Katika muktadha wa Kongo, haki hizi zinaweza kuathiriwa na mila, sheria, na mizozo ya ardhi."
},
{
"role": "user",
"content": "nini matumizi ya haki za ardhi za wanawake?"
},
{
"role": "assistant",
"content": "Haki za ardhi za wanawake zina umuhimu mkubwa kwa kuwawezesha wanawake kiuchumi, kuimarisha usalama wa chakula, na kuchangia katika maendeleo ya jamii na taifa kwa ujumla. Kwa mfano, wanawake wenye haki za ardhi wanaweza kupata mikopo, kuendeleza ardhi, na kutoa mchango muhimu katika uchumi wa familia na jamii."
},
{
"role": "user",
"content": transcribed_text
},
]
)
return response['choices'][0]['message']['content']
def inference(text):
tts = gTTS(text=text, lang='sw', tld='com.mx',slow=False)
output_file = "tts_output.mp3"
tts.save(output_file)
return output_file
def process_audio_and_respond(audio):
text = transcribe(audio)
response_text = generate_response(text)
output_file = inference(response_text)
return response_text, output_file
demo = gr.Interface(
process_audio_and_respond,
gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
[gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"), gr.outputs.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
title="Haki",
description="Uliza Swali kuhusu haki za ardhi",
theme="compact",
layout="vertical",
allow_flagging=False,
live=True,
)
demo.launch()