lyimo commited on
Commit
c04b26f
·
1 Parent(s): 8f06561

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -2,41 +2,46 @@ import os
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?"},
26
- {"role": "assistant", "content": """ viazi lishe ni Viazi vitamu vyenye rangi ya karoti kwa ndani ambavyo vina vitamin A kwa wingi"""},
27
- {"role": "user", "content": "nini matumizi ya viazi lishe?"},
28
- {"role": "assistant", "content": """ viazi lishe vinaweza kutengenezea chakula kama Keki,
29
- Maandazi, Kalimati na tambi: Ukisaga unga wa viazi lishe,
30
- unaweza kutumika kupika vyakula ambavyo huwa watu
31
- hutumia unga wa ngano kupika, unga wa viazi lishe una
32
- virutubisho vingi zaidi kuliko unga wa ngano na
33
- ukitumika kupikia vyakula tajwa hapo juu watumiaji
34
- watakuwa wanakula vyakula vyenye virutubisho Zaidi."""},
35
- {"role": "user", "content": transcribed_text},
36
- ]
37
- )
38
- return response['choices'][0]['message']['content']
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"