Samay42 commited on
Commit
d8f3b50
·
verified ·
1 Parent(s): 7a445ac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -0
app.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import fitz
3
+ import google.generativeai as genai
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+ load_dotenv()
8
+ # Initialize the Google Gemini model
9
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
10
+ model = genai.GenerativeModel('gemini-1.5-flash')
11
+
12
+ def extract_text_from_pdf(file):
13
+ """Extract text from PDF."""
14
+ text = ""
15
+ doc = fitz.open(stream=file.read(), filetype="pdf")
16
+ for page in doc:
17
+ text += page.get_text()
18
+ return text
19
+
20
+ def get_gemini_response(prompt):
21
+ """Function to load Google Gemini model and provide queries as response."""
22
+ response = model.generate_content([prompt])
23
+ return response.text
24
+
25
+ def analyze_resume(text):
26
+ prompt = (
27
+ "You are an expert resume analyst. Analyze the following resume text and provide the following:\n\n"
28
+ "1. A brief summary of the resume, including the candidate's main interests and the fields they are most passionate about.\n"
29
+ "2. A detailed percentage distribution of fields/domains present in the resume, with keywords extracted from the resume. Ensure the total sums up to 100%.\n"
30
+ "Here is an example of how the output should look like:\n\n"
31
+ "### Summary\n"
32
+ "The resume indicates a strong background and interest in machine learning, data science, and software development. The candidate has worked on several projects involving machine learning algorithms, data preprocessing, and building software applications. They have demonstrated proficiency in Python, Java, and various machine learning frameworks. The candidate is passionate about solving complex problems using AI and has a keen interest in continuing to develop their skills in this area.\n\n"
33
+ "### Percentage Distribution of Fields/Domains\n"
34
+ "Note: only include standard technologies as keywords.\n"
35
+ "- *Machine Learning (ML)*: 40%\n"
36
+ " - Keywords: Algorithms, Keras, PyTorch, Scikit-Learn, Predictive Models\n"
37
+ "- *Data Science (DS)*: 30%\n"
38
+ " - Keywords: Data Analysis, Pandas, NumPy, Visualization, Statistical Methods\n"
39
+ "- *Software Development (SD)*: 30%\n"
40
+ " - Keywords: Python, Java, Software Engineering, APIs, Git\n"
41
+ )
42
+ analysis = get_gemini_response(prompt + text)
43
+ return analysis
44
+
45
+ def extract_domains_from_analysis(analysis):
46
+ """Extracts domain names from the analysis text."""
47
+ domain_prompt = (
48
+ "Based on the following resume analysis, extract and return the standard domain/field/keywords names present in the resume. "
49
+ "Provide only the domain names separated by commas:\n\n"
50
+ f"{analysis}"
51
+ )
52
+ domain_response = get_gemini_response(domain_prompt)
53
+ return domain_response.strip()
54
+
55
+ def generate_mcq_questions(analysis, selected_domain):
56
+ num_questions = 20 # Default to 20 questions
57
+ prompt_template = (
58
+ f"*Subject:* {selected_domain}\n\n"
59
+ f"*Bloom Taxonomy Levels:*\n\n"
60
+ f"- *Analysis:* Identify the relationships between concepts, analyze data, identify patterns and causes, and draw conclusions.\n"
61
+ f"- *Apply:* Use learned concepts to solve problems, complete tasks, and apply principles to new situations.\n"
62
+ f"- *Evaluate:* Assess the value or quality of something, make judgments, and justify decisions.\n\n"
63
+ f"*MCQ Prompt:*\n\n"
64
+ f"Generate {num_questions} code snippet types of multiple-choice questions (MCQs) for the subject of {selected_domain} aligned with the following Bloom Taxonomy levels:\n\n"
65
+ f"- 8 questions at the *Analysis* level:\n"
66
+ f"- 6 questions at the *Apply* level:\n"
67
+ f"- 6 questions at the *Evaluate* level:\n\n"
68
+ f"*Format:*\n\n"
69
+ f"- Each question should have 4 options (A, B, C, D)\n"
70
+ f"- Each question should have a clear and concise stem\n"
71
+ f"- Each option should be plausible, but only one should be correct\n\n"
72
+ f"Example:\n\n"
73
+ f"1. What is the time complexity of sorting an array using the merge sort algorithm? (Multiple-choice)\n"
74
+ f"a. O(n)\n"
75
+ f"b. O(log n)\n"
76
+ f"c. O(n log n)\n"
77
+ f"d. O(n^2)\n\n"
78
+ f"Answers:\n\n"
79
+ f"1. c\n\n"
80
+ f"Note: Ensure there are exactly {num_questions} questions in total with a random mix of question types."
81
+ )
82
+ questions = get_gemini_response(prompt_template)
83
+ return questions
84
+
85
+ def generate_coding_questions(analysis, selected_domain):
86
+ num_questions = 20 # Default to 20 questions
87
+ prompt_template = (
88
+ f"You are an expert in the {selected_domain} field. Based on the analysis of the candidate's resume, which shows {analysis}, "
89
+ f"generate {num_questions} coding challenges that reflect fundamental to medium-level real-world scenarios and problems encountered in {selected_domain}. "
90
+ f"The coding challenges should:\n\n"
91
+ f"1. Be relevant to what a recruiter might ask a final-year student.\n"
92
+ f"2. Focus on practical coding tasks.\n"
93
+ f"3. Include identifying errors, completing code snippets, and writing simple to moderate algorithms.\n"
94
+ f"4. Cover a range of difficulty levels from fundamental concepts to medium topics.\n"
95
+ f"5. Be clear, concise, and directly related to the skills highlighted in the resume.\n"
96
+ f"6. Include examples of typical coding questions.\n\n"
97
+ f"Example:\n\n"
98
+ f"1. Write a function to reverse a string. (Coding challenge)\n"
99
+ f"```python\n"
100
+ f"def reverse_string(s):\n"
101
+ f" return s[::-1]\n"
102
+ f"```\n\n"
103
+ f"2. Identify the error in the following code snippet and correct it. (Open-ended)\n"
104
+ f"```python\n"
105
+ f"def sum_of_squares(n):\n"
106
+ f" total = 0\n"
107
+ f" for i in range(n):\n"
108
+ f" total += i**2\n"
109
+ f" return total\n"
110
+ f"```\n"
111
+ f"Error: The range should be `range(n+1)` to include `n`.\n\n"
112
+ f"3. Complete the following function to check if a number is prime. (Fill-in-the-blank)\n"
113
+ f"```python\n"
114
+ f"def is_prime(n):\n"
115
+ f" if n <= 1:\n"
116
+ f" return False\n"
117
+ f" for i in range(2, n):\n"
118
+ f" if n % i == 0:\n"
119
+ f" return False\n"
120
+ f" return True\n"
121
+ f"```\n\n"
122
+ f"Ensure there are exactly {num_questions} coding questions with a mix of the above types."
123
+ )
124
+ questions = get_gemini_response(prompt_template)
125
+ return questions
126
+
127
+ def generate_interview_questions(analysis, selected_domain):
128
+ num_questions = 20 # Default to 20 questions
129
+ prompt_template = (
130
+ f"Based on the candidate's resume and the identified skills, experience, and education, generate a set of {num_questions} interview questions "
131
+ f"that assess their fit for the position at our company. The questions should cover topics such as problem-solving abilities, leadership skills, "
132
+ f"communication skills, cultural fit, etc. Additionally, include follow-up questions to probe deeper into the candidate's responses and evaluate their thought process. "
133
+ f"The questions should be specific to the field of {selected_domain}."
134
+ )
135
+ questions = get_gemini_response(prompt_template)
136
+ return questions
137
+
138
+ def split_questions_answers(quiz_response):
139
+ """Function that splits the questions and answers from the quiz response."""
140
+ if "Answers:" in quiz_response:
141
+ questions = quiz_response.split("Answers:")[0]
142
+ answers = quiz_response.split("Answers:")[1]
143
+ else:
144
+ questions = quiz_response
145
+ answers = "Answers section not found in the response."
146
+ return questions, answers
147
+
148
+ def main():
149
+ st.title("PERSONAL PLACEMENT ASSISTANT")
150
+ st.subheader("Your personal expert for placement")
151
+
152
+ if 'selected_domain' not in st.session_state:
153
+ st.session_state.selected_domain = "DSA"
154
+
155
+ uploaded_file = st.file_uploader("Upload your resume PDF", type="pdf")
156
+ if uploaded_file is not None:
157
+ resume_text = extract_text_from_pdf(uploaded_file)
158
+
159
+ st.write("Analyzing Resume...")
160
+ analysis = analyze_resume(resume_text)
161
+
162
+ st.write("Analysis Result:")
163
+ st.write(analysis)
164
+
165
+ # Extract domains from the analysis
166
+ domain_response = extract_domains_from_analysis(analysis)
167
+ domains = [domain.strip() for domain in domain_response.split(',')]
168
+ default_domains = ["DSA", "DBMS", "Programming Basics"] + domains
169
+
170
+ def update_selected_domain():
171
+ st.session_state.selected_domain = st.session_state.domain_select
172
+
173
+ st.selectbox(
174
+ "Select Domain for Questions:",
175
+ default_domains,
176
+ key="domain_select",
177
+ index=default_domains.index(st.session_state.selected_domain) if st.session_state.selected_domain in default_domains else 0,
178
+ on_change=update_selected_domain
179
+ )
180
+
181
+ question_type = st.selectbox("Select Question Type:", ["Technical Round", "Interview Round"])
182
+
183
+ if question_type == "Technical Round":
184
+ technical_subtype = st.selectbox("Select Technical Round Type:", ["MCQs", "Coding Challenges"])
185
+
186
+ if st.button("Generate Questions"):
187
+ if question_type == "Technical Round":
188
+ if technical_subtype == "MCQs":
189
+ quiz_response = generate_mcq_questions(analysis, st.session_state.selected_domain)
190
+ else:
191
+ quiz_response = generate_coding_questions(analysis, st.session_state.selected_domain)
192
+ else:
193
+ quiz_response = generate_interview_questions(analysis, st.session_state.selected_domain)
194
+
195
+ questions, answers = split_questions_answers(quiz_response)
196
+
197
+ st.write("Generated Questions:")
198
+ st.write(questions)
199
+
200
+ if st.button("Show Answers"):
201
+ st.write("Answers:")
202
+ st.write(answers)
203
+
204
+ if __name__ == "__main__":
205
+ main()