lyimo commited on
Commit
4725d2e
1 Parent(s): 06dae33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -27
app.py CHANGED
@@ -2,50 +2,34 @@ import os
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"
48
- tts = gTTS(text, lang="sw")
49
  tts.save(output_file)
50
  return output_file
51
 
@@ -58,7 +42,7 @@ def process_audio_and_respond(audio):
58
  demo = gr.Interface(
59
  process_audio_and_respond,
60
  gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
61
- [gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"), gr.outputs.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
62
  title="Mtaalamu wa Viazi Lishe",
63
  description="Uliza Mtaalamu wetu swali lolote Kuhusu viazi Lishe",
64
  theme="compact",
 
2
  import subprocess
3
  import openai
4
  import gradio as gr
 
5
  from gtts import gTTS
6
 
7
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
8
 
9
  def transcribe(audio):
10
+ with open(audio, "rb") as audio_file:
11
+ transcript = openai.Audio.transcribe("whisper-1", audio_file)
12
+ return transcript["text"]
13
 
14
  def generate_response(transcribed_text):
15
  response = openai.ChatCompletion.create(
16
  model="gpt-3.5-turbo",
17
  messages=[
18
+ {"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!"},
19
  {"role": "user", "content": "Mambo vipi?"},
20
+ {"role": "assistant", "content": "Salama je una swali lolote kuhusu viazi lishe?"},
21
  {"role": "user", "content": "nini maana ya Viazi lishe?"},
22
+ {"role": "assistant", "content": "viazi lishe ni Viazi vitamu vyenye rangi ya karoti kwa ndani ambavyo vina vitamin A kwa wingi"},
23
  {"role": "user", "content": "nini matumizi ya viazi lishe?"},
24
+ {"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."},
 
 
 
 
 
 
25
  {"role": "user", "content": transcribed_text},
26
  ]
27
  )
28
  return response['choices'][0]['message']['content']
29
 
30
  def inference(text):
31
+ tts = gTTS(text=text, lang='sw')
32
+ output_file = "tts_output.mp3"
33
  tts.save(output_file)
34
  return output_file
35
 
 
42
  demo = gr.Interface(
43
  process_audio_and_respond,
44
  gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
45
+ [gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"), gr.outputs.Audio(type="mp3", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
46
  title="Mtaalamu wa Viazi Lishe",
47
  description="Uliza Mtaalamu wetu swali lolote Kuhusu viazi Lishe",
48
  theme="compact",