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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PrepGenie/app.py
|
| 2 |
+
"""Main Gradio application file."""
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
import datetime
|
| 10 |
+
|
| 11 |
+
# --- Import Logic Modules ---
|
| 12 |
+
import interview_logic
|
| 13 |
+
import interview_history
|
| 14 |
+
# import auth_logic # If you move auth logic
|
| 15 |
+
# import chat_logic # If chat logic is moved
|
| 16 |
+
|
| 17 |
+
# --- Environment and Configuration ---
|
| 18 |
+
load_dotenv()
|
| 19 |
+
|
| 20 |
+
# --- Firebase Admin SDK Setup (in app.py or auth_logic) ---
|
| 21 |
+
import firebase_admin
|
| 22 |
+
from firebase_admin import credentials, auth, firestore # Added firestore
|
| 23 |
+
|
| 24 |
+
def initialize_firebase():
|
| 25 |
+
"""Attempts to initialize Firebase Admin SDK."""
|
| 26 |
+
if firebase_admin._apps:
|
| 27 |
+
print("Firebase app already initialized in app.py.")
|
| 28 |
+
return firebase_admin.get_app()
|
| 29 |
+
cred = None
|
| 30 |
+
try:
|
| 31 |
+
firebase_credentials_path = os.getenv("FIREBASE_CREDENTIALS_PATH", "prepgenie-64134-firebase-adminsdk-fbsvc-3370ac4ab9.json")
|
| 32 |
+
if firebase_credentials_path and os.path.exists(firebase_credentials_path):
|
| 33 |
+
print(f"Initializing Firebase with credentials file in app.py: {firebase_credentials_path}")
|
| 34 |
+
cred = credentials.Certificate(firebase_credentials_path)
|
| 35 |
+
firebase_app = firebase_admin.initialize_app(cred)
|
| 36 |
+
print("Firebase Admin initialized successfully using credentials file in app.py.")
|
| 37 |
+
return firebase_app
|
| 38 |
+
elif not firebase_credentials_path:
|
| 39 |
+
print("FIREBASE_CREDENTIALS_PATH is not set or is None in app.py.")
|
| 40 |
+
else:
|
| 41 |
+
print(f"Firebase credentials file not found at {firebase_credentials_path} in app.py.")
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Failed to initialize Firebase using credentials file in app.py: {e}")
|
| 44 |
+
try:
|
| 45 |
+
firebase_credentials_json = os.getenv("FIREBASE_CREDENTIALS_JSON")
|
| 46 |
+
if firebase_credentials_json:
|
| 47 |
+
print("Initializing Firebase with credentials from FIREBASE_CREDENTIALS_JSON environment variable in app.py.")
|
| 48 |
+
cred_dict = json.loads(firebase_credentials_json)
|
| 49 |
+
cred = credentials.Certificate(cred_dict)
|
| 50 |
+
firebase_app = firebase_admin.initialize_app(cred)
|
| 51 |
+
print("Firebase Admin initialized successfully using FIREBASE_CREDENTIALS_JSON in app.py.")
|
| 52 |
+
return firebase_app
|
| 53 |
+
else:
|
| 54 |
+
print("FIREBASE_CREDENTIALS_JSON environment variable not set in app.py.")
|
| 55 |
+
except (json.JSONDecodeError, ValueError) as e:
|
| 56 |
+
print(f"Error parsing FIREBASE_CREDENTIALS_JSON in app.py: {e}")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Failed to initialize Firebase using FIREBASE_CREDENTIALS_JSON in app.py: {e}")
|
| 59 |
+
print("Warning: Firebase Admin SDK could not be initialized in app.py. Authentication features will not work.")
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
FIREBASE_APP = initialize_firebase()
|
| 63 |
+
FIREBASE_AVAILABLE = FIREBASE_APP is not None
|
| 64 |
+
|
| 65 |
+
# --- Generative AI Setup ---
|
| 66 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY") or "YOUR_DEFAULT_API_KEY_HERE")
|
| 67 |
+
TEXT_MODEL = genai.GenerativeModel("gemini-1.5-flash") # Global model instance
|
| 68 |
+
print("Using Generative AI model: gemini-1.5-flash in app.py")
|
| 69 |
+
|
| 70 |
+
# --- Import Chat Module Functions ---
|
| 71 |
+
try:
|
| 72 |
+
from login_module import chat as chat_module # Assuming this is external
|
| 73 |
+
CHAT_MODULE_AVAILABLE = True
|
| 74 |
+
print("Chat module imported successfully in app.py.")
|
| 75 |
+
except ImportError as e:
|
| 76 |
+
print(f"Warning: Could not import chat module in app.py: {e}")
|
| 77 |
+
CHAT_MODULE_AVAILABLE = False
|
| 78 |
+
chat_module = None
|
| 79 |
+
|
| 80 |
+
# --- Helper Functions for UI Updates ---
|
| 81 |
+
def apply_ui_updates(updates_dict):
|
| 82 |
+
"""Converts logic function UI update instructions to Gradio updates."""
|
| 83 |
+
gr_updates = {}
|
| 84 |
+
for component_name, instruction in updates_dict.items():
|
| 85 |
+
if instruction == "gr_hide":
|
| 86 |
+
gr_updates[component_name] = gr.update(visible=False)
|
| 87 |
+
elif instruction == "gr_show":
|
| 88 |
+
gr_updates[component_name] = gr.update(visible=True)
|
| 89 |
+
elif instruction == "gr_show_and_update":
|
| 90 |
+
# This needs specific handling in the calling function
|
| 91 |
+
# Placeholder, logic should set value in calling function
|
| 92 |
+
gr_updates[component_name] = gr.update(visible=True)
|
| 93 |
+
elif instruction == "gr_show_and_update_error":
|
| 94 |
+
gr_updates[component_name] = gr.update(visible=True)
|
| 95 |
+
elif instruction == "gr_clear":
|
| 96 |
+
gr_updates[component_name] = "" # For textboxes
|
| 97 |
+
elif instruction == "gr_clear_dict":
|
| 98 |
+
gr_updates[component_name] = {} # For JSON
|
| 99 |
+
# Add more instructions as needed
|
| 100 |
+
else:
|
| 101 |
+
# Default or pass through
|
| 102 |
+
gr_updates[component_name] = gr.update()
|
| 103 |
+
return gr_updates
|
| 104 |
+
|
| 105 |
+
# --- Navigation Functions ---
|
| 106 |
+
def navigate_to_interview():
|
| 107 |
+
return (gr.update(visible=True), gr.update(visible=False), gr.update(visible=False))
|
| 108 |
+
|
| 109 |
+
def navigate_to_chat():
|
| 110 |
+
return (gr.update(visible=False), gr.update(visible=True), gr.update(visible=False))
|
| 111 |
+
|
| 112 |
+
def navigate_to_history():
|
| 113 |
+
return (gr.update(visible=False), gr.update(visible=False), gr.update(visible=True))
|
| 114 |
+
|
| 115 |
+
# --- Event Handler Functions (Orchestrators) ---
|
| 116 |
+
def process_resume_handler(file_obj):
|
| 117 |
+
"""Handles resume processing event."""
|
| 118 |
+
result = interview_logic.process_resume_logic(file_obj)
|
| 119 |
+
ui_updates = apply_ui_updates(result["ui_updates"])
|
| 120 |
+
# Unpack ui_updates in the correct order for outputs
|
| 121 |
+
return (
|
| 122 |
+
result["status"],
|
| 123 |
+
ui_updates.get("role_selection", gr.update()),
|
| 124 |
+
ui_updates.get("start_interview_btn", gr.update()),
|
| 125 |
+
ui_updates.get("question_display", gr.update()),
|
| 126 |
+
ui_updates.get("answer_instructions", gr.update()),
|
| 127 |
+
ui_updates.get("audio_input", gr.update()),
|
| 128 |
+
ui_updates.get("submit_answer_btn", gr.update()),
|
| 129 |
+
ui_updates.get("next_question_btn", gr.update()),
|
| 130 |
+
ui_updates.get("submit_interview_btn", gr.update()),
|
| 131 |
+
ui_updates.get("answer_display", gr.update()),
|
| 132 |
+
ui_updates.get("feedback_display", gr.update()),
|
| 133 |
+
ui_updates.get("metrics_display", gr.update()),
|
| 134 |
+
result["processed_data"] # Pass processed data
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
def start_interview_handler(roles, processed_resume_data):
|
| 138 |
+
"""Handles interview start event."""
|
| 139 |
+
# First, format the resume data using the AI model
|
| 140 |
+
formatted_resume_data = interview_logic.getallinfo(processed_resume_data, TEXT_MODEL)
|
| 141 |
+
result = interview_logic.start_interview_logic(roles, formatted_resume_data, TEXT_MODEL)
|
| 142 |
+
ui_updates = apply_ui_updates(result["ui_updates"])
|
| 143 |
+
return (
|
| 144 |
+
result["status"],
|
| 145 |
+
result["initial_question"],
|
| 146 |
+
ui_updates.get("audio_input", gr.update()),
|
| 147 |
+
ui_updates.get("submit_answer_btn", gr.update()),
|
| 148 |
+
ui_updates.get("next_question_btn", gr.update()),
|
| 149 |
+
ui_updates.get("submit_interview_btn", gr.update()),
|
| 150 |
+
ui_updates.get("feedback_display", gr.update()),
|
| 151 |
+
ui_updates.get("metrics_display", gr.update()),
|
| 152 |
+
ui_updates.get("question_display", gr.update()),
|
| 153 |
+
ui_updates.get("answer_instructions", gr.update()),
|
| 154 |
+
result["interview_state"]
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
def submit_answer_handler(audio, interview_state):
|
| 158 |
+
"""Handles answer submission event."""
|
| 159 |
+
result = interview_logic.submit_answer_logic(audio, interview_state, TEXT_MODEL)
|
| 160 |
+
ui_updates = apply_ui_updates(result["ui_updates"])
|
| 161 |
+
# Handle special updates for feedback and metrics that need value setting
|
| 162 |
+
feedback_update = ui_updates.get("feedback_display", gr.update())
|
| 163 |
+
if "gr_show_and_update" in result["ui_updates"].values():
|
| 164 |
+
feedback_update = gr.update(visible=True, value=result["feedback_text"])
|
| 165 |
+
metrics_update = ui_updates.get("metrics_display", gr.update())
|
| 166 |
+
if "gr_show_and_update" in result["ui_updates"].values():
|
| 167 |
+
metrics_update = gr.update(visible=True, value=result["metrics"])
|
| 168 |
+
|
| 169 |
+
return (
|
| 170 |
+
result["status"],
|
| 171 |
+
result["answer_text"],
|
| 172 |
+
result["interview_state"],
|
| 173 |
+
feedback_update,
|
| 174 |
+
metrics_update,
|
| 175 |
+
ui_updates.get("audio_input", gr.update()),
|
| 176 |
+
ui_updates.get("submit_answer_btn", gr.update()),
|
| 177 |
+
ui_updates.get("next_question_btn", gr.update()),
|
| 178 |
+
ui_updates.get("submit_interview_btn", gr.update()),
|
| 179 |
+
ui_updates.get("question_display", gr.update()),
|
| 180 |
+
ui_updates.get("answer_instructions", gr.update())
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
def next_question_handler(interview_state):
|
| 184 |
+
"""Handles next question event."""
|
| 185 |
+
result = interview_logic.next_question_logic(interview_state)
|
| 186 |
+
ui_updates = apply_ui_updates(result["ui_updates"])
|
| 187 |
+
return (
|
| 188 |
+
result["status"],
|
| 189 |
+
result["next_q"],
|
| 190 |
+
result["interview_state"],
|
| 191 |
+
ui_updates.get("audio_input", gr.update()),
|
| 192 |
+
ui_updates.get("submit_answer_btn", gr.update()),
|
| 193 |
+
ui_updates.get("next_question_btn", gr.update()),
|
| 194 |
+
ui_updates.get("feedback_display", gr.update()),
|
| 195 |
+
ui_updates.get("metrics_display", gr.update()),
|
| 196 |
+
ui_updates.get("submit_interview_btn", gr.update()),
|
| 197 |
+
ui_updates.get("question_display", gr.update()),
|
| 198 |
+
ui_updates.get("answer_instructions", gr.update()),
|
| 199 |
+
ui_updates.get("answer_display", ""), # Clear
|
| 200 |
+
ui_updates.get("metrics_display_clear", {}) # Clear
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
def submit_interview_handler(interview_state, user_id_state=""):
|
| 204 |
+
"""Handles interview submission event."""
|
| 205 |
+
result = interview_logic.submit_interview_logic(interview_state, TEXT_MODEL)
|
| 206 |
+
ui_updates = apply_ui_updates(result["ui_updates"])
|
| 207 |
+
|
| 208 |
+
# --- NEW: Save Interview History ---
|
| 209 |
+
if FIREBASE_AVAILABLE and user_id_state and "report_text" in result and "chart_buffer" in result:
|
| 210 |
+
print(f"Attempting to save history for user: {user_id_state} in submit_interview_handler")
|
| 211 |
+
# Package data for saving (using data from interview_state and result)
|
| 212 |
+
interview_summary_data = {
|
| 213 |
+
"timestamp": datetime.datetime.now().isoformat(),
|
| 214 |
+
"resume_overview": interview_state.get("resume_data", ""),
|
| 215 |
+
"selected_roles": interview_state.get("selected_roles", []),
|
| 216 |
+
"questions": list(interview_state.get("interactions", {}).keys()),
|
| 217 |
+
"answers": list(interview_state.get("interactions", {}).values()),
|
| 218 |
+
"feedback": interview_state.get("feedback", []),
|
| 219 |
+
"interactions": interview_state.get("interactions", {}),
|
| 220 |
+
"metrics_list": interview_state.get("metrics_list", []),
|
| 221 |
+
"final_metrics": interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL)), # Recalculate or pass from logic?
|
| 222 |
+
# Use average from report text parsing or recalculate if needed, or pass from logic if stored
|
| 223 |
+
"average_rating": sum(interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL)).values()) / len(interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL))) if interview_logic.parse_metrics(interview_logic.getmetrics(interview_state.get("interactions", {}), interview_state.get("resume_data", ""), TEXT_MODEL)) else 0.0,
|
| 224 |
+
"evaluation_report": result["report_text"]
|
| 225 |
+
}
|
| 226 |
+
save_success = interview_history.save_interview_history(user_id_state, interview_summary_data)
|
| 227 |
+
if save_success:
|
| 228 |
+
print("Interview history saved successfully in submit_interview_handler.")
|
| 229 |
+
# Optionally append to report text
|
| 230 |
+
# result["report_text"] += "\n\n---\n*Interview history saved successfully.*"
|
| 231 |
+
else:
|
| 232 |
+
print("Failed to save interview history in submit_interview_handler.")
|
| 233 |
+
# result["report_text"] += "\n\n---\n*Failed to save interview history.*"
|
| 234 |
+
else:
|
| 235 |
+
if not user_id_state:
|
| 236 |
+
print("User not logged in. Skipping history save in submit_interview_handler.")
|
| 237 |
+
else:
|
| 238 |
+
print("Firestore not available. Skipping history save in submit_interview_handler.")
|
| 239 |
+
|
| 240 |
+
# Handle special updates for report and chart that need value setting
|
| 241 |
+
report_update = ui_updates.get("evaluation_report_display", gr.update())
|
| 242 |
+
if "gr_show_and_update" in result["ui_updates"].values():
|
| 243 |
+
report_update = gr.update(visible=True, value=result["report_text"])
|
| 244 |
+
elif "gr_show_and_update_error" in result["ui_updates"].values():
|
| 245 |
+
report_update = gr.update(visible=True, value=result["report_text"]) # Show error
|
| 246 |
+
|
| 247 |
+
chart_update = ui_updates.get("evaluation_chart_display", gr.update())
|
| 248 |
+
if "gr_show_and_update" in result["ui_updates"].values():
|
| 249 |
+
chart_update = gr.update(visible=True, value=result["chart_buffer"])
|
| 250 |
+
elif "gr_show_and_update_error" in result["ui_updates"].values():
|
| 251 |
+
chart_update = gr.update(visible=False) # Hide chart on error
|
| 252 |
+
|
| 253 |
+
return (
|
| 254 |
+
result["status"],
|
| 255 |
+
result["interview_state"], # Pass through
|
| 256 |
+
report_update,
|
| 257 |
+
chart_update
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
# --- Login/Logout Logic (Simplified here, could be in auth_logic) ---
|
| 261 |
+
def login(email, password):
|
| 262 |
+
if not FIREBASE_AVAILABLE:
|
| 263 |
+
return (
|
| 264 |
+
"Firebase not initialized. Login unavailable.",
|
| 265 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
|
| 266 |
+
"", "", "", ""
|
| 267 |
+
)
|
| 268 |
+
if not email or not password:
|
| 269 |
+
return (
|
| 270 |
+
"Please enter email and password.",
|
| 271 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
|
| 272 |
+
email, password, "", ""
|
| 273 |
+
)
|
| 274 |
+
try:
|
| 275 |
+
user = auth.get_user_by_email(email)
|
| 276 |
+
welcome_msg = f"Welcome, {user.display_name or user.uid}!"
|
| 277 |
+
return (
|
| 278 |
+
welcome_msg,
|
| 279 |
+
gr.update(visible=False), gr.update(visible=False), gr.update(visible=True),
|
| 280 |
+
"", "", user.uid, user.email
|
| 281 |
+
)
|
| 282 |
+
except auth.UserNotFoundError:
|
| 283 |
+
return (
|
| 284 |
+
"User not found. Please check your email or sign up.",
|
| 285 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
|
| 286 |
+
email, password, "", ""
|
| 287 |
+
)
|
| 288 |
+
except Exception as e:
|
| 289 |
+
error_msg = f"Login failed: {str(e)}"
|
| 290 |
+
print(error_msg)
|
| 291 |
+
return (
|
| 292 |
+
error_msg,
|
| 293 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
|
| 294 |
+
email, password, "", ""
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
def signup(email, password, username):
|
| 298 |
+
if not FIREBASE_AVAILABLE:
|
| 299 |
+
return (
|
| 300 |
+
"Firebase not initialized. Signup unavailable.",
|
| 301 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
|
| 302 |
+
"", "", "", "", ""
|
| 303 |
+
)
|
| 304 |
+
if not email or not password or not username:
|
| 305 |
+
return (
|
| 306 |
+
"Please fill all fields.",
|
| 307 |
+
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
|
| 308 |
+
email, password, username, "", ""
|
| 309 |
+
)
|
| 310 |
+
try:
|
| 311 |
+
user = auth.create_user(email=email, password=password, uid=username, display_name=username)
|
| 312 |
+
success_msg = f"Account created successfully for {username}!"
|
| 313 |
+
return (
|
| 314 |
+
success_msg,
|
| 315 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
|
| 316 |
+
"", "", "", user.uid, user.email
|
| 317 |
+
)
|
| 318 |
+
except auth.UidAlreadyExistsError:
|
| 319 |
+
return (
|
| 320 |
+
"Username already exists. Please choose another.",
|
| 321 |
+
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
|
| 322 |
+
email, password, username, "", ""
|
| 323 |
+
)
|
| 324 |
+
except auth.EmailAlreadyExistsError:
|
| 325 |
+
return (
|
| 326 |
+
"Email already exists. Please use another email.",
|
| 327 |
+
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
|
| 328 |
+
email, password, username, "", ""
|
| 329 |
+
)
|
| 330 |
+
except Exception as e:
|
| 331 |
+
error_msg = f"Signup failed: {str(e)}"
|
| 332 |
+
print(error_msg)
|
| 333 |
+
return (
|
| 334 |
+
error_msg,
|
| 335 |
+
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
|
| 336 |
+
email, password, username, "", ""
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
def logout():
|
| 340 |
+
return (
|
| 341 |
+
"",
|
| 342 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
|
| 343 |
+
"", "", "", "", ""
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
# --- Gradio Interface ---
|
| 347 |
+
with gr.Blocks(title="PrepGenie - Mock Interviewer") as demo:
|
| 348 |
+
interview_state = gr.State({})
|
| 349 |
+
user_state = gr.State("") # Holds user.uid
|
| 350 |
+
user_email_state = gr.State("") # Holds user.email
|
| 351 |
+
processed_resume_data_state = gr.State("")
|
| 352 |
+
|
| 353 |
+
# --- Header Section ---
|
| 354 |
+
with gr.Row():
|
| 355 |
+
gr.Markdown(
|
| 356 |
+
"""
|
| 357 |
+
<h1 style="display: flex; justify-content: center; align-items: center;">
|
| 358 |
+
PrepGenie- Interview Preparation App
|
| 359 |
+
</h1>
|
| 360 |
+
""",
|
| 361 |
+
elem_id="title"
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
# --- Login Section ---
|
| 365 |
+
with gr.Column(visible=True) as login_section:
|
| 366 |
+
gr.Markdown("## Login")
|
| 367 |
+
login_email_input = gr.Textbox(label="Email Address")
|
| 368 |
+
login_password_input = gr.Textbox(label="Password", type="password")
|
| 369 |
+
login_btn = gr.Button("Login")
|
| 370 |
+
login_status = gr.Textbox(label="Login Status", interactive=False)
|
| 371 |
+
switch_to_signup_btn = gr.Button("Don't have an account? Sign Up")
|
| 372 |
+
|
| 373 |
+
# --- Signup Section ---
|
| 374 |
+
with gr.Column(visible=False) as signup_section:
|
| 375 |
+
gr.Markdown("## Sign Up")
|
| 376 |
+
signup_email_input = gr.Textbox(label="Email Address")
|
| 377 |
+
signup_password_input = gr.Textbox(label="Password", type="password")
|
| 378 |
+
signup_username_input = gr.Textbox(label="Unique Username")
|
| 379 |
+
signup_btn = gr.Button("Create my account")
|
| 380 |
+
signup_status = gr.Textbox(label="Signup Status", interactive=False)
|
| 381 |
+
switch_to_login_btn = gr.Button("Already have an account? Login")
|
| 382 |
+
|
| 383 |
+
# --- Main App Sections ---
|
| 384 |
+
with gr.Column(visible=False) as main_app:
|
| 385 |
+
with gr.Row():
|
| 386 |
+
with gr.Column(scale=1):
|
| 387 |
+
logout_btn = gr.Button("Logout")
|
| 388 |
+
with gr.Column(scale=4):
|
| 389 |
+
welcome_display = gr.Markdown("### Welcome, User!")
|
| 390 |
+
|
| 391 |
+
with gr.Row():
|
| 392 |
+
with gr.Column(scale=1):
|
| 393 |
+
interview_btn = gr.Button("Mock Interview")
|
| 394 |
+
if CHAT_MODULE_AVAILABLE:
|
| 395 |
+
chat_btn = gr.Button("Chat with Resume")
|
| 396 |
+
else:
|
| 397 |
+
chat_btn = gr.Button("Chat with Resume (Unavailable)", interactive=False)
|
| 398 |
+
history_btn = gr.Button("My Interview History")
|
| 399 |
+
|
| 400 |
+
with gr.Column(scale=4):
|
| 401 |
+
# --- Interview Section ---
|
| 402 |
+
with gr.Column(visible=False) as interview_selection:
|
| 403 |
+
gr.Markdown("## Mock Interview")
|
| 404 |
+
with gr.Row():
|
| 405 |
+
with gr.Column():
|
| 406 |
+
file_upload_interview = gr.File(label="Upload Resume (PDF)", file_types=[".pdf"])
|
| 407 |
+
process_btn_interview = gr.Button("Process Resume")
|
| 408 |
+
with gr.Column():
|
| 409 |
+
file_status_interview = gr.Textbox(label="Status", interactive=False)
|
| 410 |
+
role_selection = gr.Dropdown(
|
| 411 |
+
choices=["Data Scientist", "Software Engineer", "Product Manager", "Data Analyst", "Business Analyst"],
|
| 412 |
+
multiselect=True, label="Select Job Role(s)", visible=False
|
| 413 |
+
)
|
| 414 |
+
start_interview_btn = gr.Button("Start Interview", visible=False)
|
| 415 |
+
question_display = gr.Textbox(label="Question", interactive=False, visible=False)
|
| 416 |
+
answer_instructions = gr.Markdown("Click 'Record Answer' and speak your response.", visible=False)
|
| 417 |
+
audio_input = gr.Audio(label="Record Answer", type="numpy", visible=False)
|
| 418 |
+
submit_answer_btn = gr.Button("Submit Answer", visible=False)
|
| 419 |
+
next_question_btn = gr.Button("Next Question", visible=False)
|
| 420 |
+
submit_interview_btn = gr.Button("Submit Interview", visible=False, variant="primary")
|
| 421 |
+
answer_display = gr.Textbox(label="Your Answer", interactive=False, visible=False)
|
| 422 |
+
feedback_display = gr.Textbox(label="Feedback", interactive=False, visible=False)
|
| 423 |
+
metrics_display = gr.JSON(label="Metrics", visible=False)
|
| 424 |
+
processed_resume_data_hidden_interview = gr.Textbox(visible=False)
|
| 425 |
+
# --- Evaluation Results Section ---
|
| 426 |
+
with gr.Column(visible=False) as evaluation_selection:
|
| 427 |
+
gr.Markdown("## Interview Evaluation Results")
|
| 428 |
+
evaluation_report_display = gr.Markdown(label="Your Evaluation Report", visible=False)
|
| 429 |
+
evaluation_chart_display = gr.Image(label="Skills Breakdown", type="pil", visible=False)
|
| 430 |
+
|
| 431 |
+
# --- Chat Section ---
|
| 432 |
+
if CHAT_MODULE_AVAILABLE:
|
| 433 |
+
with gr.Column(visible=False) as chat_selection:
|
| 434 |
+
gr.Markdown("## Chat with Resume")
|
| 435 |
+
with gr.Row():
|
| 436 |
+
with gr.Column():
|
| 437 |
+
file_upload_chat = gr.File(label="Upload Resume (PDF)", file_types=[".pdf"])
|
| 438 |
+
process_chat_btn = gr.Button("Process Resume")
|
| 439 |
+
with gr.Column():
|
| 440 |
+
file_status_chat = gr.Textbox(label="Status", interactive=False)
|
| 441 |
+
chatbot = gr.Chatbot(label="Chat History", visible=False, type="messages")
|
| 442 |
+
query_input = gr.Textbox(label="Ask about your resume", placeholder="Type your question here...", visible=False)
|
| 443 |
+
send_btn = gr.Button("Send", visible=False)
|
| 444 |
+
else:
|
| 445 |
+
with gr.Column(visible=False) as chat_selection:
|
| 446 |
+
gr.Markdown("## Chat with Resume (Unavailable)")
|
| 447 |
+
gr.Textbox(value="Chat module is not available.", interactive=False)
|
| 448 |
+
|
| 449 |
+
# --- History Section ---
|
| 450 |
+
with gr.Column(visible=False) as history_selection:
|
| 451 |
+
gr.Markdown("## Your Interview History")
|
| 452 |
+
load_history_btn = gr.Button("Load My Past Interviews")
|
| 453 |
+
history_output = gr.Textbox(label="Past Interviews", max_lines=30, interactive=False, visible=True)
|
| 454 |
+
|
| 455 |
+
# Assign view variables for navigation
|
| 456 |
+
interview_view = interview_selection
|
| 457 |
+
chat_view = chat_selection
|
| 458 |
+
history_view = history_selection
|
| 459 |
+
|
| 460 |
+
# Navigation button listeners
|
| 461 |
+
interview_btn.click(fn=navigate_to_interview, inputs=None, outputs=[interview_view, chat_view, history_view])
|
| 462 |
+
if CHAT_MODULE_AVAILABLE:
|
| 463 |
+
chat_btn.click(fn=navigate_to_chat, inputs=None, outputs=[interview_view, chat_view, history_view])
|
| 464 |
+
history_btn.click(fn=navigate_to_history, inputs=None, outputs=[interview_view, chat_view, history_view])
|
| 465 |
+
|
| 466 |
+
# --- Event Listeners for Interview ---
|
| 467 |
+
process_btn_interview.click(
|
| 468 |
+
fn=process_resume_handler,
|
| 469 |
+
inputs=[file_upload_interview],
|
| 470 |
+
outputs=[
|
| 471 |
+
file_status_interview, role_selection, start_interview_btn,
|
| 472 |
+
question_display, answer_instructions, audio_input,
|
| 473 |
+
submit_answer_btn, next_question_btn, submit_interview_btn,
|
| 474 |
+
answer_display, feedback_display, metrics_display,
|
| 475 |
+
processed_resume_data_hidden_interview
|
| 476 |
+
]
|
| 477 |
+
)
|
| 478 |
+
start_interview_btn.click(
|
| 479 |
+
fn=start_interview_handler,
|
| 480 |
+
inputs=[role_selection, processed_resume_data_hidden_interview],
|
| 481 |
+
outputs=[
|
| 482 |
+
file_status_interview, question_display,
|
| 483 |
+
audio_input, submit_answer_btn, next_question_btn,
|
| 484 |
+
submit_interview_btn, feedback_display, metrics_display,
|
| 485 |
+
question_display, answer_instructions,
|
| 486 |
+
interview_state
|
| 487 |
+
]
|
| 488 |
+
)
|
| 489 |
+
submit_answer_btn.click(
|
| 490 |
+
fn=submit_answer_handler,
|
| 491 |
+
inputs=[audio_input, interview_state],
|
| 492 |
+
outputs=[
|
| 493 |
+
file_status_interview, answer_display, interview_state,
|
| 494 |
+
feedback_display, metrics_display,
|
| 495 |
+
audio_input, submit_answer_btn, next_question_btn,
|
| 496 |
+
submit_interview_btn, question_display, answer_instructions
|
| 497 |
+
]
|
| 498 |
+
)
|
| 499 |
+
next_question_btn.click(
|
| 500 |
+
fn=next_question_handler,
|
| 501 |
+
inputs=[interview_state],
|
| 502 |
+
outputs=[
|
| 503 |
+
file_status_interview, question_display, interview_state,
|
| 504 |
+
audio_input, submit_answer_btn, next_question_btn,
|
| 505 |
+
feedback_display, metrics_display, submit_interview_btn,
|
| 506 |
+
question_display, answer_instructions,
|
| 507 |
+
answer_display, metrics_display # Clear previous
|
| 508 |
+
]
|
| 509 |
+
)
|
| 510 |
+
submit_interview_btn.click(
|
| 511 |
+
fn=submit_interview_handler,
|
| 512 |
+
inputs=[interview_state, user_state], # Pass user_state for history saving
|
| 513 |
+
outputs=[
|
| 514 |
+
file_status_interview,
|
| 515 |
+
interview_state,
|
| 516 |
+
evaluation_report_display,
|
| 517 |
+
evaluation_chart_display
|
| 518 |
+
]
|
| 519 |
+
)
|
| 520 |
+
|
| 521 |
+
# --- Event Listeners for Chat ---
|
| 522 |
+
if CHAT_MODULE_AVAILABLE:
|
| 523 |
+
process_chat_btn.click(
|
| 524 |
+
fn=chat_module.process_resume_chat,
|
| 525 |
+
inputs=[file_upload_chat],
|
| 526 |
+
outputs=[file_status_chat, processed_resume_data_state, query_input, send_btn, chatbot]
|
| 527 |
+
)
|
| 528 |
+
send_btn.click(
|
| 529 |
+
fn=chat_module.chat_with_resume,
|
| 530 |
+
inputs=[query_input, processed_resume_data_state, chatbot],
|
| 531 |
+
outputs=[query_input, chatbot]
|
| 532 |
+
)
|
| 533 |
+
query_input.submit(
|
| 534 |
+
fn=chat_module.chat_with_resume,
|
| 535 |
+
inputs=[query_input, processed_resume_data_state, chatbot],
|
| 536 |
+
outputs=[query_input, chatbot]
|
| 537 |
+
)
|
| 538 |
+
|
| 539 |
+
# --- NEW: Event Listener for History ---
|
| 540 |
+
load_history_btn.click(fn=interview_history.load_user_history, inputs=[user_state], outputs=[history_output])
|
| 541 |
+
|
| 542 |
+
# --- Login/Logout Event Listeners ---
|
| 543 |
+
login_btn.click(
|
| 544 |
+
fn=login,
|
| 545 |
+
inputs=[login_email_input, login_password_input],
|
| 546 |
+
outputs=[login_status, login_section, signup_section, main_app,
|
| 547 |
+
login_email_input, login_password_input, user_state, user_email_state]
|
| 548 |
+
)
|
| 549 |
+
signup_btn.click(
|
| 550 |
+
fn=signup,
|
| 551 |
+
inputs=[signup_email_input, signup_password_input, signup_username_input],
|
| 552 |
+
outputs=[signup_status, login_section, signup_section, main_app,
|
| 553 |
+
signup_email_input, signup_password_input, signup_username_input,
|
| 554 |
+
user_state, user_email_state]
|
| 555 |
+
)
|
| 556 |
+
logout_btn.click(
|
| 557 |
+
fn=logout,
|
| 558 |
+
inputs=None,
|
| 559 |
+
outputs=[login_status, login_section, signup_section, main_app,
|
| 560 |
+
login_email_input, login_password_input, signup_username_input,
|
| 561 |
+
user_state, user_email_state]
|
| 562 |
+
)
|
| 563 |
+
switch_to_signup_btn.click(
|
| 564 |
+
fn=lambda: (gr.update(visible=False), gr.update(visible=True)),
|
| 565 |
+
inputs=None,
|
| 566 |
+
outputs=[login_section, signup_section]
|
| 567 |
+
)
|
| 568 |
+
switch_to_login_btn.click(
|
| 569 |
+
fn=lambda: (gr.update(visible=True), gr.update(visible=False)),
|
| 570 |
+
inputs=None,
|
| 571 |
+
outputs=[login_section, signup_section]
|
| 572 |
+
)
|
| 573 |
+
|
| 574 |
+
if __name__ == "__main__":
|
| 575 |
+
demo.launch(server_name="0.0.0.0")
|