File size: 31,263 Bytes
5798cfc
 
3717960
 
5798cfc
3717960
 
5798cfc
 
3717960
 
 
5798cfc
 
3717960
5798cfc
 
78b8b85
 
 
e9a795e
 
 
5798cfc
 
 
 
3717960
5798cfc
 
 
 
 
3717960
 
 
 
 
 
 
 
 
 
 
5798cfc
 
3717960
 
 
78b8b85
 
 
 
3717960
78b8b85
3717960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b8b85
 
 
 
3717960
78b8b85
3717960
 
 
 
 
 
 
5798cfc
a1872b4
 
 
 
 
 
 
 
 
 
 
 
5798cfc
 
3717960
 
 
 
 
5798cfc
a1872b4
 
 
 
 
 
 
 
 
 
 
5798cfc
a1872b4
 
 
 
 
 
 
 
 
5798cfc
3717960
5798cfc
3717960
 
 
 
5798cfc
 
 
 
 
3717960
 
 
 
 
 
 
 
5798cfc
 
3717960
 
 
 
 
 
5798cfc
 
3717960
5798cfc
 
3717960
 
 
 
 
 
 
 
 
 
 
 
a1872b4
 
 
 
 
 
 
 
3717960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5798cfc
3717960
 
 
 
 
5798cfc
3717960
 
 
 
 
5798cfc
 
3717960
5798cfc
3717960
 
 
 
 
 
 
 
 
 
 
5798cfc
3717960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1872b4
5798cfc
 
3717960
 
 
 
5798cfc
3717960
 
5798cfc
3717960
 
5798cfc
3717960
5798cfc
 
3717960
 
5798cfc
3717960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5798cfc
3717960
a1872b4
5798cfc
3717960
 
5798cfc
 
3717960
 
5798cfc
 
3717960
5798cfc
3717960
e9a795e
3717960
 
 
 
 
 
5798cfc
78b8b85
a1872b4
 
 
3717960
 
 
 
 
 
 
 
 
5798cfc
3717960
 
 
b7dc77c
3717960
b7dc77c
 
 
 
e9a795e
b7dc77c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a795e
3717960
5798cfc
b7dc77c
 
 
 
3717960
 
 
 
 
 
 
 
 
 
a1872b4
b7dc77c
a1872b4
e9a795e
a1872b4
3717960
 
 
 
 
 
5798cfc
 
3717960
5798cfc
 
e9a795e
 
3717960
 
 
 
 
 
 
 
 
 
5798cfc
3717960
 
5798cfc
 
 
3717960
5798cfc
 
 
3717960
 
 
 
 
5798cfc
 
 
3717960
 
5798cfc
 
3717960
 
5798cfc
 
 
 
 
 
 
 
 
 
 
 
3717960
5798cfc
 
 
 
 
 
3717960
5798cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3717960
 
 
 
 
 
 
 
5798cfc
 
3717960
5798cfc
 
 
3717960
 
 
 
 
 
5798cfc
3717960
 
 
 
 
 
 
 
5798cfc
 
 
 
 
 
 
3717960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5798cfc
3717960
 
 
 
 
 
 
 
5798cfc
 
3717960
5798cfc
3717960
 
 
 
 
 
78b8b85
a1872b4
 
 
 
 
 
 
3717960
e9a795e
3717960
 
 
 
 
 
 
 
 
 
 
 
5798cfc
3717960
 
 
 
 
 
 
 
5798cfc
3717960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b8b85
3717960
 
 
 
5798cfc
3717960
 
 
 
b7dc77c
5798cfc
 
3717960
 
 
 
b7dc77c
3717960
5798cfc
3717960
 
 
 
b7dc77c
3717960
b7dc77c
 
 
3717960
b7dc77c
 
3717960
 
 
 
 
 
b7dc77c
3717960
 
 
 
 
 
 
 
5798cfc
 
 
a1872b4
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import os
import json
import time
import tempfile
from collections import deque

import gradio as gr
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage  # Import AIMessage
from openai import OpenAI
from datetime import datetime  # Import datetime for timestamp


# Load environment variables
load_dotenv()

