File size: 4,033 Bytes
a8246b8
 
 
 
465686b
a8246b8
0c9c3ff
465686b
 
 
 
 
 
ef2ffd9
a8246b8
 
 
 
0c9c3ff
 
a8246b8
0c9c3ff
 
 
a8246b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c9c3ff
a8246b8
 
 
0c9c3ff
 
 
 
 
 
 
 
 
 
 
 
 
 
a8246b8
0c9c3ff
a8246b8
0c9c3ff
a8246b8
 
 
 
 
 
 
0c9c3ff
a8246b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c9c3ff
 
 
 
 
a8246b8
 
 
0c9c3ff
a8246b8
0c9c3ff
a8246b8
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
103
104
105
106
107
108
109
110
111
112
113
114
# 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()