Spaces:
Sleeping
Sleeping
# app.py | |
import gradio as gr | |
import requests | |
import os # Import os to access environment variables | |
def optimize_resume_with_explanation(resume, job_description): | |
# Get the Gemini API key from Hugging Face secrets (environment variable) | |
GEMINI_API_KEY = os.environ.get("api_key") | |
if not GEMINI_API_KEY: | |
return "Error: Gemini API key not found in environment variables.", "" | |
# Gemini API endpoint | |
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent" | |
# Prepare the prompt for the AI model | |
prompt = ( | |
"You are a professional resume writer. " | |
"Given the following resume and job description, do two things:\n" | |
"1. Rewrite the resume to be optimized for the job. " | |
"Keep the formatting clear and professional. " | |
"Only output the improved resume in a section titled 'Optimized Resume'.\n" | |
"2. In a section titled 'Explanation of Modifications', explain what changes you made to the resume and why, " | |
"focusing on how the modifications help better match the job description.\n\n" | |
f"Resume:\n{resume}\n\n" | |
f"Job Description:\n{job_description}\n" | |
) | |
# Prepare the request payload for Gemini API | |
data = { | |
"contents": [ | |
{ | |
"parts": [ | |
{"text": prompt} | |
] | |
} | |
] | |
} | |
# Set the headers, including the API key for authentication | |
headers = { | |
"Content-Type": "application/json", | |
"x-goog-api-key": GEMINI_API_KEY | |
} | |
# Send the POST request to Gemini API | |
response = requests.post(url, headers=headers, json=data) | |
# If the request is successful, extract and return the optimized resume and explanation | |
if response.status_code == 200: | |
result = response.json() | |
try: | |
full_response = result["candidates"][0]["content"]["parts"][0]["text"] | |
# Split the response into the two sections | |
optimized_resume = "" | |
explanation = "" | |
if "Optimized Resume" in full_response and "Explanation of Modifications" in full_response: | |
parts = full_response.split("Explanation of Modifications") | |
optimized_resume_section = parts[0] | |
explanation_section = parts[1] | |
optimized_resume = optimized_resume_section.replace("Optimized Resume", "").strip(": \n") | |
explanation = explanation_section.strip(": \n") | |
else: | |
optimized_resume = "Could not extract optimized resume. Please try again." | |
explanation = full_response | |
return optimized_resume, explanation | |
except (KeyError, IndexError): | |
return "Error: Unexpected response format from Gemini API.", "" | |
else: | |
return f"Error: {response.status_code} - {response.text}", "" | |
# Create the Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
""" | |
# AI Resume Optimizer | |
Paste your resume and the job description below. | |
The AI will rewrite your resume to better match the job, and explain what it changed and why! | |
""" | |
) | |
resume_input = gr.Textbox( | |
label="Paste your Resume", | |
lines=15, | |
placeholder="Paste your resume here..." | |
) | |
job_desc_input = gr.Textbox( | |
label="Paste the Job Description", | |
lines=10, | |
placeholder="Paste the job description here..." | |
) | |
optimized_resume_output = gr.Textbox( | |
label="Optimized Resume", | |
lines=15 | |
) | |
explanation_output = gr.Textbox( | |
label="Explanation of Modifications", | |
lines=10 | |
) | |
submit_btn = gr.Button("Optimize Resume") | |
submit_btn.click( | |
fn=optimize_resume_with_explanation, | |
inputs=[resume_input, job_desc_input], | |
outputs=[optimized_resume_output, explanation_output] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |