Update app.py
Browse files
app.py
CHANGED
@@ -2,41 +2,46 @@ import os
|
|
2 |
import subprocess
|
3 |
import openai
|
4 |
import gradio as gr
|
|
|
|
|
5 |
|
|
|
|
|
6 |
|
7 |
-
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
|
12 |
def transcribe(audio):
|
13 |
-
|
14 |
-
|
15 |
-
return transcript["text"]
|
16 |
-
|
17 |
|
18 |
def generate_response(transcribed_text):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
def inference(text):
|
42 |
output_file = "tts_output.wav"
|
|
|
2 |
import subprocess
|
3 |
import openai
|
4 |
import gradio as gr
|
5 |
+
import requests
|
6 |
+
from gtts import gTTS
|
7 |
|
8 |
+
API_URL = "https://api-inference.huggingface.co/models/lyimo/whisper-small-sw2"
|
9 |
+
headers = {"Authorization": "Bearer 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"}
|
10 |
|
11 |
+
openai.api_key = "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"
|
12 |
|
13 |
+
def query(filename):
|
14 |
+
with open(filename, "rb") as f:
|
15 |
+
data = f.read()
|
16 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
17 |
+
return response.json()
|
18 |
|
19 |
|
20 |
def transcribe(audio):
|
21 |
+
output = query(audio)
|
22 |
+
return output["text"]
|
|
|
|
|
23 |
|
24 |
def generate_response(transcribed_text):
|
25 |
+
response = openai.ChatCompletion.create(
|
26 |
+
model="gpt-3.5-turbo",
|
27 |
+
messages=[
|
28 |
+
{"role": "system", "content": "Wewe ni mtaalamu wa viazi lishe na utajibu maswali yote kwa kiswahili tu!"},
|
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?"},
|
32 |
+
{"role": "assistant", "content": """ viazi lishe ni Viazi vitamu vyenye rangi ya karoti kwa ndani ambavyo vina vitamin A kwa wingi"""},
|
33 |
+
{"role": "user", "content": "nini matumizi ya viazi lishe?"},
|
34 |
+
{"role": "assistant", "content": """ viazi lishe vinaweza kutengenezea chakula kama Keki,
|
35 |
+
Maandazi, Kalimati na tambi: Ukisaga unga wa viazi lishe,
|
36 |
+
unaweza kutumika kupika vyakula ambavyo huwa watu
|
37 |
+
hutumia unga wa ngano kupika, unga wa viazi lishe una
|
38 |
+
virutubisho vingi zaidi kuliko unga wa ngano na
|
39 |
+
ukitumika kupikia vyakula tajwa hapo juu watumiaji
|
40 |
+
watakuwa wanakula vyakula vyenye virutubisho Zaidi."""},
|
41 |
+
{"role": "user", "content": transcribed_text},
|
42 |
+
]
|
43 |
+
)
|
44 |
+
return response['choices'][0]['message']['content']
|
|
|
45 |
|
46 |
def inference(text):
|
47 |
output_file = "tts_output.wav"
|