maahikachitagi commited on
Commit
f737e92
Β·
verified Β·
1 Parent(s): 000de83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -73
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import re
 
4
 
5
  # ---- Load and parse questions from knowledge.txt ----
6
  def load_questions(file_path):
@@ -8,7 +9,6 @@ def load_questions(file_path):
8
  data = f.read()
9
 
10
  question_blocks = re.split(r'Question:\s*', data)[1:]
11
-
12
  questions = []
13
  for block in question_blocks:
14
  parts = block.split('Possible Answers:')
@@ -20,94 +20,63 @@ def load_questions(file_path):
20
 
21
  all_questions = load_questions('knowledge.txt')
22
 
23
- # ---- Simple way to assign questions to interview types ----
24
- # You can replace this later with better tagging
25
  questions_by_type = {
26
  'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
27
- 'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',
28
- 'stack', 'queue', 'recursion', 'reverse', 'bfs', 'dfs', 'time complexity', 'binary search tree',
29
- 'web application', 'chat system', 'load balancing', 'caching', 'normalization', 'acid', 'indexing',
30
- 'sql injection', 'https', 'xss', 'hash', 'vulnerabilities'])],
31
-
32
  'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
33
- "Debugging",
34
- "Learning Fast",
35
- "Deadlines",
36
- "Teamwork",
37
- "Leadership",
38
- "Mistake Recovery",
39
- "Conflict Management",
40
- "Decision Making"])],
41
-
42
  'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
43
- "A/B Testing",
44
- "Financial Modeling",
45
- "Automation",
46
- "Data Analysis",
47
- "Regression",
48
- "Business Opportunity",
49
- "Stakeholder Alignment"])]
50
  }
51
 
52
-
53
- # ---- Hugging Face Client ----
54
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
55
 
56
- # ---- Interview type selection ----
57
  def set_type(choice, user_profile):
58
  user_profile["interview_type"] = choice
59
  return "Great! What’s your background and what field/role are you aiming for?", user_profile
60
 
61
- # ---- Save background ----
62
  def save_background(info, user_profile):
63
  user_profile["field"] = info
64
  return "Awesome! Type 'start' below to begin your interview.", user_profile
65
 
66
- # ---- Main respond logic ----
67
  def respond(message, chat_history, user_profile):
 
 
68
  if not user_profile.get("interview_type") or not user_profile.get("field"):
69
  bot_msg = "Please finish steps 1 and 2 before starting the interview."
70
  chat_history.append((message, bot_msg))
71
  return chat_history
72
 
73
  # Start interview logic
74
- if message.strip().lower() == 'start':
75
  interview_type = user_profile['interview_type']
76
  selected_questions = questions_by_type.get(interview_type, [])
 
 
 
77
  user_profile['questions'] = selected_questions
78
  user_profile['current_q'] = 0
79
  user_profile['user_answers'] = []
80
- if not selected_questions:
81
- bot_msg = "No questions available for this interview type."
82
- else:
83
- bot_msg = f"First question: {selected_questions[0]['question']}"
 
 
 
 
 
 
 
84
  chat_history.append((message, bot_msg))
85
  return chat_history
86
 
87
- # If interview is ongoing
88
- # if user_profile.get("questions"):
89
- # q_index = user_profile['current_q']
90
- # user_profile['user_answers'].append(message)
91
-
92
- # q_index += 1
93
- # user_profile['current_q'] = q_index
94
-
95
- # if q_index < len(user_profile['questions']):
96
- # bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}"
97
- # else:
98
- # bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers."
99
- # chat_history.append((message, bot_msg))
100
- # return chat_history
101
- if user_profile.get("questions"):
102
-
103
- # --- NEW STOP LOGIC ---
104
- if message.strip().lower() == 'stop':
105
- bot_msg = "Thank you for chatting with Intervu! The interview has been stopped. Type 'feedback' if you'd like me to analyze your answers."
106
- chat_history.append((message, bot_msg))
107
- user_profile['questions'] = [] # clear questions list to stop
108
- return chat_history
109
-
110
- # Existing interview logic continues here:
111
  q_index = user_profile['current_q']
112
  user_profile['user_answers'].append(message)
113
 
@@ -117,22 +86,18 @@ def respond(message, chat_history, user_profile):
117
  if q_index < len(user_profile['questions']):
118
  bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}"
119
  else:
120
- bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers."
121
-
122
  chat_history.append((message, bot_msg))
123
  return chat_history
124
-
125
 
126
- # Handle feedback request
127
- if message.strip().lower() == 'feedback':
128
  feedback = generate_feedback(user_profile)
129
  chat_history.append((message, feedback))
130
  return chat_history
131
 
132
- # Default fallback
133
- messages = [
134
- {"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in the {user_profile['field']} field."}
135
- ]
136
  for q, a in chat_history:
137
  messages.append({"role": "user", "content": q})
138
  messages.append({"role": "assistant", "content": a})
@@ -143,25 +108,23 @@ def respond(message, chat_history, user_profile):
143
  chat_history.append((message, bot_msg))
144
  return chat_history
145
 
146
- # ---- Simple feedback function (keyword based for now) ----
147
  def generate_feedback(user_profile):
148
  feedback = []
149
  questions = user_profile.get('questions', [])
150
  answers = user_profile.get('user_answers', [])
151
-
152
  for i, user_ans in enumerate(answers):
153
  correct_answers = questions[i]['answers']
