gigswar commited on
Commit
3466d16
·
verified ·
1 Parent(s): 6e10908

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -4
app.py CHANGED
@@ -1,9 +1,38 @@
1
  import os
2
  import openai
3
  import gradio as gr
 
 
 
4
 
5
  openai.api_key = os.getenv("OPENAI_API_KEY")
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def chat_with_gpt4o(message):
8
  if not message:
9
  return "Please say something."
@@ -15,14 +44,27 @@ def chat_with_gpt4o(message):
15
  {"role": "user", "content": message}
16
  ]
17
  )
18
- return response.choices[0].message["content"]
 
 
 
 
 
 
 
 
 
 
 
19
 
 
20
  iface = gr.Interface(
21
- fn=chat_with_gpt4o,
22
  inputs=gr.Textbox(placeholder="Say something..."),
23
- outputs=gr.Textbox(label="AI Interviewer Response"),
24
  title="🎤 AI Interview Bot",
25
- description="Talk to a GPT-4o powered AI Interviewer. One-on-one style."
 
26
  )
27
 
28
  if __name__ == "__main__":
 
1
  import os
2
  import openai
3
  import gradio as gr
4
+ import pyttsx3
5
+ import speech_recognition as sr
6
+ from tempfile import NamedTemporaryFile
7
 
8
  openai.api_key = os.getenv("OPENAI_API_KEY")
9
 
10
+ # Initialize the speech recognition and TTS engines
11
+ recognizer = sr.Recognizer()
12
+ tts_engine = pyttsx3.init()
13
+
14
+ # Set up for speech-to-text (STT)
15
+ def recognize_speech_from_microphone():
16
+ with sr.Microphone() as source:
17
+ print("Listening...")
18
+ audio = recognizer.listen(source)
19
+ try:
20
+ print("Recognizing...")
21
+ return recognizer.recognize_google(audio)
22
+ except sr.UnknownValueError:
23
+ return "Sorry, I could not understand what you said."
24
+ except sr.RequestError:
25
+ return "Sorry, the service is down."
26
+
27
+ # Text to Speech (TTS) function
28
+ def speak_text(text):
29
+ with NamedTemporaryFile(delete=False, suffix='.mp3') as temp_file:
30
+ temp_file.close()
31
+ tts_engine.save_to_file(text, temp_file.name)
32
+ tts_engine.runAndWait()
33
+ return temp_file.name
34
+
35
+ # GPT-4o Q&A function
36
  def chat_with_gpt4o(message):
37
  if not message:
38
  return "Please say something."
 
44
  {"role": "user", "content": message}
45
  ]
46
  )
47
+ reply = response.choices[0].message["content"]
48
+ return reply
49
+
50
+ # Main interface function for Gradio
51
+ def chat_and_speak(message):
52
+ # Get GPT response
53
+ reply = chat_with_gpt4o(message)
54
+
55
+ # Convert text to speech
56
+ audio_path = speak_text(reply)
57
+
58
+ return reply, audio_path
59
 
60
+ # Create the Gradio Interface
61
  iface = gr.Interface(
62
+ fn=chat_and_speak,
63
  inputs=gr.Textbox(placeholder="Say something..."),
64
+ outputs=[gr.Textbox(label="AI Interviewer Response"), gr.Audio(label="Bot's Response")],
65
  title="🎤 AI Interview Bot",
66
+ description="Talk to a GPT-4o powered AI Interviewer. One-on-one style.",
67
+ live=True
68
  )
69
 
70
  if __name__ == "__main__":