File size: 4,321 Bytes
253e25a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import json
import time
import os

from generator import PROFESSIONS_FILE, TYPES_FILE, OUTPUT_FILE
from generator import generate_questions, load_json_data, save_questions_to_file
from  splitgpt import save_questions
# Load professions and interview types from JSON files
try:
    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 = []

# Extract profession names and interview types for the dropdown menus
profession_names = [item["profession"] for item in professions_data]
interview_types = [item["type"] for item in types_data]

# Define path for the questions.json file
QUESTIONS_FILE = "questions.json"


def generate_and_save_questions(profession, interview_type, num_questions, overwrite=True, progress=gr.Progress()):
    """

    Generates questions using the generate_questions function and saves them to JSON files.

    Provides progress updates.

    """
    profession_info = next(
        (item for item in professions_data if item["profession"] == profession), None
    )
    interview_type_info = next(
        (item for item in types_data if item["type"] == interview_type), None
    )

    if profession_info is None or interview_type_info is None:
        return "Error: Invalid profession or interview type selected.", None

    description = profession_info["description"]
    max_questions = min(int(num_questions), 20)  # Ensure max is 20

    progress(0, desc="Starting question generation...")

    questions = generate_questions(
        profession, interview_type, description, max_questions
    )

    progress(0.5, desc=f"Generated {len(questions)} questions. Saving...")

    # Save the generated questions to the all_questions.json file
    
    all_questions_entry = {
            "profession": profession,
            "interview_type": interview_type,
            "description": description,
            "max_questions": max_questions,
            "questions": questions,
        }
    
    
    save_questions_to_file(OUTPUT_FILE, [all_questions_entry], overwrite=overwrite)

    save_questions(questions)

    # Save the generated questions to the new questions.json file
    with open(QUESTIONS_FILE, "w") as outfile:
        json.dump(questions, outfile, indent=4)

    progress(1, desc="Questions saved.")

    return (
        f"βœ… Questions generated and saved for {profession} ({interview_type}). Max questions: {max_questions}",
        questions,
    )



def update_max_questions(interview_type):
    """

    Updates the default value of the number input based on the selected interview type.

    """
    interview_type_info = next(
        (item for item in types_data if item["type"] == interview_type), None
    )
    if interview_type_info:
        default_max_questions = interview_type_info.get("max_questions", 5)
        return gr.update(value=default_max_questions, minimum=1, maximum=20)
    else:
        return gr.update(value=5, minimum=1, maximum=20)

'''

with gr.Blocks() as demo:

    gr.Markdown("## πŸ“„ Interview Question Generator for IBM CIC")

    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

    )



    generate_button = gr.Button("Generate Questions")



    output_text = gr.Textbox(label="Output")

    question_output = gr.JSON(label="Generated Questions")



    # 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.click(

        generate_and_save_questions,

        inputs=[profession_input, interview_type_input, num_questions_input],

        outputs=[output_text, question_output],

    )



if __name__ == "__main__":

    demo.queue().launch()



'''