maahikachitagi commited on
Commit
6d2ca81
Β·
verified Β·
1 Parent(s): 84176a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -3
app.py CHANGED
@@ -1,22 +1,89 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
 
6
  def set_type(choice, user_profile):
7
  user_profile["interview_type"] = choice
8
  return "Great! What’s your background and what field/role are you aiming for?", user_profile
9
 
 
10
  def save_background(info, user_profile):
11
  user_profile["field"] = info
12
  return "Awesome! Type 'start' below to begin your interview.", user_profile
13
 
 
14
  def respond(message, chat_history, user_profile):
15
  if not user_profile.get("interview_type") or not user_profile.get("field"):
16
  bot_msg = "Please finish steps 1 and 2 before starting the interview."
17
  chat_history.append((message, bot_msg))
18
  return chat_history
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  messages = [
21
  {"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."}
22
  ]
@@ -30,13 +97,29 @@ def respond(message, chat_history, user_profile):
30
  chat_history.append((message, bot_msg))
31
  return chat_history
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  with gr.Blocks() as demo:
34
  user_profile = gr.State({"interview_type": "", "field": ""})
35
  chat_history = gr.State([])
36
 
37
  gr.Markdown("# 🎀 Welcome to Intervu")
38
 
39
- # Step 1
40
  gr.Markdown("### Step 1: Choose Interview Type")
41
  with gr.Row():
42
  with gr.Column():
@@ -49,7 +132,6 @@ with gr.Blocks() as demo:
49
  btn2.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
50
  btn3.click(set_type, inputs=[gr.Textbox(value="College", visible=False), user_profile], outputs=[type_output, user_profile])
51
 
52
- # Step 2
53
  gr.Markdown("### Step 2: Enter Your Background")
54
  background = gr.Textbox(label="Your background and field/goal")
55
  background_btn = gr.Button("Submit")
@@ -57,7 +139,6 @@ with gr.Blocks() as demo:
57
 
58
  background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
59
 
60
- # Step 3
61
  gr.Markdown("### Step 3: Start Interview")
62
  chatbot = gr.Chatbot(label="Interview Bot")
63
  msg = gr.Textbox(label="Your message")
@@ -67,3 +148,4 @@ with gr.Blocks() as demo:
67
  send_btn.click(lambda: "", None, msg, queue=False)
68
 
69
  demo.launch()
 
 
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):
7
+ with open(file_path, 'r') as f:
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:')
15
+ question_text = parts[0].strip()
16
+ answers_text = parts[1].strip()
17
+ possible_answers = [ans.strip() for ans in re.split(r'\d+\.\s+', answers_text) if ans.strip()]
18
+ questions.append({'question': question_text, 'answers': possible_answers})
19
+ return questions
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 ['function', 'algorithm', 'sql', 'system', 'java', 'programming', 'data', 'design', 'api', 'distributed', 'garbage', 'hash', 'stack', 'bfs', 'dfs'])],
27
+ 'Behavioral': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in ['tell me about a time', 'describe a situation', 'example of', 'negotiation', 'lead', 'mistake', 'stakeholder'])],
28
+ 'College': [] # For now keep empty unless you add questions for this
29
+ }
30
+
31
+ # ---- Hugging Face Client ----
32
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
33
 
34
+ # ---- Interview type selection ----
35
  def set_type(choice, user_profile):
36
  user_profile["interview_type"] = choice
37
  return "Great! What’s your background and what field/role are you aiming for?", user_profile
38
 
39
+ # ---- Save background ----
40
  def save_background(info, user_profile):
41
  user_profile["field"] = info
42
  return "Awesome! Type 'start' below to begin your interview.", user_profile
43
 
44
+ # ---- Main respond logic ----
45
  def respond(message, chat_history, user_profile):
46
  if not user_profile.get("interview_type") or not user_profile.get("field"):
47
  bot_msg = "Please finish steps 1 and 2 before starting the interview."
48
  chat_history.append((message, bot_msg))
49
  return chat_history
50
 
51
+ # Start interview logic
52
+ if message.strip().lower() == 'start':
53
+ interview_type = user_profile['interview_type']
54
+ selected_questions = questions_by_type.get(interview_type, [])
55
+ user_profile['questions'] = selected_questions
56
+ user_profile['current_q'] = 0
57
+ user_profile['user_answers'] = []
58
+ if not selected_questions:
59
+ bot_msg = "No questions available for this interview type."
60
+ else:
61
+ bot_msg = f"First question: {selected_questions[0]['question']}"
62
+ chat_history.append((message, bot_msg))
63
+ return chat_history
64
+
65
+ # If interview is ongoing
66
+ if user_profile.get("questions"):
67
+ q_index = user_profile['current_q']
68
+ user_profile['user_answers'].append(message)
69
+
70
+ q_index += 1
71
+ user_profile['current_q'] = q_index
72
+
73
+ if q_index < len(user_profile['questions']):
74
+ bot_msg = f"Next question: {user_profile['questions'][q_index]['question']}"
75
+ else:
76
+ bot_msg = "Interview complete! Type 'feedback' if you'd like me to analyze your answers."
77
+ chat_history.append((message, bot_msg))
78
+ return chat_history
79
+
80
+ # Handle feedback request
81
+ if message.strip().lower() == 'feedback':
82
+ feedback = generate_feedback(user_profile)
83
+ chat_history.append((message, feedback))
84
+ return chat_history
85
+
86
+ # Default fallback
87
  messages = [
88
  {"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."}
89
  ]
 
97
  chat_history.append((message, bot_msg))
98
  return chat_history
99
 
100
+ # ---- Simple feedback function (keyword based for now) ----
101
+ def generate_feedback(user_profile):
102
+ feedback = []
103
+ questions = user_profile.get('questions', [])
104
+ answers = user_profile.get('user_answers', [])
105
+
106
+ for i, user_ans in enumerate(answers):
107
+ correct_answers = questions[i]['answers']
108
+ match = any(ans.lower() in user_ans.lower() for ans in correct_answers)
109
+ if match:
110
+ fb = f"Question {i+1}: βœ… Good job! You covered key points."
111
+ else:
112
+ fb = f"Question {i+1}: ❌ You missed some key points: {correct_answers[0]}"
113
+ feedback.append(fb)
114
+ return "\n".join(feedback)
115
+
116
+ # ---- Gradio Interface ----
117
  with gr.Blocks() as demo:
118
  user_profile = gr.State({"interview_type": "", "field": ""})
119
  chat_history = gr.State([])
120
 
121
  gr.Markdown("# 🎀 Welcome to Intervu")
122
 
 
123
  gr.Markdown("### Step 1: Choose Interview Type")
124
  with gr.Row():
125
  with gr.Column():
 
132
  btn2.click(set_type, inputs=[gr.Textbox(value="Technical", visible=False), user_profile], outputs=[type_output, user_profile])
133
  btn3.click(set_type, inputs=[gr.Textbox(value="College", visible=False), user_profile], outputs=[type_output, user_profile])
134
 
 
135
  gr.Markdown("### Step 2: Enter Your Background")
136
  background = gr.Textbox(label="Your background and field/goal")
137
  background_btn = gr.Button("Submit")
 
139
 
140
  background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
141
 
 
142
  gr.Markdown("### Step 3: Start Interview")
143
  chatbot = gr.Chatbot(label="Interview Bot")
144
  msg = gr.Textbox(label="Your message")
 
148
  send_btn.click(lambda: "", None, msg, queue=False)
149
 
150
  demo.launch()
151
+