Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -350,10 +350,38 @@ def main():
|
|
| 350 |
# Build Gradio interface
|
| 351 |
with gr.Blocks(css=css) as demo:
|
| 352 |
gr.Markdown(
|
| 353 |
-
"<h1 style='text-align:center;'>π AI HR Interview Assistant
|
| 354 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
# Admin Panel Tab
|
| 358 |
with gr.Tab("Admin Panel", id="admin_tab"):
|
| 359 |
with gr.Tab("API Key Settings"):
|
|
@@ -365,15 +393,8 @@ def main():
|
|
| 365 |
|
| 366 |
def update_api_key(api_key):
|
| 367 |
os.environ["OPENAI_API_KEY"] = api_key # Caution: Modifying os.environ is session-based
|
| 368 |
-
global interview_func, initial_message, final_message, initial_api_key_status_message # Declare globals to update them
|
| 369 |
initial_api_key_status_message = check_api_key() # Update status immediately after key is entered
|
| 370 |
-
QUESTIONS_FILE_PATH = "questions.json" # Define QUESTIONS_FILE_PATH here
|
| 371 |
-
try:
|
| 372 |
-
questions = read_questions_from_json(QUESTIONS_FILE_PATH) # reload questions here to fix NameError: name 'questions' is not defined
|
| 373 |
-
except Exception as e:
|
| 374 |
-
print(f"Error reloading questions in update_api_key: {e}")
|
| 375 |
-
return f"β Error reloading questions: {e}"
|
| 376 |
-
|
| 377 |
interview_func, initial_message, final_message = conduct_interview(questions) # Re-init interview function
|
| 378 |
return initial_api_key_status_message # Return status message
|
| 379 |
|
|
@@ -387,8 +408,8 @@ def main():
|
|
| 387 |
# with gr.Tab("Generate Questions"):
|
| 388 |
with gr.Tab("Generate Questions"):
|
| 389 |
try:
|
| 390 |
-
# Assuming these are defined in
|
| 391 |
-
from
|
| 392 |
load_json_data,
|
| 393 |
PROFESSIONS_FILE,
|
| 394 |
TYPES_FILE,
|
|
@@ -530,28 +551,6 @@ def main():
|
|
| 530 |
)
|
| 531 |
|
| 532 |
|
| 533 |
-
# --- Chatbot, User Input, Audio Output definitions (moved to be before button event handlers) ---
|
| 534 |
-
chatbot = gr.Chatbot(
|
| 535 |
-
[],
|
| 536 |
-
elem_id="chatbot",
|
| 537 |
-
bubble_full_width=False,
|
| 538 |
-
height=650
|
| 539 |
-
)
|
| 540 |
-
|
| 541 |
-
user_input = gr.Textbox(
|
| 542 |
-
"",
|
| 543 |
-
placeholder="Type response here... or record audio",
|
| 544 |
-
elem_id="user-input",
|
| 545 |
-
show_label=False,
|
| 546 |
-
)
|
| 547 |
-
|
| 548 |
-
audio_output = gr.Audio(
|
| 549 |
-
autoplay=True,
|
| 550 |
-
streaming=False
|
| 551 |
-
)
|
| 552 |
-
# --- End of Chatbot, User Input, Audio Output definitions ---
|
| 553 |
-
|
| 554 |
-
|
| 555 |
# --- Gradio callback functions ---
|
| 556 |
|
| 557 |
def start_interview():
|
|
@@ -622,44 +621,40 @@ def main():
|
|
| 622 |
# --- Wire up the event handlers ---
|
| 623 |
|
| 624 |
# 1) Start button
|
| 625 |
-
start_btn = gr.Button("Start Interview", variant="primary") # Define start_btn here
|
| 626 |
start_btn.click(
|
| 627 |
start_interview,
|
| 628 |
inputs=[],
|
| 629 |
-
outputs=[chatbot, user_input, audio_output]
|
| 630 |
)
|
| 631 |
|
| 632 |
# 2) Audio: when recording stops
|
| 633 |
-
audio_input = gr.Audio(sources=["microphone"], streaming=False, type="filepath", label="Record Audio") # Define audio_input here, before use
|
| 634 |
audio_input.stop_recording(
|
| 635 |
interview_step_wrapper,
|
| 636 |
inputs=[user_input, audio_input, chatbot],
|
| 637 |
-
outputs=[chatbot, user_input, audio_output]
|
| 638 |
)
|
| 639 |
|
| 640 |
# 3) Submit button
|
| 641 |
-
submit_btn = gr.Button("Submit", variant="primary") # Define submit_btn here
|
| 642 |
submit_btn.click(
|
| 643 |
interview_step_wrapper,
|
| 644 |
inputs=[user_input, audio_input, chatbot],
|
| 645 |
-
outputs=[chatbot, user_input, audio_output]
|
| 646 |
)
|
| 647 |
-
|
| 648 |
-
|
|
|
|
| 649 |
on_enter_submit,
|
| 650 |
-
inputs=[chatbot, user_input],
|
| 651 |
-
outputs=[chatbot, user_input, audio_output]
|
| 652 |
)
|
| 653 |
|
| 654 |
# 5) Clear button
|
| 655 |
-
clear_btn = gr.Button("Clear Chat") # Define clear_btn here
|
| 656 |
clear_btn.click(
|
| 657 |
clear_chat,
|
| 658 |
inputs=[],
|
| 659 |
-
outputs=[chatbot, user_input, audio_output]
|
| 660 |
)
|
| 661 |
|
| 662 |
-
|
| 663 |
# Launch Gradio (remove `share=True` if it keeps failing)
|
| 664 |
demo.launch(
|
| 665 |
server_name="0.0.0.0",
|
|
|
|
| 350 |
# Build Gradio interface
|
| 351 |
with gr.Blocks(css=css) as demo:
|
| 352 |
gr.Markdown(
|
| 353 |
+
"<h1 style='text-align:center;'>π AI HR Interview Assistant</h1>"
|
| 354 |
)
|
| 355 |
+
gr.Markdown(
|
| 356 |
+
"I will ask you a series of questions. Please answer honestly and thoughtfully. "
|
| 357 |
+
"When you are ready, click **Start Interview** to begin."
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
chatbot = gr.Chatbot( # Moved up here
|
| 361 |
+
label="Interview Chat",
|
| 362 |
+
height=650,
|
| 363 |
+
type='messages' # must return a list of dicts: {"role":..., "content":...}
|
| 364 |
+
)
|
| 365 |
+
audio_input = gr.Audio( # Moved up here
|
| 366 |
+
sources=["microphone"],
|
| 367 |
+
type="filepath",
|
| 368 |
+
label="Record Your Answer"
|
| 369 |
+
)
|
| 370 |
+
user_input = gr.Textbox( # Moved up here
|
| 371 |
+
label="Your Response",
|
| 372 |
+
placeholder="Type your answer here or use the microphone...",
|
| 373 |
+
lines=1,
|
| 374 |
+
)
|
| 375 |
+
audio_output = gr.Audio(label="Response Audio", autoplay=True) # Moved up here
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
start_btn = gr.Button("Start Interview", variant="primary")
|
| 379 |
|
| 380 |
|
| 381 |
+
with gr.Row():
|
| 382 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 383 |
+
clear_btn = gr.Button("Clear Chat")
|
| 384 |
+
|
| 385 |
# Admin Panel Tab
|
| 386 |
with gr.Tab("Admin Panel", id="admin_tab"):
|
| 387 |
with gr.Tab("API Key Settings"):
|
|
|
|
| 393 |
|
| 394 |
def update_api_key(api_key):
|
| 395 |
os.environ["OPENAI_API_KEY"] = api_key # Caution: Modifying os.environ is session-based
|
| 396 |
+
global interview_func, initial_message, final_message, questions, initial_api_key_status_message # Declare globals to update them and questions
|
| 397 |
initial_api_key_status_message = check_api_key() # Update status immediately after key is entered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
interview_func, initial_message, final_message = conduct_interview(questions) # Re-init interview function
|
| 399 |
return initial_api_key_status_message # Return status message
|
| 400 |
|
|
|
|
| 408 |
# with gr.Tab("Generate Questions"):
|
| 409 |
with gr.Tab("Generate Questions"):
|
| 410 |
try:
|
| 411 |
+
# Assuming these are defined in backend3.py
|
| 412 |
+
from backend3 import (
|
| 413 |
load_json_data,
|
| 414 |
PROFESSIONS_FILE,
|
| 415 |
TYPES_FILE,
|
|
|
|
| 551 |
)
|
| 552 |
|
| 553 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
# --- Gradio callback functions ---
|
| 555 |
|
| 556 |
def start_interview():
|
|
|
|
| 621 |
# --- Wire up the event handlers ---
|
| 622 |
|
| 623 |
# 1) Start button
|
|
|
|
| 624 |
start_btn.click(
|
| 625 |
start_interview,
|
| 626 |
inputs=[],
|
| 627 |
+
outputs=[chatbot, user_input, audio_output]
|
| 628 |
)
|
| 629 |
|
| 630 |
# 2) Audio: when recording stops
|
|
|
|
| 631 |
audio_input.stop_recording(
|
| 632 |
interview_step_wrapper,
|
| 633 |
inputs=[user_input, audio_input, chatbot],
|
| 634 |
+
outputs=[chatbot, user_input, audio_output]
|
| 635 |
)
|
| 636 |
|
| 637 |
# 3) Submit button
|
|
|
|
| 638 |
submit_btn.click(
|
| 639 |
interview_step_wrapper,
|
| 640 |
inputs=[user_input, audio_input, chatbot],
|
| 641 |
+
outputs=[chatbot, user_input, audio_output]
|
| 642 |
)
|
| 643 |
+
|
| 644 |
+
# 4) Pressing Enter in the textbox
|
| 645 |
+
user_input.submit(
|
| 646 |
on_enter_submit,
|
| 647 |
+
inputs=[chatbot, user_input],
|
| 648 |
+
outputs=[chatbot, user_input, audio_output]
|
| 649 |
)
|
| 650 |
|
| 651 |
# 5) Clear button
|
|
|
|
| 652 |
clear_btn.click(
|
| 653 |
clear_chat,
|
| 654 |
inputs=[],
|
| 655 |
+
outputs=[chatbot, user_input, audio_output]
|
| 656 |
)
|
| 657 |
|
|
|
|
| 658 |
# Launch Gradio (remove `share=True` if it keeps failing)
|
| 659 |
demo.launch(
|
| 660 |
server_name="0.0.0.0",
|