# Initialize API key status message globally
initial_api_key_status_message = "Checking API Key..."

# Global variable for questions
questions = [] # Declare questions as a global variable

# Function to read questions from JSON
def read_questions_from_json(file_path):
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"The file '{file_path}' does not exist.")
    with open(file_path, 'r', encoding='utf-8') as f:
        questions_list = json.load(f)
    if not questions_list:
        raise ValueError("The JSON file is empty or has invalid content.")
    return questions_list

# Function to save interview history to JSON
def save_interview_history(history, filename="interview_history.json"):
    """Saves the interview history to a JSON file."""
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filepath = f"{timestamp}_{filename}"
    try:
        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(history, f, ensure_ascii=False, indent=4)
        print(f"Interview history saved to: {filepath}")
    except Exception as e:
        print(f"Error saving interview history: {e}")


# Function to convert text to speech (OpenAI's TTS usage, adjust if needed)
def convert_text_to_speech(text):
    start_time = time.time()
    api_key = os.getenv("OPENAI_API_KEY")
    if not api_key:
        print("API key is missing, cannot perform text-to-speech.")
        return None
    try:
        client = OpenAI(api_key=api_key)
        response = client.audio.speech.create(model="tts-1", voice="alloy", input=text)

        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
            for chunk in response.iter_bytes():
                tmp_file.write(chunk)
            temp_audio_path = tmp_file.name

        print(f"DEBUG - Text-to-speech conversion time: {time.time() - start_time:.2f} seconds")
        return temp_audio_path
    except Exception as e:
        print(f"Error during text-to-speech conversion: {e}")
        return None


# Function to transcribe audio (OpenAI Whisper usage, adjust if needed)
def transcribe_audio(audio_file_path):
    start_time = time.time()
    api_key = os.getenv("OPENAI_API_KEY")
    if not api_key:
        print("API key is missing, cannot perform audio transcription.")
        return None
    try:
        client = OpenAI(api_key=api_key)
        with open(audio_file_path, "rb") as audio_file:
            transcription = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
        print(f"DEBUG - Audio transcription time: {time.time() - start_time:.2f} seconds")
        return transcription.text
    except Exception as e:
        print(f"Error during audio transcription: {e}")
        return None

def check_api_key():
    """Checks if the OpenAI API key is valid."""
    api_key = os.getenv("OPENAI_API_KEY")
    if not api_key:
        return "❌ API Key Not Found. Please enter in Admin Panel."
    try:
        client = OpenAI(api_key=api_key)
        client.models.list() # Simple API call to check if the key is working
        return "✅ API Key Loaded."
    except Exception as e:
        return f"❌ API Key Invalid: {e}"


