import os import openai import time import numpy import gradio as gr import soundfile as sf from pydub import AudioSegment from openai import OpenAI # Load API key from an environment variable OPENAI_SECRET_KEY = os.environ.get("OPENAI_SECRET_KEY") client = OpenAI(api_key = OPENAI_SECRET_KEY) ####################### Choose Question Type language_type_map = { "French": "FrenchTutor.txt", "Spanish": "SpanishTutor.txt", } def generate_question(language_type): ######################## Create Sample Question ################## if language_type == "French": with open(f"Format_Library/FrenchQuestion.txt", "r") as f: FrenchQGen = f.read() print(FrenchQGen+"/n/n") messages = [ {"role": "system", "content": FrenchQGen}, {"role": "user", "content": "Donnez moi le context et la question."} ] else: messages = [ {"role": "system", "content": "Eres una máquina de preguntas que crea preguntas para estimular una respuesta de un estudiante intermedio de Espangol. La pregunta debe tener un contexto, pero el contexto puede ser cualquier cosa. Favorece los contextos cotidianos y evita los temas turísticos. Proporciona el contexto seguido de la pregunta."}, {"role": "user", "content": "Proporcióneme el contexto y la pregunta."} ] question = client.chat.completions.create(model="gpt-3.5-turbo-1106", temperature=1, messages=messages) question_text=question.choices[0].message.content return question_text def generate_feedback(recorder,language_type): audio_data, sr = sf.read(recorder) with sf.SoundFile("Audio_Files/test.mp3", mode='w', samplerate=sr, channels=len(audio_data.shape)) as file: file.write(audio_data) file_size = os.path.getsize("Audio_Files/test.mp3") print(f"File size after writing {file_size} bytes") ##################################Send file to Whisper for Transcription with open("Audio_Files/test.mp3", "rb") as audio_file: max_attempts = 3 attempt = 0 while attempt < max_attempts: try: audio_transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file) break except openai.error.APIConnectionError as e: print(f"Attempt {attempt + 1} failed with error: {e}") attempt += 1 time.sleep(3) # wait for 3 seconds before retrying else: print("Failed to transcribe audio after multiple attempts") print(audio_transcript.text+"\n\n") ####################################Ask OpenAI to provide feedback on audio response file_name = language_type_map.get(language_type, "FrenchTutor.txt") with open(f"Format_Library/{file_name}", "r") as f: role = f.read() print(role) messages = [{"role": "system", "content": role}, {"role": "user", "content": audio_transcript.text } ] response = client.chat.completions.create(model="gpt-3.5-turbo-1106", temperature=0, messages=messages) note_transcript = response.choices[0].message.content print(note_transcript) #######################################Create Text to Speech of Response speech_file_path = "Audio_Files/speech.mp3" response = client.audio.speech.create( model="tts-1", voice="fable", input=note_transcript ) response.stream_to_file(speech_file_path) return note_transcript, speech_file_path ########################## Define Gradio Interface Using Blocks with gr.Blocks() as demo: output_file = "Audio_Files/speech.mp3" language_type = gr.Radio(["French","Spanish"], show_label=False) txt = gr.Textbox(label="Question") btn = gr.Button(value="Generate Question") btn.click(generate_question, inputs=[language_type], outputs=[txt]) recorder = gr.Audio(sources=["microphone"],type="filepath") txt_in = gr.Textbox(value="", label ="Voice Transcript") btn2 = gr.Button(value="Generate Feedback") output = gr.Audio(value=output_file) txt_2 = gr.Textbox(value="", label="Suggestions") btn2.click(generate_feedback, inputs=[recorder, language_type], outputs=[txt_2,output]) if __name__ == "__main__": demo.launch(share=False, debug=True)