Mahmoud-Dev commited on
Commit
2025989
·
verified ·
1 Parent(s): 6419b7f

Fix: Remove size parameter and improve error handling for dataset loading

Browse files
Files changed (1) hide show
  1. app.py +100 -84
app.py CHANGED
@@ -1,120 +1,135 @@
1
  import gradio as gr
2
  import random
3
- import json
4
  from datasets import load_dataset
5
 
6
- try:
7
- ds = load_dataset('Aiman1234/Interview-questions')
8
- questions_data = ds['train']
9
- except:
10
- questions_data = None
 
 
 
 
 
 
 
 
11
 
12
  class InterviewBot:
13
  def __init__(self):
14
  self.current_question = None
15
  self.score = 0
16
  self.total_questions = 0
17
- self.performance_notes = []
18
  self.answers_given = []
19
 
20
  def get_next_question(self, difficulty='Easy'):
21
- if questions_data is None:
22
- return "Dataset not available", "N/A", "N/A"
23
 
24
- filtered = [q for q in questions_data if q['level'] == difficulty]
25
- if not filtered:
26
- filtered = questions_data
 
 
 
 
27
 
28
- self.current_question = random.choice(filtered)
29
- self.total_questions += 1
30
-
31
- return (
32
- self.current_question['Questions'],
33
- self.current_question['language'],
34
- self.current_question['level']
35
- )
36
 
37
  def evaluate_answer(self, user_answer):
38
  if not self.current_question:
39
  return "Please get a question first!"
40
 
41
- answer_length = len(user_answer.split())
42
- correct_answer_length = len(self.current_question['Answers'].split())
43
-
44
- similarity = min(answer_length, correct_answer_length) / max(answer_length, correct_answer_length) if max(answer_length, correct_answer_length) > 0 else 0
45
-
46
- if similarity > 0.5:
47
- feedback = f"Good effort! Your answer covers key points.\nExpected answer length: ~{correct_answer_length} words, Your answer: {answer_length} words"
48
- self.score += 1
49
- else:
50
- feedback = f"Consider expanding your answer. Your response should be more detailed.\nExpected length: ~{correct_answer_length} words, Your answer: {answer_length} words"
51
-
52
- self.answers_given.append({
53
- 'question': self.current_question['Questions'],
54
- 'your_answer': user_answer,
55
- 'similarity': similarity,
56
- 'correct_answer': self.current_question['Answers']
57
- })
58
-
59
- return feedback
 
 
60
 
61
  def get_performance_analysis(self):
62
  if self.total_questions == 0:
63
  return "No questions answered yet!"
64
 
65
- accuracy = (self.score / self.total_questions) * 100
66
-
67
- analysis = f"""
68
- === PERFORMANCE ANALYSIS ===
69
-
70
- Total Questions: {self.total_questions}
71
- Correct Answers: {self.score}
72
- Accuracy: {accuracy:.1f}%
73
-
74
- STRENGTHS:
75
- - You completed {self.total_questions} interview questions
76
- - Your accuracy rate is {accuracy:.1f}%
77
-
78
- AREAS FOR IMPROVEMENT:
79
- - Focus on providing detailed and structured answers
80
- - Practice technical concepts and definitions
81
- - Improve answer articulation and clarity
82
-
83
- RECOMMENDATIONS:
84
- 1. Review the expected answers for failed questions
85
- 2. Practice similar questions in your weak areas
86
- 3. Work on concise but comprehensive responses
87
- 4. Practice explaining complex concepts simply
88
- """
89
-
90
- return analysis
 
 
91
 
92
  bot = InterviewBot()
93
 
94
  def start_interview(difficulty):
95
- question, language, level = bot.get_next_question(difficulty)
96
- return f"Question ({language} - {level}): {question}"
 
 
 
97
 
98
  def submit_answer(answer_text):
99
  if not answer_text.strip():
100
  return "Please provide an answer!"
101
- evaluation = bot.evaluate_answer(answer_text)
102
- return evaluation
103
 
104
  def show_analysis():
105
  return bot.get_performance_analysis()
106
 
107
  with gr.Blocks(title="AI Job Interview Bot - Computer Science") as demo:
108
  gr.Markdown("""
109
- # AI Job Interview Bot for Computer Science
110
-
111
- Practice technical interview questions with AI-powered feedback!
112
-
113
- **Features:**
114
- - 496+ real interview questions
115
- - Multiple difficulty levels
116
- - Performance analysis and feedback
117
- - Identified strengths and weaknesses
118
  """)
119
 
120
  with gr.Row():
@@ -132,7 +147,7 @@ with gr.Blocks(title="AI Job Interview Bot - Computer Science") as demo:
132
  question_display = gr.Textbox(
133
  label="Question",
134
  interactive=False,
135
- lines=4
136
  )
137
 
138
  with gr.Row():
@@ -155,11 +170,12 @@ with gr.Blocks(title="AI Job Interview Bot - Computer Science") as demo:
155
 
156
  with gr.Row():
157
  analysis_btn = gr.Button("Show Performance Analysis", variant="secondary")
158
- analysis_display = gr.Textbox(
159
- label="Performance Analysis",
160
- interactive=False,
161
- lines=15
162
- )
 
163
 