154
  match = any(ans.lower() in user_ans.lower() for ans in correct_answers)
155
  if match:
156
- fb = f"Question {i+1}: βœ… Good job! You covered key points."
157
  else:
158
- fb = f"Question {i+1}: ❌ You missed some key points: {correct_answers[0]}"
159
  feedback.append(fb)
160
  return "\n".join(feedback)
161
 
162
- # ---- Gradio Interface ----
163
  with gr.Blocks() as demo:
164
- user_profile = gr.State({"interview_type": "", "field": ""})
165
  chat_history = gr.State([])
166
 
167
  gr.Markdown("# 🎀 Welcome to Intervu")
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import re
4
+ import random
5
 
6
  # ---- Load and parse questions from knowledge.txt ----
7
  def load_questions(file_path):
 
9
  data = f.read()
10
 
11
  question_blocks = re.split(r'Question:\s*', data)[1:]
 
12
  questions = []
13
  for block in question_blocks:
14
  parts = block.split('Possible Answers:')
 
20
 
21
  all_questions = load_questions('knowledge.txt')
22
 
23
+ # ---- Tagging ----
 
24
  questions_by_type = {
25
  'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
26
+ 'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',
27
+ 'stack', 'queue', 'recursion', 'reverse', 'bfs', 'dfs', 'time complexity', 'binary search tree',
28
+ 'web application', 'chat system', 'load balancing', 'caching', 'normalization', 'acid', 'indexing',
29
+ 'sql injection', 'https', 'xss', 'hash', 'vulnerabilities'])],
 
30
  'Competency-Based Interview': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
31
+ "debugging", "learning", "deadlines", "teamwork", "leadership", "mistake", "conflict", "decision"])],
 
 
 
 
 
 
 
 
32
  'Case': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
33
+ "testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]
 
 
 
 
 
 
34
  }
35
 
 
 
36
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
37
 
38
+ # ---- Logic functions ----
39
  def set_type(choice, user_profile):
40
  user_profile["interview_type"] = choice
41
  return "Great! What’s your background and what field/role are you aiming for?", user_profile
42
 
 
43
  def save_background(info, user_profile):
44
  user_profile["field"] = info
45
  return "Awesome! Type 'start' below to begin your interview.", user_profile
46
 
 
47
  def respond(message, chat_history, user_profile):
48
+ message_lower = message.strip().lower()
49
+
50
  if not user_profile.get("interview_type") or not user_profile.get("field"):
51
  bot_msg = "Please finish steps 1 and 2 before starting the interview."
52
  chat_history.append((message, bot_msg))
53
  return chat_history
54
 
55
  # Start interview logic
56
+ if message_lower == 'start':
57
  interview_type = user_profile['interview_type']
58
  selected_questions = questions_by_type.get(interview_type, [])
59
+ random.shuffle(selected_questions)
60
+ selected_questions = selected_questions[:10]
61
+
62
  user_profile['questions'] = selected_questions
63
  user_profile['current_q'] = 0
64
  user_profile['user_answers'] = []
65
+ user_profile['interview_in_progress'] = True
66
+
67
+ intro = f"Welcome to your {interview_type} interview for a {user_profile['field']} position. I will ask you up to 10 questions. Type 'stop' anytime to end."
68
+ first_q = f"First question: {selected_questions[0]['question']}"
69
+ chat_history.append((message, intro))
70
+ chat_history.append(("", first_q))
71
+ return chat_history
72
+
73
+ if message_lower == 'stop' and user_profile.get("interview_in_progress"):
74
+ user_profile['interview_in_progress'] = False
75
+ bot_msg = "Interview stopped. Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!"
76
  chat_history.append((message, bot_msg))
77
  return chat_history
78
 
79
+ if user_profile.get("interview_in_progress"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  q_index = user_profile['current_q']
81
  user_profile['user_answers'].append(message)
82
 
 
86
  if q_index < len(user_profile['questions']):
87
  bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}"
88
  else:
89
+ user_profile['interview_in_progress'] = False
90
+ bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers. Thanks for interviewing with Intervu!"
91
  chat_history.append((message, bot_msg))
92
  return chat_history
 
93
 
94
+ if message_lower == 'feedback':
 
95
  feedback = generate_feedback(user_profile)
96
  chat_history.append((message, feedback))
97
  return chat_history
98
 
99
+ # fallback small talk
100
+ messages = [{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."}]
 
 
101
  for q, a in chat_history:
102
  messages.append({"role": "user", "content": q})
103
  messages.append({"role": "assistant", "content": a})
 
108
  chat_history.append((message, bot_msg))
109
  return chat_history
110
 
 
111
  def generate_feedback(user_profile):
112
  feedback = []
113
  questions = user_profile.get('questions', [])
114
  answers = user_profile.get('user_answers', [])
 
115
  for i, user_ans in enumerate(answers):
116
  correct_answers = questions[i]['answers']
117
  match = any(ans.lower() in user_ans.lower() for ans in correct_answers)
118
  if match:
119
+ fb = f"Question {i+1}: βœ… Good job!"
120
  else:
121
+ fb = f"Question {i+1}: ❌ Missed some key points: {correct_answers[0]}"
122
  feedback.append(fb)
123
  return "\n".join(feedback)
124
 
125
+ # ---- Gradio UI ----
126
  with gr.Blocks() as demo:
127
+ user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})
128
  chat_history = gr.State([])
129
 
130
  gr.Markdown("# 🎀 Welcome to Intervu")