Sandra Sanchez commited on
Commit ·
a8b3c67
1
Parent(s): ab11b1b
Initial commit
Browse files- .gitignore +1 -0
- app.py +90 -0
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.idea/
|
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# imports
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
from pydub import AudioSegment
|
| 9 |
+
from pydub.playback import play
|
| 10 |
+
|
| 11 |
+
# Initialization
|
| 12 |
+
|
| 13 |
+
load_dotenv(override=True)
|
| 14 |
+
|
| 15 |
+
openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 16 |
+
if openai_api_key:
|
| 17 |
+
print(f"OpenAI API Key exists and begins {openai_api_key[:8]}")
|
| 18 |
+
else:
|
| 19 |
+
print("OpenAI API Key not set")
|
| 20 |
+
|
| 21 |
+
MODEL = "gpt-4o-mini"
|
| 22 |
+
openai = OpenAI()
|
| 23 |
+
|
| 24 |
+
system_message = "You are a language tutor, and as such provide only with \
|
| 25 |
+
helpful tips and accurate translations. You are entertaining and polite. \
|
| 26 |
+
If you don't know something, you say so."
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def talker(message):
|
| 30 |
+
response = openai.audio.speech.create(
|
| 31 |
+
model="tts-1",
|
| 32 |
+
voice="nova", # Can I change the vibe parameter?
|
| 33 |
+
input=message
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
audio_stream = BytesIO(response.content)
|
| 37 |
+
audio = AudioSegment.from_file(audio_stream, format="mp3")
|
| 38 |
+
play(audio)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Transcription function
|
| 42 |
+
def transcribe_audio(audio_file):
|
| 43 |
+
# Ensure the audio file is opened as a binary file
|
| 44 |
+
with open(audio_file, "rb") as audio:
|
| 45 |
+
translation = openai.audio.translations.create(
|
| 46 |
+
model="whisper-1",
|
| 47 |
+
file=audio # Pass the opened file, not the filepath
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
print(translation.text)
|
| 51 |
+
return translation.text
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Wrapper function to combine microphone input, transcription, and chat
|
| 55 |
+
def process_microphone_input(audio, history=[]):
|
| 56 |
+
if audio is None:
|
| 57 |
+
raise ValueError("No audio input detected. Please ensure the microphone is functioning correctly.")
|
| 58 |
+
# Step 1: Transcribe the audio captured from the microphone
|
| 59 |
+
transcribed_text = transcribe_audio(audio)
|
| 60 |
+
|
| 61 |
+
# Step 2: Pass the transcription to the chat function
|
| 62 |
+
response = chat(transcribed_text, history)
|
| 63 |
+
|
| 64 |
+
return response
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def chat(message, history):
|
| 68 |
+
messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": message}]
|
| 69 |
+
response = openai.chat.completions.create(model=MODEL, messages=messages)
|
| 70 |
+
reply = response.choices[0].message.content
|
| 71 |
+
|
| 72 |
+
print(f"History: {history}")
|
| 73 |
+
print(f"Message: {message}")
|
| 74 |
+
print(f"Messages: {messages}")
|
| 75 |
+
|
| 76 |
+
talker(reply)
|
| 77 |
+
return reply
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# Gradio interface for microphone input
|
| 81 |
+
interface = gr.Interface(
|
| 82 |
+
fn=process_microphone_input,
|
| 83 |
+
inputs=[gr.Audio(sources="microphone", type="filepath")], # Microphone as input
|
| 84 |
+
outputs="text", # GPT response as text
|
| 85 |
+
title="Speech-to-Chatbot-to-Speech Language Tutor",
|
| 86 |
+
description="Speak into the microphone to chat with GPT-4. Wait a couple of seconds before you submit your message."
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|
| 3 |
+
pydub
|
| 4 |
+
dotenv
|