Update app.py
Browse files
app.py
CHANGED
|
@@ -1,137 +1,140 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import PyPDF2
|
| 3 |
-
import os
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
-
import tensorflow as tf
|
| 6 |
-
from transformers import BertTokenizer, TFBertModel
|
| 7 |
-
import numpy as np
|
| 8 |
-
import math
|
| 9 |
-
import speech_recognition as sr
|
| 10 |
-
import gtts
|
| 11 |
-
from streamlit.components.v1 import html
|
| 12 |
-
import time
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
from dotenv import load_dotenv
|
| 16 |
-
load_dotenv()
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
st.
|
| 103 |
-
return
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# st.write(
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import PyPDF2
|
| 3 |
+
import os
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from transformers import BertTokenizer, TFBertModel
|
| 7 |
+
import numpy as np
|
| 8 |
+
import math
|
| 9 |
+
import speech_recognition as sr
|
| 10 |
+
import gtts
|
| 11 |
+
from streamlit.components.v1 import html
|
| 12 |
+
import time
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
+
os.system('pip install --upgrade pip')
|
| 19 |
+
|
| 20 |
+
# no wide mode
|
| 21 |
+
st.set_page_config(page_title="Streamlit App", page_icon=":shark:", layout="centered", initial_sidebar_state="auto")
|
| 22 |
+
|
| 23 |
+
st.title("Mock Interview")
|
| 24 |
+
|
| 25 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 26 |
+
text_model= genai.GenerativeModel("gemini-pro")
|
| 27 |
+
|
| 28 |
+
st.write("Welcome to the mock interview app. This app will help you prepare for your next interview. You can practice your responses to common interview questions and receive feedback on your responses.")
|
| 29 |
+
|
| 30 |
+
def getallinfo(data):
|
| 31 |
+
text = f"{data} is not properly formatted for this model. Please try again and format the whole in a single paragraph covering all the information."
|
| 32 |
+
response = text_model.generate_content(text)
|
| 33 |
+
response.resolve()
|
| 34 |
+
return response.text
|
| 35 |
+
|
| 36 |
+
def file_processing(uploaded_file):
|
| 37 |
+
# upload pdf of resume
|
| 38 |
+
reader = PyPDF2.PdfReader(uploaded_file)
|
| 39 |
+
text = ""
|
| 40 |
+
for page in reader.pages:
|
| 41 |
+
text += page.extract_text()
|
| 42 |
+
return text
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Load the pre-trained BERT model
|
| 46 |
+
model = TFBertModel.from_pretrained("bert-base-uncased")
|
| 47 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 48 |
+
|
| 49 |
+
# Function to preprocess text and get embeddings
|
| 50 |
+
def get_embedding(text):
|
| 51 |
+
encoded_text = tokenizer(text, return_tensors="tf")
|
| 52 |
+
output = model(encoded_text)
|
| 53 |
+
embedding = output.last_hidden_state[:, 0, :]
|
| 54 |
+
return embedding
|
| 55 |
+
|
| 56 |
+
# Function to generate feedback (replace with your logic)
|
| 57 |
+
def generate_feedback(question, answer):
|
| 58 |
+
# Ensure correct variable name (case-sensitive)
|
| 59 |
+
question_embedding = get_embedding(question)
|
| 60 |
+
answer_embedding = get_embedding(answer)
|
| 61 |
+
|
| 62 |
+
# Enable NumPy-like behavior for transpose
|
| 63 |
+
tf.experimental.numpy.experimental_enable_numpy_behavior()
|
| 64 |
+
|
| 65 |
+
# Calculate similarity score (cosine similarity)
|
| 66 |
+
similarity_score = np.dot(question_embedding, answer_embedding.T) / (np.linalg.norm(question_embedding) * np.linalg.norm(answer_embedding))
|
| 67 |
+
|
| 68 |
+
# Generate basic feedback based on similarity score
|
| 69 |
+
corrected_string = f"Feedback: {np.array2string(similarity_score, precision=2)}"
|
| 70 |
+
# print(corrected_string)
|
| 71 |
+
return np.array2string(similarity_score, precision=2)
|
| 72 |
+
|
| 73 |
+
def generate_questions(roles, data):
|
| 74 |
+
questions = []
|
| 75 |
+
text = f"If this is not a resume then return text uploaded pdf is not a resume. this is a resume overview of the candidate. The candidate details are in {data}. The candidate has applied for the role of {roles}. Generate questions for the candidate based on the role applied and on the Resume of the candidate. Not always necceassary to ask only technical questions related to the role. Ask some personal questions too. Ask no additional questions. Dont categorize the questions. No of questions should range from 1-3 questions only. Ask one question at a time only."
|
| 76 |
+
response = text_model.generate_content(text)
|
| 77 |
+
response.resolve()
|
| 78 |
+
# slipt the response into questions either by \n or by ? or by . or by !
|
| 79 |
+
questions = response.text.split("\n")
|
| 80 |
+
|
| 81 |
+
return questions
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def generate_overall_feedback(data, percent, answer, questions):
|
| 85 |
+
percent = float(percent)
|
| 86 |
+
if percent > 0.5:
|
| 87 |
+
test = f"Here is the overview of the candidate {data}. In the interview the questions asked were {questions}. The candidate has answered the questions as follows: {answer}. Based on the answers provided, the candidate has scored {percent}. The candidate has done well in the interview. The candidate has answered the questions well and has a good understanding of the concepts. The candidate has scored well in the interview. The candidate has scored {percent} in the interview. The candidate has done well in the interview. The candidate has answered the questions well and has a good understanding of the concepts. The candidate has scored well in the interview. The candidate has scored {percent} in the interview."
|
| 88 |
+
else:
|
| 89 |
+
test = f"Here is the overview of the candidate {data}. In the interview the questions asked were {questions}. The candidate has answered the questions as follows: {answer}. Based on the answers provided, the candidate has scored {percent}. tell the average percent and rate the interview out of 10. Give the feedback to the candidate about the interview and areas of improvements. While talking to candidate always take their name. give the candidate various ways to improve their interview skills. The candidate needs to know about where they are going wrong and the solution to the issues they are having during the interview."
|
| 90 |
+
# st.write(test)
|
| 91 |
+
response = text_model.generate_content(test)
|
| 92 |
+
response.resolve()
|
| 93 |
+
return response.text
|
| 94 |
+
|
| 95 |
+
def store_audio_text():
|
| 96 |
+
r = sr.Recognizer()
|
| 97 |
+
with sr.Microphone() as source:
|
| 98 |
+
st.error("Speak now")
|
| 99 |
+
audio = r.listen(source)
|
| 100 |
+
try:
|
| 101 |
+
text = r.recognize_google(audio)
|
| 102 |
+
# st.success(f"Your Answer: {text}")
|
| 103 |
+
return text
|
| 104 |
+
except:
|
| 105 |
+
st.error("Sorry could not recognize your voice")
|
| 106 |
+
return " "
|
| 107 |
+
|
| 108 |
+
uploaded_file = st.file_uploader("Upload your resume in simple Document Format", type=["pdf"])
|
| 109 |
+
roles_applied = []
|
| 110 |
+
if uploaded_file is not None:
|
| 111 |
+
st.write("File uploaded successfully!")
|
| 112 |
+
data = file_processing(uploaded_file)
|
| 113 |
+
# st.write(data)
|
| 114 |
+
# st.write(getallinfo(data))
|
| 115 |
+
updated_data = getallinfo(data)
|
| 116 |
+
# st.write(updated_data)
|
| 117 |
+
roles = st.multiselect("Select your job role:", ["Data Scientist", "Software Engineer", "Product Manager", "Data Analyst", "Business Analyst"])
|
| 118 |
+
if roles:
|
| 119 |
+
roles_applied.append(roles)
|
| 120 |
+
st.write(f"Selected roles: {roles}")
|
| 121 |
+
questions = generate_questions(roles, updated_data)
|
| 122 |
+
feedback = []
|
| 123 |
+
answers = []
|
| 124 |
+
ans = ""
|
| 125 |
+
interaction = {}
|
| 126 |
+
for i in range(len(questions)):
|
| 127 |
+
st.write(questions[i])
|
| 128 |
+
ans = store_audio_text()
|
| 129 |
+
st.success(ans)
|
| 130 |
+
answers.append(ans)
|
| 131 |
+
percent = 0.0
|
| 132 |
+
percent = generate_feedback(questions[i], answers[i])
|
| 133 |
+
print(percent)
|
| 134 |
+
feedback.append(generate_overall_feedback(data, percent, answers[i], questions[i]))
|
| 135 |
+
interaction[questions[i]] = answers[i]
|
| 136 |
+
if st.button("Submit"):
|
| 137 |
+
for i in range(len(questions)):
|
| 138 |
+
st.write(interaction[questions[i]])
|
| 139 |
+
st.write(feedback[i])
|
| 140 |
+
# st.write("Thank you for your responses!")
|