Upload app.py
Browse files
app.py
CHANGED
@@ -8,9 +8,6 @@ import os
|
|
8 |
from dotenv import load_dotenv
|
9 |
load_dotenv()
|
10 |
|
11 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
12 |
-
model_vision = genai.GenerativeModel('gemini-pro-vision')
|
13 |
-
model_text = genai.GenerativeModel("gemini-pro")
|
14 |
|
15 |
INTERMEDIATE_JSON_PATH = "intermediate_data.json"
|
16 |
INTERMEDIATE_JOB_DESC_PATH = "intermediate_job_desc.txt"
|
@@ -33,43 +30,41 @@ def load_prompt(filename):
|
|
33 |
except Exception as e:
|
34 |
return f"Error loading prompt: {e}"
|
35 |
|
36 |
-
def process_pdf_and_save_job_desc(pdf_file, job_description):
|
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 |
-
except Exception as e:
|
72 |
-
return None, f"An unexpected error occurred: {e}"
|
73 |
|
74 |
def display_json():
|
75 |
try:
|
@@ -98,30 +93,30 @@ def generate_content_based_on_json(example_functionality):
|
|
98 |
except Exception as e:
|
99 |
return f"An error occurred: {e}"
|
100 |
|
101 |
-
def generate_interview_questions():
|
102 |
-
# Assuming json_data is a string containing JSON data
|
103 |
-
# Here, you would customize the prompt to include specific details
|
104 |
-
# from the jsodef generate_interview_questions():
|
105 |
with open(INTERMEDIATE_JSON_PATH, "r") as json_file:
|
106 |
json_data = json.load(json_file)
|
107 |
|
108 |
-
combined_data = " ".join(json_data) # Combine with spaces(adjust as needed)
|
109 |
prompt = load_prompt("prompts/interview_questions_prompt.txt") + combined_data
|
110 |
-
responses = model_text.generate_content(prompt)
|
111 |
-
return responses.text
|
112 |
|
|
|
|
|
|
|
113 |
|
|
|
|
|
114 |
|
115 |
-
# Define the new Gradio interface for generating interview questions
|
116 |
interview_interface = gr.Interface(
|
117 |
fn=generate_interview_questions,
|
118 |
-
inputs=[],
|
119 |
outputs=gr.Textbox(label="Generated Interview Questions"),
|
120 |
title="Generate Interview Questions"
|
121 |
)
|
122 |
|
123 |
|
124 |
-
def generate_skill_gap_analysis():
|
125 |
try:
|
126 |
# Read the saved resume data (JSON)
|
127 |
with open(INTERMEDIATE_JSON_PATH, "r") as file:
|
@@ -131,6 +126,10 @@ def generate_skill_gap_analysis():
|
|
131 |
with open(INTERMEDIATE_JOB_DESC_PATH, "r") as file:
|
132 |
job_description = file.read()
|
133 |
|
|
|
|
|
|
|
|
|
134 |
# Construct a detailed prompt for the Gemini model
|
135 |
prompt = load_prompt("prompts/skills_gap_prompt.txt").replace(
|
136 |
"job_description", job_description).replace("json_data", json_data)
|
@@ -143,17 +142,21 @@ def generate_skill_gap_analysis():
|
|
143 |
except Exception as e:
|
144 |
return f"An error occurred: {e}"
|
145 |
|
146 |
-
|
147 |
skill_gap_analysis_interface = gr.Interface(
|
148 |
fn=generate_skill_gap_analysis,
|
149 |
-
inputs=[
|
150 |
outputs=gr.Textbox(label="Skill Gap Analysis"),
|
151 |
title="Skill Gap Analysis"
|
152 |
)
|
153 |
|
154 |
|
155 |
-
def generate_cover_letter():
|
156 |
try:
|
|
|
|
|
|
|
|
|
157 |
# Read the saved job description
|
158 |
with open(INTERMEDIATE_JOB_DESC_PATH, "r") as file:
|
159 |
job_description = file.read()
|
@@ -167,41 +170,37 @@ def generate_cover_letter():
|
|
167 |
"job_description", job_description).replace("json_data", json_data)
|
168 |
|
169 |
# Generate the cover letter using the model
|
170 |
-
response = model_text.generate_content(prompt
|
171 |
-
response.resolve()
|
172 |
|
173 |
return response.text
|
174 |
|
175 |
except Exception as e:
|
176 |
return f"An error occurred: {e}"
|
177 |
|
178 |
-
|
179 |
-
# Define the Gradio interface for generating a cover letter
|
180 |
cover_letter_interface = gr.Interface(
|
181 |
fn=generate_cover_letter,
|
182 |
-
inputs=[],
|
183 |
outputs=gr.Textbox(label="Generated Cover Letter"),
|
184 |
title="Cover Letter Generator"
|
185 |
)
|
186 |
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
images, _ = process_pdf_and_save_job_desc(pdf_content, job_description)
|
191 |
return images # Return the list of images to be displayed in the Gallery
|
192 |
|
193 |
-
# Define the updated interface for PDF processing
|
194 |
-
# Define the updated interface for PDF processing with better descriptions
|
195 |
pdf_interface = gr.Interface(
|
196 |
fn=gradio_pdf_interface,
|
197 |
inputs=[
|
198 |
gr.File(type="binary", label="Upload PDF Resume"),
|
199 |
-
gr.Textbox(label="Job Description", placeholder="Enter the job description here...",
|
|
|
200 |
],
|
201 |
outputs=gr.Gallery(label="Processed PDF Pages"),
|
202 |
title="PDF Processing and Job Description",
|
203 |
-
description="Upload a PDF resume
|
204 |
-
theme=custom_theme
|
205 |
)
|
206 |
|
207 |
|
|
|
8 |
from dotenv import load_dotenv
|
9 |
load_dotenv()
|
10 |
|
|
|
|
|
|
|
11 |
|
12 |
INTERMEDIATE_JSON_PATH = "intermediate_data.json"
|
13 |
INTERMEDIATE_JOB_DESC_PATH = "intermediate_job_desc.txt"
|
|
|
30 |
except Exception as e:
|
31 |
return f"Error loading prompt: {e}"
|
32 |
|
33 |
+
def process_pdf_and_save_job_desc(pdf_file, job_description, api_key):
|
34 |
+
if not pdf_file:
|
35 |
+
return None, "No file provided"
|
36 |
+
|
37 |
+
# Configure the Gemini model using the provided API key
|
38 |
+
genai.configure(api_key=api_key)
|
39 |
+
model_vision = genai.GenerativeModel('gemini-pro-vision')
|
40 |
+
model_text = genai.GenerativeModel("gemini-pro")
|
41 |
+
|
42 |
+
doc = fitz.open(stream=pdf_file, filetype="pdf")
|
43 |
+
|
44 |
+
# Store results in a list and process all pages
|
45 |
+
json_data = []
|
46 |
+
images = [] # List to hold images of each page
|
47 |
+
for page_num in range(len(doc)):
|
48 |
+
page = doc.load_page(page_num)
|
49 |
+
pix = page.get_pixmap()
|
50 |
+
img_bytes = pix.tobytes("png")
|
51 |
+
image = Image.open(io.BytesIO(img_bytes))
|
52 |
+
images.append(image)
|
53 |
+
|
54 |
+
# ... Your image processing with the genai model
|
55 |
+
prompt = load_prompt("prompts/resume_parsing_prompt.txt")
|
56 |
+
response = model_vision.generate_content([prompt, image])
|
57 |
+
json_data.append(response.text)
|
58 |
+
|
59 |
+
doc.close()
|
60 |
+
|
61 |
+
# Store data appropriately (consider a list of JSON objects, or a structured dict)
|
62 |
+
with open(INTERMEDIATE_JSON_PATH, "w", encoding='utf-8') as json_file: # Specify UTF-8 encoding
|
63 |
+
json.dump(json_data, json_file)
|
64 |
+
with open(INTERMEDIATE_JOB_DESC_PATH, "w", encoding='utf-8') as file: # Specify UTF-8 encoding
|
65 |
+
file.write(job_description)
|
66 |
+
|
67 |
+
return images, json_data # Return the list of images
|
|
|
|
|
68 |
|
69 |
def display_json():
|
70 |
try:
|
|
|
93 |
except Exception as e:
|
94 |
return f"An error occurred: {e}"
|
95 |
|
96 |
+
def generate_interview_questions(api_key):
|
|
|
|
|
|
|
97 |
with open(INTERMEDIATE_JSON_PATH, "r") as json_file:
|
98 |
json_data = json.load(json_file)
|
99 |
|
100 |
+
combined_data = " ".join(json_data) # Combine with spaces (adjust as needed)
|
101 |
prompt = load_prompt("prompts/interview_questions_prompt.txt") + combined_data
|
|
|
|
|
102 |
|
103 |
+
# Configure the Gemini model using the provided API key
|
104 |
+
genai.configure(api_key=api_key)
|
105 |
+
model_text = genai.GenerativeModel("gemini-pro")
|
106 |
|
107 |
+
responses = model_text.generate_content(prompt)
|
108 |
+
return responses.text
|
109 |
|
110 |
+
# Define the new Gradio interface for generating interview questions with API key input
|
111 |
interview_interface = gr.Interface(
|
112 |
fn=generate_interview_questions,
|
113 |
+
inputs=[gr.Textbox(label="Gemini API Key", placeholder="Enter your Gemini API key here...")],
|
114 |
outputs=gr.Textbox(label="Generated Interview Questions"),
|
115 |
title="Generate Interview Questions"
|
116 |
)
|
117 |
|
118 |
|
119 |
+
def generate_skill_gap_analysis(api_key):
|
120 |
try:
|
121 |
# Read the saved resume data (JSON)
|
122 |
with open(INTERMEDIATE_JSON_PATH, "r") as file:
|
|
|
126 |
with open(INTERMEDIATE_JOB_DESC_PATH, "r") as file:
|
127 |
job_description = file.read()
|
128 |
|
129 |
+
# Configure the Gemini model using the provided API key
|
130 |
+
genai.configure(api_key=api_key)
|
131 |
+
model_text = genai.GenerativeModel("gemini-pro")
|
132 |
+
|
133 |
# Construct a detailed prompt for the Gemini model
|
134 |
prompt = load_prompt("prompts/skills_gap_prompt.txt").replace(
|
135 |
"job_description", job_description).replace("json_data", json_data)
|
|
|
142 |
except Exception as e:
|
143 |
return f"An error occurred: {e}"
|
144 |
|
145 |
+
# Define the Gradio interface for generating a skill gap analysis with API key input
|
146 |
skill_gap_analysis_interface = gr.Interface(
|
147 |
fn=generate_skill_gap_analysis,
|
148 |
+
inputs=[gr.Textbox(label="Gemini API Key", placeholder="Enter your Gemini API key here...")],
|
149 |
outputs=gr.Textbox(label="Skill Gap Analysis"),
|
150 |
title="Skill Gap Analysis"
|
151 |
)
|
152 |
|
153 |
|
154 |
+
def generate_cover_letter(api_key):
|
155 |
try:
|
156 |
+
# Configure the Gemini model using the provided API key
|
157 |
+
genai.configure(api_key=api_key)
|
158 |
+
model_text = genai.GenerativeModel("gemini-pro")
|
159 |
+
|
160 |
# Read the saved job description
|
161 |
with open(INTERMEDIATE_JOB_DESC_PATH, "r") as file:
|
162 |
job_description = file.read()
|
|
|
170 |
"job_description", job_description).replace("json_data", json_data)
|
171 |
|
172 |
# Generate the cover letter using the model
|
173 |
+
response = model_text.generate_content(prompt)
|
|
|
174 |
|
175 |
return response.text
|
176 |
|
177 |
except Exception as e:
|
178 |
return f"An error occurred: {e}"
|
179 |
|
180 |
+
# Define the Gradio interface for generating a cover letter with API key input
|
|
|
181 |
cover_letter_interface = gr.Interface(
|
182 |
fn=generate_cover_letter,
|
183 |
+
inputs=[gr.Textbox(label="Gemini API Key", placeholder="Enter your Gemini API key here...")],
|
184 |
outputs=gr.Textbox(label="Generated Cover Letter"),
|
185 |
title="Cover Letter Generator"
|
186 |
)
|
187 |
|
188 |
|
189 |
+
def gradio_pdf_interface(pdf_content, job_description, api_key):
|
190 |
+
images, _ = process_pdf_and_save_job_desc(pdf_content, job_description, api_key)
|
|
|
191 |
return images # Return the list of images to be displayed in the Gallery
|
192 |
|
193 |
+
# Define the updated interface for PDF processing with an additional input for the API key
|
|
|
194 |
pdf_interface = gr.Interface(
|
195 |
fn=gradio_pdf_interface,
|
196 |
inputs=[
|
197 |
gr.File(type="binary", label="Upload PDF Resume"),
|
198 |
+
gr.Textbox(label="Job Description", placeholder="Enter the job description here..."),
|
199 |
+
gr.Textbox(label="Gemini API Key", placeholder="Enter your Gemini API key here...")
|
200 |
],
|
201 |
outputs=gr.Gallery(label="Processed PDF Pages"),
|
202 |
title="PDF Processing and Job Description",
|
203 |
+
description="Upload a PDF resume, provide the job description, and enter your Gemini API key. The system will process the resume and extract relevant data."
|
|
|
204 |
)
|
205 |
|
206 |
|