def conduct_interview(questions, language="English", history_limit=5):
    """
    Sets up a function (interview_step) that handles each round of Q&A.
    Returns (interview_step, initial_message, final_message).
    """
    start_time = time.time()
    openai_api_key = os.getenv("OPENAI_API_KEY")

    initial_message = (
        "👋 Hi there, I'm Sarah, your friendly AI HR assistant! "
        "I'll guide you through a series of interview questions to learn more about you. "
        "Take your time and answer each question thoughtfully."
    )
    final_message_content = (
        "That wraps up our interview. Thank you for your responses—it's been great learning more about you!"
        " I will share the feedback with HR Team, and they will reach out to you soon." # added line
    )

    if not openai_api_key:
        placeholder_message = "⚠️ OpenAI API Key not configured. Please enter your API key in the Admin Panel to start the interview."
        placeholder_audio_path = convert_text_to_speech(placeholder_message)

        def placeholder_interview_step(user_input, audio_input, history):
            history.append({"role": "assistant", "content": placeholder_message})
            return history, "", placeholder_audio_path

        return placeholder_interview_step, initial_message, final_message_content


    # LangChain-based ChatOpenAI
    chat = ChatOpenAI(
        openai_api_key=openai_api_key,
        model="gpt-4o",  # or "gpt-3.5-turbo", etc.
        temperature=0.7,
        max_tokens=750
    )

    conversation_history = deque(maxlen=history_limit)
    system_prompt = (
        f"You are Sarah, an empathetic HR interviewer conducting a technical interview in {language}. "
        "You respond politely, concisely, and provide clarifications if needed.  "
        "Ask only ONE question at a time. Wait for the user to respond before asking the next question.  "
        "Provide a very brief, positive acknowledgement of the user's response, *then* ask the next question. "
        "Limit follow-up questions to a maximum of ONE per main interview question to keep the interview concise." # Added instruction for single follow-up
        "If the user provides strange answers, give maximum one feedback and continue with the next question. Do not ask more follow up questions if the answer is strange."
        "After the last interview question is answered by the user, ask 'Do you have any questions for me?'. "
        "If the user asks questions, answer them concisely and politely. After answering user questions, or if the user says they have no questions, deliver the final message: '{final_message_placeholder}'. "
        "Keep track of the interview stage and manage the conversation flow accordingly."
    )

    current_question_index = [0]  # Store index in a list so it's mutable in nested func
    is_interview_finished = [False]  # Use a list for mutability
    interview_transcript = [] # List to store full interview history for saving
    follow_up_count = [0] # Counter for follow-up questions within the current main question
    interview_stage = ["questioning"] # "questioning", "user_questions_prompt", "answering_user_questions", "final_message_stage", "finished"
    user_questions_asked = [False] # Flag to track if "Do you have any questions?" has been asked


    updated_system_prompt = system_prompt.replace("{final_message_placeholder}", final_message_content)


    print(f"DEBUG - conduct_interview setup time: {time.time() - start_time:.2f} seconds")

    def interview_step(user_input, audio_input, history):
        """
        Called each time the user clicks submit or finishes audio recording.
        `history` is a list of { 'role': '...', 'content': '...' } messages.
        We must return an updated version of that list in the same format.
        """
        nonlocal current_question_index, is_interview_finished, interview_transcript, follow_up_count, interview_stage, user_questions_asked

        step_start_time = time.time()

        # Check if API key is configured before proceeding with OpenAI calls
        if not os.getenv("OPENAI_API_KEY"):
            api_missing_message = "⚠️ OpenAI API Key not configured. Please enter your API key in the Admin Panel to continue the interview."
            api_missing_audio_path = convert_text_to_speech(api_missing_message)
            history.append({"role": "assistant", "content": api_missing_message})
            return history, "", api_missing_audio_path


        # If there's audio, transcribe it.
        if audio_input:
            transcript = transcribe_audio(audio_input)
            user_input = transcript if transcript else user_input  # Use transcribed text if available

        # If user typed "exit" or "quit"
        if user_input.strip().lower() in ["exit", "quit"]:
            history.append({
                "role": "assistant",
                "content": "The interview has ended at your request. Thank you for your time!"
            })
            is_interview_finished[0] = True
            save_interview_history(interview_transcript) # Save history before exit
            return history, "", None

        # If the interview is already finished, do nothing.
        if is_interview_finished[0]:
            return history, "", None

        # Add user's input to history
        history.append({"role": "user", "content": user_input})
        interview_transcript.append({"role": "user", "content": user_input}) # Add to transcript

        #This is a new user response, add to the short history
        conversation_history.append({
            "question": questions[current_question_index[0]] if current_question_index[0] < len(questions) and interview_stage[0] == "questioning" else ("User Question" if interview_stage[0] == "answering_user_questions" else "End of interview"), # to handle index out of bound during final step
            "answer": user_input
        })

        # Build the prompt
        short_history = "\n".join([
            f"Q: {entry['question']}\nA: {entry['answer']}"
            for entry in conversation_history
        ])


        messages = []

        if interview_stage[0] == "questioning":
            # Normal question flow
            combined_prompt = (
                f"{updated_system_prompt}\n\nPrevious Q&A:\n{short_history}\n\n"
                f"User's input: {user_input}\n\n"
                "Acknowledge the user's answer briefly, then ask the *next* question, unless this was the last question."
            )
            messages = [
                SystemMessage(content=updated_system_prompt),
                HumanMessage(content=combined_prompt),
            ]

        elif interview_stage[0] == "user_questions_prompt" or interview_stage[0] == "answering_user_questions":
            # Handling user questions phase
            combined_prompt = (
                f"{updated_system_prompt}\n\nPrevious Q&A:\n{short_history}\n\n"
                f"User's input (User Question): {user_input}\n\n"
                "Answer the user's question concisely and politely. If the user says they have no questions or similar, then deliver the final message."
            )
            messages = [
                SystemMessage(content=updated_system_prompt),
                HumanMessage(content=combined_prompt),
            ]
        elif interview_stage[0] == "final_message_stage":
            # Should not reach here as final message is sent directly and stage becomes "finished"
            pass
        elif interview_stage[0] == "finished":
            return history, "", None  # Interview is finished


        if messages: # Proceed only if messages are prepared (not in final_message_stage or finished)
            # Ask ChatOpenAI
            response = chat.invoke(messages)
            response_content = response.content.strip()

            history.append({"role": "assistant", "content": response_content})
            interview_transcript.append({"role": "assistant", "content": response_content}) # Add to transcript

            # Convert the LLM's answer to speech
            audio_file_path = convert_text_to_speech(response_content)
        else:
            audio_file_path = None


        if interview_stage[0] == "questioning":
            # Advance to the next question or handle end of questions

            follow_up_count[0] = 0 # Reset follow-up counter for the next main question
            if current_question_index[0] < len(questions) -1 :  # Check against len(questions) - 1
                current_question_index[0] += 1
                print(f"DEBUG - question index {current_question_index[0]}")
                print("DEBUG - Moving to next main question.")
                print(f"DEBUG - Interview step time: {time.time() - step_start_time:.2f} seconds")
                return history, "", audio_file_path  # Return current audio
            else:
                # Last question answered, ask "Do you have any questions?"
                if not user_questions_asked[0]:
                    user_questions_prompt_message = "Thank you for your answer. Do you have any questions for me?"
                    user_questions_audio_path = convert_text_to_speech(user_questions_prompt_message)
                    history.append({"role": "assistant", "content": user_questions_prompt_message})
                    interview_transcript.append({"role": "assistant", "content": user_questions_prompt_message})
                    interview_stage[0] = "user_questions_prompt"
                    user_questions_asked[0] = True # Ensure this prompt is only asked once
                    print("DEBUG - Asked 'Do you have any questions?'")
                    print(f"DEBUG - Interview step time: {time.time() - step_start_time:.2f} seconds")
                    return history, "", user_questions_audio_path
                else:
                    # This should not be reached in normal flow for last question, but as a fallback.
                    pass # Fallthrough to handle user questions or finalize below

        if interview_stage[0] == "user_questions_prompt":
            # Check if user has questions or says no questions
            if user_input.strip().lower() in ["no", "no questions", "none", "nothing", "that's all", "no, thank you"]:
                final_audio_path = convert_text_to_speech(final_message_content)
                history.append({"role": "assistant", "content": final_message_content})
                interview_transcript.append({"role": "assistant", "content": final_message_content})
                interview_stage[0] = "finished"
                is_interview_finished[0] = True
                save_interview_history(interview_transcript) # Save history at the end
                print("DEBUG - Interview finished after user said no questions.")
                print(f"DEBUG - Interview step time: {time.time() - step_start_time:.2f} seconds")
                return history, "", final_audio_path
            else:
                # User asked a question, move to answering stage
                interview_stage[0] = "answering_user_questions"
                print("DEBUG - User asked a question, moving to answering stage.")
                print(f"DEBUG - Interview step time: {time.time() - step_start_time:.2f} seconds")
                return history, "", audio_file_path # Respond with the AI's answer to user's question in the 'messages' processing block

        elif interview_stage[0] == "answering_user_questions":
            # After answering user question, go back to user_questions_prompt to allow more questions or finalize
            interview_stage[0] = "user_questions_prompt"
            print("DEBUG - Answered user question, back to user_questions_prompt.")
            print(f"DEBUG - Interview step time: {time.time() - step_start_time:.2f} seconds")
            return history, "", audio_file_path # Already responded in 'messages' block

        elif interview_stage[0] == "final_message_stage": # Redundant stage, final message sent directly when no more questions
            pass # Should not reach here

        elif interview_stage[0] == "finished":
            return history, "", None  # Interview already finished

        print(f"DEBUG - Interview step time: {time.time() - step_start_time:.2f} seconds")
        return history, "", audio_file_path


    # Return the step function plus initial/final text
    return interview_step, initial_message, final_message_content


