AI-Edify's picture
Update app.py
112a3e2 verified
raw
history blame
2.46 kB
import os
import gradio as gr
import openai
import speech_recognition as sr
import time
# Set OpenAI API key
openai.api_key = os.environ.get("OPENAI_API_KEY")
def generate_text():
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Generate a short paragraph (2-3 sentences) for an English learner to read aloud."},
{"role": "user", "content": "Create a practice text."}
]
)
return response.choices[0].message['content']
def get_pronunciation_feedback(original_text, transcription):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful pronunciation assistant. Compare the generated text with the user's transcription and provide feedback on how the user can improve their pronunciation. Single out specific words they pronounced incorrectly and give tips on how to improve, like for example 'schedule' can be pronounced as 'sked-jool'."},
{"role": "user", "content": f"Original text: '{original_text}'\nTranscription: '{transcription}'\nProvide pronunciation feedback."}
]
)
return response.choices[0].message['content']
def transcribe_audio_realtime(audio):
recognizer = sr.Recognizer()
with sr.AudioFile(audio) as source:
audio_data = recognizer.record(source)
try:
return recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError:
return "Could not request results from the speech recognition service"
def practice_pronunciation(audio):
original_text = generate_text()
transcription = transcribe_audio_realtime(audio)
feedback = get_pronunciation_feedback(original_text, transcription)
return original_text, transcription, feedback
# Gradio interface
demo = gr.Interface(
fn=practice_pronunciation,
inputs=[
gr.Audio(type="filepath")
],
outputs=[
gr.Textbox(label="Text to Read"),
gr.Textbox(label="Your Transcription"),
gr.Textbox(label="Pronunciation Feedback")
],
title="Pronunciation Practice Tool",
description="Read the generated text aloud. The system will transcribe your speech and provide pronunciation feedback.",
live=True
)
# Launch the app
if __name__ == "__main__":
demo.launch()