File size: 3,188 Bytes
a8246b8
 
 
 
 
 
 
 
 
 
 
 
ef2ffd9
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
# app.py

import gradio as gr
import requests

# Replace 'YOUR_GEMINI_API_KEY' with your actual Gemini API key.
GEMINI_API_KEY = "AIzaSyB05WLxtH1x4fMvB87M-GggjQlBnm3YWeE"

# This function sends the resume and job description to the Gemini API
# and returns an optimized resume tailored to the job description.
def optimize_resume(resume, job_description):
    # Gemini API endpoint for text generation (update if needed)
    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, rewrite the resume to be optimized for the job. "
        "Keep the formatting clear and professional. "
        "Only output the improved resume.\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
    if response.status_code == 200:
        result = response.json()
        # Extract the generated text from the response
        try:
            optimized_resume = result["candidates"][0]["content"]["parts"][0]["text"]
            return optimized_resume
        except (KeyError, IndexError):
            return "Error: Unexpected response format from Gemini API."
    else:
        # If there's an error, return the error message
        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!
        """
    )
    
    # Input for the user's resume
    resume_input = gr.Textbox(
        label="Paste your Resume",
        lines=15,
        placeholder="Paste your resume here..."
    )
    
    # Input for the job description
    job_desc_input = gr.Textbox(
        label="Paste the Job Description",
        lines=10,
        placeholder="Paste the job description here..."
    )
    
    # Output box for the optimized resume
    optimized_resume_output = gr.Textbox(
        label="Optimized Resume",
        lines=15
    )
    
    # Button to submit the inputs and get the optimized resume
    submit_btn = gr.Button("Optimize Resume")
    
    # When the button is clicked, call the optimize_resume function
    submit_btn.click(
        fn=optimize_resume,
        inputs=[resume_input, job_desc_input],
        outputs=optimized_resume_output
    )

# Run the Gradio app
if __name__ == "__main__":
    demo.launch()