Zeebra commited on
Commit
97ae67e
1 Parent(s): 48ffadb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -2,10 +2,8 @@ import gradio as gr
2
  import openai
3
  from decouple import config
4
  from gtts import gTTS
5
- import os
6
- import pydub
7
  import io
8
- import config
9
 
10
  openai.api_key = config.API_KEYS['openai']
11
 
@@ -19,17 +17,18 @@ def decipher(audio):
19
  global messages
20
 
21
  # Using openAI's speech to text model
22
- audio_file = open(audio, "rb")
23
- transcript = openai.Audio.transcribe("whisper-1", audio_file)
24
 
25
  messages.append({"role": "user", "content": transcript["text"]})
26
 
27
- response = openai.ChatCompletion.create(
28
- model="gpt-3.5-turbo",
29
- messages=messages
 
 
30
  )
31
 
32
- system_message = response["choices"][0]["message"]["content"]
33
  messages.append({"role": "assistant", "content": system_message})
34
 
35
  # Convert the text to audio using gTTS
@@ -39,10 +38,11 @@ def decipher(audio):
39
 
40
  # Convert the audio to a playable format using pydub
41
  audio_data.seek(0)
42
- audio = pydub.AudioSegment.from_file(audio_data, format="mp3")
43
 
44
  # Play the audio using VLC
45
- audio.play()
 
46
 
47
  chat_transcript = ""
48
  for message in messages:
@@ -52,6 +52,12 @@ def decipher(audio):
52
  return chat_transcript
53
 
54
  # Using Gradio's audio Interface
55
- interface = gr.Interface(fn=decipher, inputs=gr.Audio(
56
- source="microphone", type="filepath"), outputs="text")
57
- interface.launch()
 
 
 
 
 
 
 
2
  import openai
3
  from decouple import config
4
  from gtts import gTTS
 
 
5
  import io
6
+ import pydub
7
 
8
  openai.api_key = config.API_KEYS['openai']
9
 
 
17
  global messages
18
 
19
  # Using openAI's speech to text model
20
+ transcript = openai.Audio.transcribe("whisper-1", audio)
 
21
 
22
  messages.append({"role": "user", "content": transcript["text"]})
23
 
24
+ response = openai.Completion.create(
25
+ model="text-davinci-002",
26
+ prompt="Conversation:\n" + "\n".join([f"{m['role']}: {m['content']}" for m in messages]),
27
+ temperature=0.7,
28
+ max_tokens=1024,
29
  )
30
 
31
+ system_message = response.choices[0].text
32
  messages.append({"role": "assistant", "content": system_message})
33
 
34
  # Convert the text to audio using gTTS
 
38
 
39
  # Convert the audio to a playable format using pydub
40
  audio_data.seek(0)
41
+ audio = pydub.AudioSegment.from_file(io.BytesIO(audio_data.getvalue()))
42
 
43
  # Play the audio using VLC
44
+ player = pydub.playback.play
45
+ player(audio)
46
 
47
  chat_transcript = ""
48
  for message in messages:
 
52
  return chat_transcript
53
 
54
  # Using Gradio's audio Interface
55
+ interface = gr.Interface(fn=decipher, inputs=gr.inputs.Audio(
56
+ type="file", label="Record a voice message"),
57
+ outputs="text",
58
+ title="AI Assistant",
59
+ description="An AI assistant that can transcribe and respond to voice messages",
60
+ theme="compact")
61
+
62
+ if __name__ == "__main__":
63
+ interface.launch()