maahikachitagi commited on
Commit
45a190a
Β·
verified Β·
1 Parent(s): 606735f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -46
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # imports
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
  import re
@@ -10,41 +10,28 @@ import torchvision.transforms as transforms
10
  import torchvision.models as models
11
  from PIL import Image
12
 
 
13
 
14
- # Load ResNet18 model
 
 
 
 
 
 
15
  model = models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1)
16
- model.fc = torch.nn.Linear(model.fc.in_features, 2) # Adjust for two classes
17
  model.eval()
18
 
19
- # Define image transformation
20
  transform = transforms.Compose([
21
  transforms.Resize((224, 224)),
22
  transforms.ToTensor()
23
  ])
24
 
25
- # Function to classify posture images
26
- def classify_image(image):
27
- if image is None:
28
- return "No image provided! Please upload or capture an image."
29
-
30
- image = transform(image).unsqueeze(0)
31
- output = model(image)
32
- _, predicted = torch.max(output, 1)
33
- return (
34
- "Good Posture! Sit exactly like that for your Interview!"
35
- if predicted.item() == 0
36
- else "Bad Posture, you should think of sitting a little straighter or more in frame for your real interview."
37
- )
38
-
39
- # Set up Gradio interface
40
- iface = gr.Interface(fn=classify_image, inputs=gr.Image(type="pil"), outputs="text")
41
- iface.launch()
42
-
43
- # uploading and cleaning the knowledge txt file
44
  def load_questions(file_path):
45
  with open(file_path, 'r') as f:
46
  data = f.read()
47
-
48
  question_blocks = re.split(r'Question:\s*', data)[1:]
49
  questions = []
50
  for block in question_blocks:
@@ -57,7 +44,6 @@ def load_questions(file_path):
57
 
58
  all_questions = load_questions('knowledge.txt')
59
 
60
- # creating the questions based on each interview
61
  questions_by_type = {
62
  'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
63
  'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',
@@ -70,14 +56,9 @@ questions_by_type = {
70
  "testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]
71
  }
72
 
73
- # models
74
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
75
- whisper_model = whisper.load_model("base")
76
-
77
- # whisper audio-to-text function
78
  def transcribe_audio(file_path):
79
  try:
80
- print(f"πŸ“‚ Processing audio: {file_path}")
81
  audio = AudioSegment.from_file(file_path)
82
  converted_path = "converted.wav"
83
  audio.export(converted_path, format="wav")
@@ -86,17 +67,29 @@ def transcribe_audio(file_path):
86
  except Exception as e:
87
  return f"❌ ERROR: {str(e)}"
88
 
89
- # setting up the users profile (step 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  def set_type(choice, user_profile):
91
  user_profile["interview_type"] = choice
92
  return "Great! What’s your background and what field/role are you aiming for?", user_profile
93
 
94
- # step 2
95
  def save_background(info, user_profile):
96
  user_profile["field"] = info
97
  return "Awesome! Type 'start' below to begin your interview.", user_profile
98
 
99
- # step 3
100
  def respond(message, chat_history, user_profile):
101
  message_lower = message.strip().lower()
102
 
@@ -105,7 +98,6 @@ def respond(message, chat_history, user_profile):
105
  chat_history.append((message, bot_msg))
106
  return chat_history
107
 
108
- # interview process
109
  if message_lower == 'start':
110
  interview_type = user_profile['interview_type']
111
  selected_questions = questions_by_type.get(interview_type, [])
@@ -149,7 +141,6 @@ def respond(message, chat_history, user_profile):
149
  chat_history.append((message, feedback))
150
  return chat_history
151
 
152
- # starting the chatbot
153
  messages = [{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."}]
154
  for q, a in chat_history:
155
  messages.append({"role": "user", "content": q})
@@ -171,11 +162,11 @@ def generate_feedback(user_profile):
171
  if match:
172
  fb = f"Question {i+1}: βœ… Good job!"
173
  else:
174
- fb = f"Question {i+1}: ❌ Missed some key points: {correct_answers[0]}"
175
  feedback.append(fb)
176
  return "\n".join(feedback)
177
 
178
- # handle audio input
179
  def handle_audio(audio_file, chat_history, user_profile):
180
  transcribed = transcribe_audio(audio_file)
181
  if transcribed.startswith("❌"):
@@ -183,12 +174,13 @@ def handle_audio(audio_file, chat_history, user_profile):
183
  return chat_history
184
  return respond(transcribed, chat_history, user_profile)
185
 
186
- # creating the visual elements
 
187
  with gr.Blocks() as demo:
188
  user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})
189
  chat_history = gr.State([])
190
 
191
- gr.Markdown("# Welcome to Intervu")
192
  gr.Image(value="images.JPEG", show_label=False, width=200)
193
 
194
  gr.Markdown("### Step 1: Choose Interview Type")
@@ -207,24 +199,26 @@ with gr.Blocks() as demo:
207
  background = gr.Textbox(label="Your background and field/goal")
208
  background_btn = gr.Button("Submit")
209
  background_output = gr.Textbox(label="Bot response", interactive=False)
210
-
211
  background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
212
 
213
  gr.Markdown("### Step 3: Start Interview")
214
  chatbot = gr.Chatbot(label="Interview Bot")
215
-
216
  with gr.Row():
217
  msg = gr.Textbox(label="Your message")
218
  audio_input = gr.Audio(type="filepath", label="πŸŽ™οΈ Upload or Record your answer")
219
-
220
  with gr.Row():
221
  send_btn = gr.Button("Send Text")
222
  audio_btn = gr.Button("Send Audio")
223
-
224
-
225
  send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
226
  send_btn.click(lambda: "", None, msg, queue=False)
227
  audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)