def main():
    QUESTIONS_FILE_PATH = "questions.json"
    try:
        global questions # Use the global questions variable
        questions = read_questions_from_json(QUESTIONS_FILE_PATH)
        num_questions = len(questions) # Count the number of questions
        print(f"Loaded {num_questions} questions from {QUESTIONS_FILE_PATH}") # Inform user about question count
    except Exception as e:
        print(f"Error reading questions: {e}")
        return

    global initial_api_key_status_message # Access and set the global variable
    initial_api_key_status_message = check_api_key() # Check API key and update status

    interview_func, initial_message, final_message = conduct_interview(questions) # Initialize even if API key is missing

    css = """
    .contain { display: flex; flex-direction: column; }
    .gradio-container { height: 100vh !important; overflow-y: auto; }
    #component-0 { height: 100%; }
    .chatbot { flex-grow: 1; overflow: auto; height: 650px; }
    .user > div > .message { background-color: #dcf8c6 !important }
    .bot > div > .message { background-color: #f7f7f8 !important }
    """

    # Build Gradio interface
    with gr.Blocks(css=css) as demo:
        gr.Markdown(
            "<h1 style='text-align:center;'>👋 AI HR Interview Assistant</h1>"
        )
        gr.Markdown(
            "I will ask you a series of questions. Please answer honestly and thoughtfully. "
            "When you are ready, click **Start Interview** to begin."
        )
        start_btn = gr.Button(" Start Interview", variant="primary")
        chatbot = gr.Chatbot( # Moved up here
            label="Interview Chat",
            height=650,
            type='messages'  # must return a list of dicts: {"role":..., "content":...}
        )
        audio_input = gr.Audio( # Moved up here
            sources=["microphone"],
            type="filepath",
            label="Record Your Answer"
        )
        user_input = gr.Textbox( # Moved up here
            label="Your Response",
            placeholder="Type your answer here or use the microphone...",
            lines=1,
        )
        audio_output = gr.Audio(label="Response Audio", autoplay=True) # Moved up here


        


        with gr.Row():
            submit_btn = gr.Button("Submit", variant="primary")
            clear_btn = gr.Button("Clear Chat")

        # Admin Panel Tab
        with gr.Tab("Admin Panel", id="admin_tab"):
            with gr.Tab("API Key Settings"):
                gr.Markdown("### OpenAI API Key Configuration")
                api_key_input = gr.Textbox(label="Enter your OpenAI API Key", type="password", placeholder="••••••••••••••••••••••••••••••••")
                api_key_status_output = gr.Textbox(label="API Key Status", value=initial_api_key_status_message, interactive=False)
                update_api_key_button = gr.Button("Update API Key")
                gr.Markdown("*This application does not store your API key. It is used only for this session and is not persisted when you close the app.*")

                def update_api_key(api_key):
                    os.environ["OPENAI_API_KEY"] = api_key  # Caution: Modifying os.environ is session-based
                    global interview_func, initial_message, final_message, questions, initial_api_key_status_message  # Declare globals to update them and questions
                    initial_api_key_status_message = check_api_key() # Update status immediately after key is entered
                    interview_func, initial_message, final_message = conduct_interview(questions)  # Re-init interview function, now questions is in scope
                    return initial_api_key_status_message # Return status message

                update_api_key_button.click(
                    update_api_key,
                    inputs=[api_key_input],
                    outputs=[api_key_status_output],
                )


            # with gr.Tab("Generate Questions"):
            with gr.Tab("Generate Questions"):
                try:
                    # Assuming these are defined in backend2.py
                    from backend3 import (
                        load_json_data,
                        PROFESSIONS_FILE,
                        TYPES_FILE,
                        generate_questions_manager,
                        update_max_questions,
                        generate_and_save_questions_from_pdf3,
                        generate_questions_from_job_description,
                        cleanup
                    )

                    professions_data = load_json_data(PROFESSIONS_FILE)
                    types_data     = load_json_data(TYPES_FILE)

                except (FileNotFoundError, json.JSONDecodeError) as e:
                    print(f"Error loading data from JSON files: {e}")
                    professions_data = []
                    types_data     = []

                profession_names = [
                    item["profession"] for item in professions_data
                ] if professions_data else []

                interview_types = [
                    item["type"] for item in types_data
                ] if types_data else []

                with gr.Row():
                    profession_input = gr.Dropdown(
                        label="Select Profession",
                        choices=profession_names
                    )
                    interview_type_input = gr.Dropdown(
                        label="Select Interview Type",
                        choices=interview_types
                    )

                num_questions_input = gr.Number(
                    label="Number of Questions (1-20)",
                    value=5,
                    precision=0,
                    minimum=1,
                    maximum=20,
                )
                overwrite_input = gr.Checkbox(
                    label="Overwrite all_questions.json?", value=True
                )

                # Update num_questions_input when interview_type_input changes
                interview_type_input.change(
                    fn=update_max_questions,
                    inputs=interview_type_input,
                    outputs=num_questions_input,
                )

                generate_button = gr.Button("Generate Questions")

                output_text = gr.Textbox(label="Output")
                question_output = gr.JSON(label="Generated Questions")

                generate_button.click(
                    generate_questions_manager,
                    inputs=[
                        profession_input,
                        interview_type_input,
                        num_questions_input,
                        overwrite_input,
                    ],
                    outputs=[output_text, question_output],
                )


            with gr.Tab("Generate from PDF"):
                gr.Markdown("### 📄 Upload PDF for Question Generation")
                pdf_file_input = gr.File(label="Upload PDF File", type="filepath")
                num_questions_pdf_input = gr.Number(
                    label="Number of Questions (1-30)",
                    value=5,
                    precision=0,
                    minimum=1,
                    maximum=30,
                )

                pdf_status_output = gr.Textbox(label="Status", lines=3)
                pdf_question_output = gr.JSON(label="Generated Questions")

                generate_pdf_button = gr.Button("Generate Questions from PDF")

                def update_pdf_ui(pdf_path, num_questions):
                    print(f"[DEBUG] PDF Path: {pdf_path}")
                    print(f"[DEBUG] Requested Number of Questions: {num_questions}")

                    all_statuses = []
                    all_questions = []
                    print(f"[DEBUG] Calling generate_and_save_questions_from_pdf3 with {num_questions}")
                    for status, questions in generate_and_save_questions_from_pdf3(pdf_path, num_questions):
                        print(f"[DEBUG] Status: {status}, Questions Generated: {len(questions)}")
                        all_statuses.append(status)
                        all_questions.append(questions)

                    combined_status = "\n".join(all_statuses)
                    final_questions = all_questions[-1] if all_questions else []

                    return gr.update(value=combined_status), gr.update(value=final_questions)

                generate_pdf_button.click(
                    update_pdf_ui,
                    inputs=[pdf_file_input, num_questions_pdf_input],
                    outputs=[pdf_status_output, pdf_question_output],
                )

            with gr.Tab("Generate from Job Description"):
                gr.Markdown("### 📝 Enter Job Description for Question Generation")

                job_description_input = gr.Textbox(label="Job Description", placeholder="Type or paste the job description here...", lines=6)
                num_questions_job_input = gr.Number(
                    label="Number of Questions (1-30)",
                    value=5,
                    precision=0,
                    minimum=1,
                    maximum=30
                )

                job_status_output = gr.Textbox(label="Status", lines=3)
                job_question_output = gr.JSON(label="Generated Questions")

                generate_job_button = gr.Button("Generate Questions from Job Description")

                def update_job_description_ui(job_description, num_questions):
                    print(f"[DEBUG] Job Description Length: {len(job_description)} characters")
                    print(f"[DEBUG] Requested Number of Questions: {num_questions}")

                    status, questions = generate_questions_from_job_description(job_description, num_questions)
                    return gr.update(value=status), gr.update(value=questions)

                generate_job_button.click(
                    update_job_description_ui,
                    inputs=[job_description_input, num_questions_job_input],
                    outputs=[job_status_output, job_question_output],
                )


        # --- Gradio callback functions ---

        def start_interview():
            """
            Resets the chat and provides an initial greeting and first question.
            Must return a list of {'role':'assistant','content':'...'} messages
            plus empty text for user_input and path for audio_output.
            """
            global interview_func, questions, initial_api_key_status_message # Access global variables, use global not nonlocal here

            current_api_key_status = check_api_key() # Check API key status right before starting interview
            if not current_api_key_status.startswith("✅"): # If API key is not valid
                error_message = "Please set a valid OpenAI API Key in the Admin Panel before starting the interview."
                tts_path = convert_text_to_speech(error_message)
                return [{"role": "assistant", "content": error_message}], "", tts_path

            try:
                global questions # Ensure we are using the global questions variable
                questions = read_questions_from_json(QUESTIONS_FILE_PATH) # Reload questions in case file changed
                interview_func, initial_message, final_message = conduct_interview(questions) # Re-init interview func with new questions
            except Exception as e:
                error_message = f"Error reloading questions or setting up interview: {e}. Please check questions.json and API Key."
                print(error_message)
                tts_path = convert_text_to_speech(error_message)
                return [{"role": "assistant", "content": error_message}], "", tts_path # Return error message to chatbot

            history = []
            # Combine initial + the first question
            if questions:
                first_q_text = f" Let's begin! Here's your first question: {questions[0]}"
            else:
                first_q_text = "No questions loaded. Please check questions.json or generate questions in the Admin Panel."

            combined = initial_message + first_q_text
            tts_path = convert_text_to_speech(combined)

            # Return one assistant message to the Chatbot
            history.append({"role": "assistant", "content": combined})
            return history, "", tts_path

        def interview_step_wrapper(user_response, audio_response, history):
            """
            Wrap the 'interview_func' so we always return the correct format:
            (list_of_dicts, str, audio_file_path).
            """
            new_history, _, audio_path = interview_func(user_response, audio_response, history)
            return new_history, "", audio_path

        def on_enter_submit(history, user_text):
            """
            If user presses Enter in the textbox. Return updated Chatbot history,
            empty user_input, and any audio.
            """
            if not user_text.strip():
                # If empty, do nothing
                return history, "", None
            new_history, _, audio_path = interview_func(user_text, None, history)
            return new_history, "", audio_path

        def clear_chat():
            """
            Re-initialize the interview function entirely
            to start from scratch, clearing the Chatbot.
            """
            global interview_func, initial_message, final_message, questions # Access global variables, use global not nonlocal here
            interview_func, initial_msg, final_msg = conduct_interview(questions) # Re-init with current questions
            return [], "", None

        # --- Wire up the event handlers ---

        # 1) Start button
        start_btn.click(
            start_interview,
            inputs=[],
            outputs=[chatbot, user_input, audio_output]
        )

        # 2) Audio: when recording stops
        audio_input.stop_recording(
            interview_step_wrapper,
            inputs=[user_input, audio_input, chatbot],
            outputs=[chatbot, user_input, audio_output]
        )

        # 3) Submit button
        submit_btn.click(
            interview_step_wrapper,
            inputs=[user_input, audio_input, chatbot],
            outputs=[chatbot, user_input, audio_output]
        )

        # 4) Pressing Enter in the textbox
        user_input.submit(
            on_enter_submit,
            inputs=[chatbot, user_input],
            outputs=[chatbot, user_input, audio_output]
        )

        # 5) Clear button
        clear_btn.click(
            clear_chat,
            inputs=[],
            outputs=[chatbot, user_input, audio_output]
        )

    # Launch Gradio (remove `share=True` if it keeps failing)
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        # share=True  # Remove or comment out if you get share-link errors
    )


if __name__ == "__main__":
    main()