164
  start_btn.click(
165
  fn=start_interview,
 
1
  import gradio as gr
2
  import random
 
3
  from datasets import load_dataset
4
 
5
+ questions_data = None
6
+
7
+ def load_questions():
8
+ global questions_data
9
+ if questions_data is None:
10
+ try:
11
+ ds = load_dataset('Aiman1234/Interview-questions')
12
+ questions_data = ds['train']
13
+ return True
14
+ except Exception as e:
15
+ print(f"Error loading dataset: {e}")
16
+ return False
17
+ return True
18
 
19
  class InterviewBot:
20
  def __init__(self):
21
  self.current_question = None
22
  self.score = 0
23
  self.total_questions = 0
 
24
  self.answers_given = []
25
 
26
  def get_next_question(self, difficulty='Easy'):
27
+ if not load_questions():
28
+ return "Failed to load questions. Please try again.", "N/A", "N/A"
29
 
30
+ try:
31
+ filtered = [q for q in questions_data if q['level'] == difficulty]
32
+ if not filtered:
33
+ filtered = list(questions_data)
34
+
35
+ self.current_question = random.choice(filtered)
36
+ self.total_questions += 1
37
 
38
+ return (
39
+ self.current_question['Questions'],
40
+ self.current_question['language'],
41
+ self.current_question['level']
42
+ )
43
+ except Exception as e:
44
+ return f"Error getting question: {str(e)}", "Error", "Error"
 
45
 
46
  def evaluate_answer(self, user_answer):
47
  if not self.current_question:
48
  return "Please get a question first!"
49
 
50
+ try:
51
+ answer_length = len(user_answer.split())
52
+ correct_answer_length = len(self.current_question['Answers'].split())
53
+
54
+ similarity = min(answer_length, correct_answer_length) / max(answer_length, correct_answer_length) if max(answer_length, correct_answer_length) > 0 else 0
55
+
56
+ if similarity > 0.5:
57
+ feedback = f"Good effort! Your answer covers key points.\nExpected answer length: ~{correct_answer_length} words\nYour answer: {answer_length} words"
58
+ self.score += 1
59
+ else:
60
+ feedback = f"Consider expanding your answer. Your response should be more detailed.\nExpected length: ~{correct_answer_length} words\nYour answer: {answer_length} words"
61
+
62
+ self.answers_given.append({
63
+ 'question': self.current_question['Questions'],
64
+ 'your_answer': user_answer,
65
+ 'similarity': similarity
66
+ })
67
+
68
+ return feedback
69
+ except Exception as e:
70
+ return f"Error evaluating answer: {str(e)}"
71
 
72
  def get_performance_analysis(self):
73
  if self.total_questions == 0:
74
  return "No questions answered yet!"
75
 
76
+ try:
77
+ accuracy = (self.score / self.total_questions) * 100
78
+
79
+ analysis = f"""
80
+ PERFORMANCE ANALYSIS
81
+ {'='*50}
82
+ Total Questions Attempted: {self.total_questions}
83
+ Correct Answers: {self.score}
84
+ Accuracy Rate: {accuracy:.1f}%
85
+
86
+ STRENGTHS:
87
+ - Successfully completed {self.total_questions} interview questions
88
+ - Current accuracy rate: {accuracy:.1f}%
89
+
90
+ AREAS FOR IMPROVEMENT:
91
+ - Focus on providing detailed and structured answers
92
+ - Practice technical concepts and definitions
93
+ - Improve answer clarity and completeness
94
+
95
+ RECOMMENDATIONS:
96
+ 1. Review the expected answers for failed questions
97
+ 2. Practice similar questions in weak areas
98
+ 3. Work on concise but comprehensive responses
99
+ 4. Practice explaining complex concepts clearly
100
+ """
101
+ return analysis
102
+ except Exception as e:
103
+ return f"Error generating analysis: {str(e)}"
104
 
105
  bot = InterviewBot()
106
 
107
  def start_interview(difficulty):
108
+ try:
109
+ question, language, level = bot.get_next_question(difficulty)
110
+ return f"Question ({language} - {level}):\n{question}"
111
+ except Exception as e:
112
+ return f"Error: {str(e)}"
113
 
114
  def submit_answer(answer_text):
115
  if not answer_text.strip():
116
  return "Please provide an answer!"
117
+ return bot.evaluate_answer(answer_text)
 
118
 
119
  def show_analysis():
120
  return bot.get_performance_analysis()
121
 
122
  with gr.Blocks(title="AI Job Interview Bot - Computer Science") as demo:
123
  gr.Markdown("""
124
+ # AI Job Interview Bot for Computer Science
125
+
126
+ Practice technical interview questions with AI-powered feedback!
127
+
128
+ **Features:**
129
+ - 496+ real interview questions
130
+ - Multiple difficulty levels (Easy, Medium, Hard)
131
+ - Real-time AI feedback on your answers
132
+ - Performance analysis and recommendations
133
  """)
134
 
135
  with gr.Row():
 
147
  question_display = gr.Textbox(
148
  label="Question",
149
  interactive=False,
150
+ lines=5
151
  )
152
 
153
  with gr.Row():
 
170
 
171
  with gr.Row():
172
  analysis_btn = gr.Button("Show Performance Analysis", variant="secondary")
173
+
174
+ analysis_display = gr.Textbox(
175
+ label="Performance Analysis",
176
+ interactive=False,
177
+ lines=15
178
+ )
179
 
180
  start_btn.click(
181
  fn=start_interview,