228
 
 
 
 
 
 
 
229
 
230
- demo.launch()
 
 
1
+ # --- IMPORTS ---
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
  import re
 
10
  import torchvision.models as models
11
  from PIL import Image
12
 
13
+ # --- LOAD MODELS ---
14
 
15
+ # HuggingFace Zephyr Model for Chat
16
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
17
+
18
+ # Whisper Model for Audio-to-Text
19
+ whisper_model = whisper.load_model("base")
20
+
21
+ # ResNet18 Model for Posture Classification
22
  model = models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1)
23
+ model.fc = torch.nn.Linear(model.fc.in_features, 2)
24
  model.eval()
25
 
 
26
  transform = transforms.Compose([
27
  transforms.Resize((224, 224)),
28
  transforms.ToTensor()
29
  ])
30
 
31
+ # --- LOAD QUESTIONS ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def load_questions(file_path):
33
  with open(file_path, 'r') as f:
34
  data = f.read()
 
35
  question_blocks = re.split(r'Question:\s*', data)[1:]
36
  questions = []
37
  for block in question_blocks:
 
44
 
45
  all_questions = load_questions('knowledge.txt')
46
 
 
47
  questions_by_type = {
48
  'Technical': [q for q in all_questions if any(keyword in q['question'].lower() for keyword in [
49
  'function', 'linked list', 'url', 'rest', 'graphql', 'garbage', 'cap theorem', 'sql', 'hash table',
 
56
  "testing", "financial", "automation", "analysis", "regression", "business", "stakeholder"])]
57
  }
58
 
59
+ # --- AUDIO TRANSCRIPTION ---
 
 
 
 
60
  def transcribe_audio(file_path):
61
  try:
 
62
  audio = AudioSegment.from_file(file_path)
63
  converted_path = "converted.wav"
64
  audio.export(converted_path, format="wav")
 
67
  except Exception as e:
68
  return f"❌ ERROR: {str(e)}"
69
 
