ruslanmv commited on
Commit
253e25a
Β·
verified Β·
1 Parent(s): 192d45f

Upload generatorgr.py

Browse files
Files changed (1) hide show
  1. generatorgr.py +125 -121
generatorgr.py CHANGED
@@ -1,121 +1,125 @@
1
- import gradio as gr
2
- import json
3
- import time
4
- import os
5
-
6
- from generator import PROFESSIONS_FILE, TYPES_FILE, OUTPUT_FILE
7
- from generator import generate_questions, load_json_data, save_questions_to_file
8
-
9
- # Load professions and interview types from JSON files
10
- try:
11
- professions_data = load_json_data(PROFESSIONS_FILE)
12
- types_data = load_json_data(TYPES_FILE)
13
- except (FileNotFoundError, json.JSONDecodeError) as e:
14
- print(f"Error loading data from JSON files: {e}")
15
- professions_data = []
16
- types_data = []
17
-
18
- # Extract profession names and interview types for the dropdown menus
19
- profession_names = [item["profession"] for item in professions_data]
20
- interview_types = [item["type"] for item in types_data]
21
-
22
- # Define path for the questions.json file
23
- QUESTIONS_FILE = "questions.json"
24
-
25
-
26
- def generate_and_save_questions(profession, interview_type, num_questions, overwrite=True, progress=gr.Progress()):
27
- """
28
- Generates questions using the generate_questions function and saves them to JSON files.
29
- Provides progress updates.
30
- """
31
- profession_info = next(
32
- (item for item in professions_data if item["profession"] == profession), None
33
- )
34
- interview_type_info = next(
35
- (item for item in types_data if item["type"] == interview_type), None
36
- )
37
-
38
- if profession_info is None or interview_type_info is None:
39
- return "Error: Invalid profession or interview type selected.", None
40
-
41
- description = profession_info["description"]
42
- max_questions = min(int(num_questions), 20) # Ensure max is 20
43
-
44
- progress(0, desc="Starting question generation...")
45
-
46
- questions = generate_questions(
47
- profession, interview_type, description, max_questions
48
- )
49
-
50
- progress(0.5, desc=f"Generated {len(questions)} questions. Saving...")
51
-
52
- # Save the generated questions to the all_questions.json file
53
-
54
- all_questions_entry = {
55
- "profession": profession,
56
- "interview_type": interview_type,
57
- "description": description,
58
- "max_questions": max_questions,
59
- "questions": questions,
60
- }
61
-
62
-
63
- save_questions_to_file(OUTPUT_FILE, [all_questions_entry], overwrite=overwrite)
64
-
65
- # Save the generated questions to the new questions.json file
66
- with open(QUESTIONS_FILE, "w") as outfile:
67
- json.dump(questions, outfile, indent=4)
68
-
69
- progress(1, desc="Questions saved.")
70
-
71
- return (
72
- f"βœ… Questions generated and saved for {profession} ({interview_type}). Max questions: {max_questions}",
73
- questions,
74
- )
75
-
76
-
77
-
78
- def update_max_questions(interview_type):
79
- """
80
- Updates the default value of the number input based on the selected interview type.
81
- """
82
- interview_type_info = next(
83
- (item for item in types_data if item["type"] == interview_type), None
84
- )
85
- if interview_type_info:
86
- default_max_questions = interview_type_info.get("max_questions", 5)
87
- return gr.update(value=default_max_questions, minimum=1, maximum=20)
88
- else:
89
- return gr.update(value=5, minimum=1, maximum=20)
90
-
91
-
92
- with gr.Blocks() as demo:
93
- gr.Markdown("## πŸ“„ Interview Question Generator for IBM CIC")
94
- with gr.Row():
95
- profession_input = gr.Dropdown(label="Select Profession", choices=profession_names)
96
- interview_type_input = gr.Dropdown(label="Select Interview Type", choices=interview_types)
97
-
98
- num_questions_input = gr.Number(
99
- label="Number of Questions (1-20)", value=5, precision=0, minimum=1, maximum=20
100
- )
101
-
102
- generate_button = gr.Button("Generate Questions")
103
-
104
- output_text = gr.Textbox(label="Output")
105
- question_output = gr.JSON(label="Generated Questions")
106
-
107
- # Update num_questions_input when interview_type_input changes
108
- interview_type_input.change(
109
- fn=update_max_questions,
110
- inputs=interview_type_input,
111
- outputs=num_questions_input,
112
- )
113
-
114
- generate_button.click(
115
- generate_and_save_questions,
116
- inputs=[profession_input, interview_type_input, num_questions_input],
117
- outputs=[output_text, question_output],
118
- )
119
-
120
- if __name__ == "__main__":
121
- demo.queue().launch()
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import time
4
+ import os
5
+
6
+ from generator import PROFESSIONS_FILE, TYPES_FILE, OUTPUT_FILE
7
+ from generator import generate_questions, load_json_data, save_questions_to_file
8
+ from splitgpt import save_questions
9
+ # Load professions and interview types from JSON files
10
+ try:
11
+ professions_data = load_json_data(PROFESSIONS_FILE)
12
+ types_data = load_json_data(TYPES_FILE)
13
+ except (FileNotFoundError, json.JSONDecodeError) as e:
14
+ print(f"Error loading data from JSON files: {e}")
15
+ professions_data = []
16
+ types_data = []
17
+
18
+ # Extract profession names and interview types for the dropdown menus
19
+ profession_names = [item["profession"] for item in professions_data]
20
+ interview_types = [item["type"] for item in types_data]
21
+
22
+ # Define path for the questions.json file
23
+ QUESTIONS_FILE = "questions.json"
24
+
25
+
26
+ def generate_and_save_questions(profession, interview_type, num_questions, overwrite=True, progress=gr.Progress()):
27
+ """
28
+ Generates questions using the generate_questions function and saves them to JSON files.
29
+ Provides progress updates.
30
+ """
31
+ profession_info = next(
32
+ (item for item in professions_data if item["profession"] == profession), None
33
+ )
34
+ interview_type_info = next(
35
+ (item for item in types_data if item["type"] == interview_type), None
36
+ )
37
+
38
+ if profession_info is None or interview_type_info is None:
39
+ return "Error: Invalid profession or interview type selected.", None
40
+
41
+ description = profession_info["description"]
42
+ max_questions = min(int(num_questions), 20) # Ensure max is 20
43
+
44
+ progress(0, desc="Starting question generation...")
45
+
46
+ questions = generate_questions(
47
+ profession, interview_type, description, max_questions
48
+ )
49
+
50
+ progress(0.5, desc=f"Generated {len(questions)} questions. Saving...")
51
+
52
+ # Save the generated questions to the all_questions.json file
53
+
54
+ all_questions_entry = {
55
+ "profession": profession,
56
+ "interview_type": interview_type,
57
+ "description": description,
58
+ "max_questions": max_questions,
59
+ "questions": questions,
60
+ }
61
+
62
+
63
+ save_questions_to_file(OUTPUT_FILE, [all_questions_entry], overwrite=overwrite)
64
+
65
+ save_questions(questions)
66
+
67
+ # Save the generated questions to the new questions.json file
68
+ with open(QUESTIONS_FILE, "w") as outfile:
69
+ json.dump(questions, outfile, indent=4)
70
+
71
+ progress(1, desc="Questions saved.")
72
+
73
+ return (
74
+ f"βœ… Questions generated and saved for {profession} ({interview_type}). Max questions: {max_questions}",
75
+ questions,
76
+ )
77
+
78
+
79
+
80
+ def update_max_questions(interview_type):
81
+ """
82
+ Updates the default value of the number input based on the selected interview type.
83
+ """
84
+ interview_type_info = next(
85
+ (item for item in types_data if item["type"] == interview_type), None
86
+ )
87
+ if interview_type_info:
88
+ default_max_questions = interview_type_info.get("max_questions", 5)
89
+ return gr.update(value=default_max_questions, minimum=1, maximum=20)
90
+ else:
91
+ return gr.update(value=5, minimum=1, maximum=20)
92
+
93
+ '''
94
+ with gr.Blocks() as demo:
95
+ gr.Markdown("## πŸ“„ Interview Question Generator for IBM CIC")
96
+ with gr.Row():
97
+ profession_input = gr.Dropdown(label="Select Profession", choices=profession_names)
98
+ interview_type_input = gr.Dropdown(label="Select Interview Type", choices=interview_types)
99
+
100
+ num_questions_input = gr.Number(
101
+ label="Number of Questions (1-20)", value=5, precision=0, minimum=1, maximum=20
102
+ )
103
+
104
+ generate_button = gr.Button("Generate Questions")
105
+
106
+ output_text = gr.Textbox(label="Output")
107
+ question_output = gr.JSON(label="Generated Questions")
108
+
109
+ # Update num_questions_input when interview_type_input changes
110
+ interview_type_input.change(
111
+ fn=update_max_questions,
112
+ inputs=interview_type_input,
113
+ outputs=num_questions_input,
114
+ )
115
+
116
+ generate_button.click(
117
+ generate_and_save_questions,
118
+ inputs=[profession_input, interview_type_input, num_questions_input],
119
+ outputs=[output_text, question_output],
120
+ )
121
+
122
+ if __name__ == "__main__":
123
+ demo.queue().launch()
124
+
125
+ '''