Update app.py
Browse files
app.py
CHANGED
@@ -2,31 +2,24 @@ import os
|
|
2 |
import subprocess
|
3 |
import openai
|
4 |
import gradio as gr
|
5 |
-
import requests
|
6 |
-
from gtts import gTTS
|
7 |
|
8 |
|
9 |
-
|
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 |
-
|
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 |
-
|
23 |
-
|
|
|
|
|
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
|
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,28 +37,32 @@ def generate_response(transcribed_text):
|
|
44 |
)
|
45 |
return response['choices'][0]['message']['content']
|
46 |
|
|
|
47 |
def inference(text):
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
|
53 |
def process_audio_and_respond(audio):
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
demo = gr.Interface(
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
)
|
70 |
|
|
|
71 |
demo.launch()
|
|
|
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 transcribe(audio):
|
13 |
+
with open(audio, "rb") as audio_file:
|
14 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
15 |
+
return transcript["text"]
|
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 |
)
|
38 |
return response['choices'][0]['message']['content']
|
39 |
|
40 |
+
|
41 |
def inference(text):
|
42 |
+
output_file = "tts_output.wav"
|
43 |
+
cmd = ['tts', '--text', text, '--out_path', output_file]
|
44 |
+
subprocess.run(cmd, check=True)
|
45 |
+
return output_file
|
46 |
+
|
47 |
|
48 |
def process_audio_and_respond(audio):
|
49 |
+
text = transcribe(audio)
|
50 |
+
response_text = generate_response(text)
|
51 |
+
output_file = inference(response_text)
|
52 |
+
return response_text, output_file
|
53 |
+
|
54 |
|
55 |
demo = gr.Interface(
|
56 |
+
process_audio_and_respond,
|
57 |
+
gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
|
58 |
+
[gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"), gr.outputs.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
|
59 |
+
title="Mtaalamu wa Viazi Lishe",
|
60 |
+
description="Uliza Mtaalamu wetu swali lolote Kuhusu viazi Lishe",
|
61 |
+
theme="compact",
|
62 |
+
layout="vertical",
|
63 |
+
allow_flagging=False,
|
64 |
+
live=True,
|
65 |
)
|
66 |
|
67 |
+
|
68 |
demo.launch()
|