70
+ # --- POSTURE CLASSIFICATION ---
71
+ def classify_image(image):
72
+ if image is None:
73
+ return "No image provided! Please upload or capture an image."
74
+ image = transform(image).unsqueeze(0)
75
+ output = model(image)
76
+ _, predicted = torch.max(output, 1)
77
+ return (
78
+ "βœ… Good Posture! Sit exactly like that for your Interview!"
79
+ if predicted.item() == 0
80
+ else "⚠️ Bad Posture β€” try sitting straighter or more centered for your real interview."
81
+ )
82
+
83
+ # --- INTERVIEW LOGIC ---
84
+
85
  def set_type(choice, user_profile):
86
  user_profile["interview_type"] = choice
87
  return "Great! What’s your background and what field/role are you aiming for?", user_profile
88
 
 
89
  def save_background(info, user_profile):
90
  user_profile["field"] = info
91
  return "Awesome! Type 'start' below to begin your interview.", user_profile
92
 
 
93
  def respond(message, chat_history, user_profile):
94
  message_lower = message.strip().lower()
95
 
 
98
  chat_history.append((message, bot_msg))
99
  return chat_history
100
 
 
101
  if message_lower == 'start':
102
  interview_type = user_profile['interview_type']
103
  selected_questions = questions_by_type.get(interview_type, [])
 
141
  chat_history.append((message, feedback))
142
  return chat_history
143
 
 
144
  messages = [{"role": "system", "content": f"You are a professional interviewer conducting a {user_profile['interview_type']} interview for a candidate in {user_profile['field']}."}]
145
  for q, a in chat_history:
146
  messages.append({"role": "user", "content": q})
 
162
  if match:
163
  fb = f"Question {i+1}: βœ… Good job!"
164
  else:
165
+ fb = f"Question {i+1}: ❌ Missed key points: {correct_answers[0]}"
166
  feedback.append(fb)
167
  return "\n".join(feedback)
168
 
169
+ # AUDIO HANDLING
170
  def handle_audio(audio_file, chat_history, user_profile):
171
  transcribed = transcribe_audio(audio_file)
172
  if transcribed.startswith("❌"):
 
174
  return chat_history
175
  return respond(transcribed, chat_history, user_profile)
176
 
177
+ # --- GRADIO INTERFACE ---
178
+
179
  with gr.Blocks() as demo:
180
  user_profile = gr.State({"interview_type": "", "field": "", "interview_in_progress": False})
181
  chat_history = gr.State([])
182
 
183
+ gr.Markdown("# 🎀 Welcome to Intervu")
184
  gr.Image(value="images.JPEG", show_label=False, width=200)
185
 
186
  gr.Markdown("### Step 1: Choose Interview Type")
 
199
  background = gr.Textbox(label="Your background and field/goal")
200
  background_btn = gr.Button("Submit")
201
  background_output = gr.Textbox(label="Bot response", interactive=False)
 
202
  background_btn.click(save_background, inputs=[background, user_profile], outputs=[background_output, user_profile])
203
 
204
  gr.Markdown("### Step 3: Start Interview")
205
  chatbot = gr.Chatbot(label="Interview Bot")
 
206
  with gr.Row():
207
  msg = gr.Textbox(label="Your message")
208
  audio_input = gr.Audio(type="filepath", label="πŸŽ™οΈ Upload or Record your answer")
 
209
  with gr.Row():
210
  send_btn = gr.Button("Send Text")
211
  audio_btn = gr.Button("Send Audio")
 
 
212
  send_btn.click(respond, inputs=[msg, chat_history, user_profile], outputs=[chatbot], queue=False)
213
  send_btn.click(lambda: "", None, msg, queue=False)
214
  audio_btn.click(handle_audio, inputs=[audio_input, chat_history, user_profile], outputs=[chatbot], queue=False)
215
 
216
+ # βœ… Step 4: Webcam Posture Check
217
+ gr.Markdown("### Step 4: Webcam Posture Check")
218
+ webcam = gr.Image(source="webcam", type="pil", label="Capture Posture")
219
+ posture_output = gr.Textbox(label="Posture Feedback")
220
+ posture_btn = gr.Button("Analyze Posture")
221
+ posture_btn.click(classify_image, inputs=[webcam], outputs=[posture_output])
222
 
223
+ # LAUNCH
224
+ demo.launch()