File size: 2,078 Bytes
9dd66ed
 
 
3466d16
3585a12
3466d16
9dd66ed
ee12257
167dabb
3585a12
3466d16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3585a12
3466d16
 
 
3585a12
 
3466d16
 
 
a2f9224
 
 
 
 
 
 
 
 
 
 
3466d16
 
 
 
 
 
 
 
 
 
 
 
a2f9224
3466d16
a2f9224
3466d16
a2f9224
3466d16
a2f9224
3466d16
 
ee12257
9dd66ed
a2f9224
3585a12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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()