-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
255 lines (215 loc) · 9.69 KB
/
main.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import json
import time
import keyboard
import threading
import tkinter as tk
from vosk import Model, KaldiRecognizer
import pyaudio
import openai
import PyPDF2
import requests
# Set your API Keys for the respective models
openai.api_key = "your_openai_api_key_here"
anthropic_api_key = "your_anthropic_api_key_here"
gemini_api_key = "your_gemini_api_key_here"
llama_endpoint = "http://your_llama_api_endpoint_here" # Replace with your LLaMA API endpoint
ollama_endpoint = "http://localhost:11434/v1/chat/completions" # Default Ollama endpoint
# Vosk model setup (update the path to your downloaded model)
vosk_model_path = "path_to_vosk_model_directory"
# Initialize Vosk Speech Recognition model
if not os.path.exists(vosk_model_path):
print(f"Please download the model from https://alphacephei.com/vosk/models and unpack as '{vosk_model_path}'")
exit(1)
model = Model(vosk_model_path)
recognizer = KaldiRecognizer(model, 16000)
pause_event = threading.Event()
TOGGLE_KEY = 'space' #AI bot will repond only to this key
# Initialize PyAudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
stream.start_stream()
# Function to stop and clear the recording
# def stop_and_clear_recording():
# # global stream
# if stream.is_active():
# stream.stop_stream()
# stream.close()
# Teleprompter: Floating window
class Telepromter:
def __init__(self):
self.root = tk.Tk()
self.root.title("AI Nexus: Your Advanced AI-Powered Interview Assistant")
self.root.geometry("1100x600")
self.root.attributes("-topmost", True)
self.root.attributes("-alpha", 0.6) # Slight transparency
self.root.resizable(True, True)
# Dropdown menu for selecting the model
self.model_var = tk.StringVar(self.root)
self.model_var.set("OpenAI GPT-4") # Default model
self.models = ["OpenAI GPT-4", "Google Gemini", "Meta LLaMA", "Anthropic Claude", "Ollama"]
self.model_menu = tk.OptionMenu(self.root, self.model_var, *self.models)
self.model_menu.pack(pady=10)
# Text widget for displaying responses
self.text_widget = tk.Text(self.root, wrap='word', font=("Arial", 20), bg="black", fg="white", padx=10, pady=10)
self.text_widget.configure(state="disabled")
self.text_widget.pack(expand=True, fill='both')
self.root.update()
def update_text(self, response_text):
self.text_widget.configure(state="normal")
self.text_widget.delete(1.0, tk.END) # Clear previous text
self.text_widget.insert(tk.END, response_text) # Insert new text
self.text_widget.configure(state="disabled")
self.root.update()
def get_selected_model(self):
return self.model_var.get()
# Resume Parsing
def extract_resume_data(pdf_path):
with open(pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
resume_text = ""
for page in reader.pages:
resume_text += page.extract_text()
return resume_text
# Function to get response from different AI models
def get_response(prompt, resume_context, selected_model):
if selected_model == "OpenAI GPT-4":
return get_response_from_openai(prompt, resume_context)
elif selected_model == "Google Gemini":
return get_response_from_google(prompt, resume_context)
elif selected_model == "Meta LLaMA":
return get_response_from_meta(prompt, resume_context)
elif selected_model == "Anthropic Claude":
return get_response_from_anthropic(prompt, resume_context)
elif selected_model == "Ollama":
return get_response_from_ollama(prompt, resume_context)
else:
return "Selected model is not available."
# API call functions for each model
def get_response_from_openai(prompt, resume_context):
full_prompt = f"Respond as if you were me. The question is :\n{prompt}\n\n My Resume information: {resume_context}"
try:
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": full_prompt}],
max_tokens=70,
n=1,
stop=None,
temperature=0.7
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"Error with OpenAI gpt-4o-mini: {e}"
def get_response_from_google(prompt, resume_context):
full_prompt = f"Respond as if you were me. The question is :\n{prompt}\n\n My Resume information: {resume_context}"
try:
headers = {"Authorization": f"Bearer {gemini_api_key}"}
data = {"prompt": full_prompt}
response = requests.post("https://gemini-api.google.com/v1/generate", headers=headers, json=data)
response.raise_for_status()
return response.json().get("text", "Sorry, I couldn't process that.")
except Exception as e:
print(f"Gemini API Error: {e}")
return "Sorry, I couldn't process that."
def get_response_from_meta(prompt, resume_context):
full_prompt = f"Respond as if you were me. The question is :\n{prompt}\n\n My Resume information: {resume_context}"
try:
headers = {"Content-Type": "application/json"}
data = {"prompt": full_prompt}
response = requests.post(llama_endpoint, headers=headers, json=data)
response.raise_for_status()
return response.json().get("generated_text", "Sorry, I couldn't process that.")
except Exception as e:
print(f"LLaMA API Error: {e}")
return "Sorry, I couldn't process that."
def get_response_from_anthropic(prompt, resume_context):
full_prompt = f"Respond as if you were me. The question is :\n{prompt}\n\n My Resume information: {resume_context}"
try:
headers = {"x-api-key": anthropic_api_key}
data = {"prompt": full_prompt, "max_tokens_to_sample": 150}
response = requests.post("https://api.anthropic.com/v1/complete", headers=headers, json=data)
response.raise_for_status()
return response.json().get("completion", "Sorry, I couldn't process that.")
except Exception as e:
print(f"Anthropic Claude API Error: {e}")
return "Sorry, I couldn't process that."
def get_response_from_ollama(prompt, resume_context):
full_prompt = f"Respond as if you were me. The question is :\n{prompt}\n\n My Resume information: {resume_context}"
try:
headers = {"Content-Type": "application/json"}
data = {
"model": "llama-7b", # Specify the model name as configured in Ollama
"prompt": full_prompt,
"max_tokens": 70,
"temperature": 0.7
}
response = requests.post(ollama_endpoint, headers=headers, json=data)
response.raise_for_status()
return response.json().get("choices", [{}])[0].get("text", "Sorry, I couldn't process that.")
except Exception as e:
print(f"Ollama API Error: {e}")
return "Sorry, I couldn't process that."
# Speech Recognition with Vosk
def transcribe_audio(stream):
print("\nListening for questions...")
while True:
if pause_event.is_set():
break
data = stream.read(4000, exception_on_overflow=False)
if recognizer.AcceptWaveform(data):
result = recognizer.Result()
text = json.loads(result).get("text", "")
if text:
print(f"Transcribed text: {text}")
return text
# Pause and resume mechanism
def toggle_pause_resume(event):
if event.name != TOGGLE_KEY:
print(f'Please use toggle key = {TOGGLE_KEY} to pause/resume')
else:
if pause_event.is_set():
print("Resuming the bot...")
pause_event.clear()
else:
print("Pausing the bot...")
pause_event.set()
def listen_for_keyboard():
keyboard.on_press(toggle_pause_resume)
keyboard.wait() # Keep the listener active
# Main interview process
def start_interview():
resume_path = "path_to_resume.pdf" # Update with your resume path
resume_data = extract_resume_data(resume_path)
teleprompter = Telepromter()
while True:
if pause_event.is_set():
print(f'Press spacebar to activate AI assistant..{int(time.time())}')
time.sleep(1)
continue
try:
# Initialize PyAudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
stream.start_stream()
except Exception as e:
continue
# Step 1: Transcribe audio (question)
question = transcribe_audio(stream)
if question:
# Step 2: Get response from the selected model
selected_model = teleprompter.get_selected_model()
response = get_response(question, resume_data, selected_model)
print(f"Generated response: {response}")
# Step 3: Display response in the teleprompter
teleprompter.update_text(response)
time.sleep(1)
if __name__ == "__main__":
# Start the keyboard listener in a separate thread
keyboard_thread = threading.Thread(target=listen_for_keyboard)
keyboard_thread.daemon = True # Run as a background thread
keyboard_thread.start()
# start WITH DISABLED audio stream
pause_event.set()
# Start the interview process
start_interview()