seeafrica commited on
Commit
beeb092
1 Parent(s): a7ef35d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="seeafricatz/kiaziboraasr")
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
+ {
20
+ "role": "system",
21
+ "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!"
22
+ },
23
+ {
24
+ "role": "user",
25
+ "content": "Mambo vipi?"
26
+ },
27
+ {
28
+ "role": "assistant",
29
+ "content": "Salama, je una swali lolote kuhusu haki za ardhi za wanawake nchini Kongo?"
30
+ },
31
+ {
32
+ "role": "user",
33
+ "content": "nini maana ya haki za ardhi za wanawake?"
34
+ },
35
+ {
36
+ "role": "assistant",
37
+ "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."
38
+ },
39
+ {
40
+ "role": "user",
41
+ "content": "nini matumizi ya haki za ardhi za wanawake?"
42
+ },
43
+ {
44
+ "role": "assistant",
45
+ "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."
46
+ },
47
+ {
48
+ "role": "user",
49
+ "content": transcribed_text
50
+ },
51
+ ]
52
+ )
53
+ return response['choices'][0]['message']['content']
54
+
55
+
56
+ def inference(text):
57
+ tts = gTTS(text=text, lang='sw', tld='com.mx',slow=False)
58
+ output_file = "tts_output.mp3"
59
+ tts.save(output_file)
60
+ return output_file
61
+
62
+ def process_audio_and_respond(audio):
63
+ text = transcribe(audio)
64
+ response_text = generate_response(text)
65
+ output_file = inference(response_text)
66
+ return response_text, output_file
67
+
68
+ demo = gr.Interface(
69
+ process_audio_and_respond,
70
+ gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
71
+ [gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"), gr.outputs.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
72
+ title="Mtaalamu wa Viazi Lishe",
73
+ description="Uliza Swali kuhusu haki za ardhi",
74
+ theme="compact",
75
+ layout="vertical",
76
+ allow_flagging=False,
77
+ live=True,
78
+ )
79
+
80
+ demo.launch()