Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| import gradio as gr | |
| import speech_recognition as sr | |
| from gtts import gTTS | |
| from tempfile import NamedTemporaryFile | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| # Initialize the speech recognition engine | |
| recognizer = sr.Recognizer() | |
| # Set up for speech-to-text (STT) | |
| def recognize_speech_from_microphone(): | |
| with sr.Microphone() as source: | |
| print("Listening...") | |
| audio = recognizer.listen(source) | |
| try: | |
| print("Recognizing...") | |
| return recognizer.recognize_google(audio) | |
| except sr.UnknownValueError: | |
| return "Sorry, I could not understand what you said." | |
| except sr.RequestError: | |
| return "Sorry, the service is down." | |
| # Text to Speech (TTS) function using gTTS | |
| def speak_text(text): | |
| with NamedTemporaryFile(delete=False, suffix='.mp3') as temp_file: | |
| temp_file.close() | |
| tts = gTTS(text=text, lang='en') | |
| tts.save(temp_file.name) | |
| return temp_file.name | |
| # GPT-4o Q&A function | |
| def chat_with_gpt4o(message): | |
| if not message: | |
| return "Please say something." | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o", | |
| messages=[ | |
| {"role": "system", "content": "You are a smart, friendly AI interviewer. Ask one question at a time."}, | |
| {"role": "user", "content": message} | |
| ] | |
| ) | |
| reply = response.choices[0].message["content"] | |
| return reply | |
| # Main interface function for Gradio | |
| def chat_and_speak(message): | |
| # Get GPT response | |
| reply = chat_with_gpt4o(message) | |
| # Convert text to speech | |
| audio_path = speak_text(reply) | |
| return reply, audio_path | |
| # Create the Gradio Interface | |
| iface = gr.Interface( | |
| fn=chat_and_speak, | |
| inputs=gr.Textbox(placeholder="Say something..."), | |
| outputs=[gr.Textbox(label="AI Interviewer Response"), gr.Audio(label="Bot's Response")], | |
| title="🎤 AI Interview Bot", | |
| description="Talk to a GPT-4o powered AI Interviewer. One-on-one style.", | |
| live=True | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |