File size: 2,582 Bytes
d02132a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267d304
4751f10
d02132a
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import openai
import gradio as gr
from gtts import gTTS
from transformers import pipeline
from openai import OpenAI
client = OpenAI()

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 = client.chat.completions.create(
    
        messages=[
            {"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!"},
            {"role": "user", "content": "Mambo vipi?"},
            {"role": "assistant", "content": "Salama je una swali lolote kuhusu viazi lishe?"},
            {"role": "user", "content": "nini maana ya Viazi lishe?"},
            {"role": "assistant", "content": "viazi lishe ni Viazi vitamu vyenye rangi ya karoti kwa ndani ambavyo vina vitamin A kwa wingi"},
            {"role": "user", "content": "nini matumizi ya viazi lishe?"},
            {"role": "assistant", "content": "viazi lishe vinaweza kutengenezea chakula kama Keki, Maandazi, Kalimati na tambi: Ukisaga unga wa viazi lishe, unaweza kutumika kupika vyakula ambavyo huwa watu hutumia unga wa ngano kupika, unga wa viazi lishe una virutubisho vingi zaidi kuliko unga wa ngano na ukitumika kupikia vyakula tajwa hapo juu watumiaji watakuwa wanakula vyakula vyenye virutubisho Zaidi."},
            {
                "role": "user",
                "content": transcribed_text
            },
        ],
        model="gpt-4o"
    )
    return response.choices[0].message.content

def inference(text):
   #client = openai.OpenAI(api_key='your_api_key')
   response = client.audio.speech.create(
       model="tts-1",
       voice="nova", 
       input=text
   )
   output_file = "tts_output.mp3"
   response.stream_to_file(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.Audio(sources=["microphone"], type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
   [gr.Textbox(label="Jibu (kwa njia ya maandishi)"), gr.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
   description="Uliza Mtaalamu wetu swali lolote Kuhusu viazi Lishe",
   allow_flagging=False,
   live=True,
)
demo.launch()