Input_prompt.format issue with google.generativeai

Hello,

I really could use some help.

I am trying to create a CV screening app where you paste in a job description of the job you want to apply to; you upload your CV, and it will match keywords at count a % match. Much like an ATS (Applicant Tracking System), but much simpler.

I have gotten everything to work, but when I try to do a match, I get this error:


Traceback:
File "C:\ProgramData\anaconda3\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 542, in _run_script
    exec(code, module.__dict__)File "C:\Users\svanb\PY_Proj\ATS\ATS_CV.py", line 69, in <module>
    prompt = input_prompt.format(text=text, jd=jd)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

KeyError: ' "JD Match" '

  • I have tried so many different ways to input a job description, but it keeps saying this the whole time.
  • I am very new to Python and certainly to Google AI, but I really want to learn more about this.
  • Anyone that perhaps can see what I am doing wrong and can lead to the right path? :wink: <3

This is the code I have gotten:

import streamlit as st
import google.generativeai as genai
import os
import json
import PyPDF2 as pdf
import pandas as pd
from dotenv import load_dotenv

load_dotenv()  # Load all our environment variables

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

def get_gemini_response(input):
    model = genai.GenerativeModel('gemini-pro')
    response = model.generate_content(input)
    return str(response.text)

def input_pdf_text(uploaded_file):
    reader = pdf.PdfReader(uploaded_file)
    text = ""
    for page in range(len(reader.pages)):
        page = reader.pages[page]
        text += str(page.extract_text())
    return text

# Prompt Template
input_prompt = """
Act as a highly skilled and experienced Applicant Tracking System (ATS) with profound knowledge 
in the technology field, including IT Support, IT Project Management, Continuous Improvement, data science, data analysis, 
and big data engineering. Your task is to evaluate the resume against the provided job description. 
Given the highly competitive job market, provide the best possible assistance for enhancing the resume. 
Assign the percentage Matching based on Jd and Matching based on Jd and the missing keywords with high accuracy
resume:{text}
description:{jd}

I want the response in one single string having the structure
{"JD Match":"%","MissingKeywords:[]","Profile Summary":""}
"""

## Streamlit Application
st.markdown("""
    <div style='background-color: #6CD2E9; padding: 20px; border-radius: 20px;border-style: solid; border-color: darkcyan; border-width: 4px;'>
        <h1 style='font-weight: bold; font-size: 30px; font-color: white; font-family: Verdana; text-align: center;'>Screen your CV</h1>
    </div>
""", unsafe_allow_html=True)

st.markdown("""
<div class="container" style='border: 4px solid #198F96; padding: 20px; border-radius: 10px;'>
    <div class='markdown-text-container'>
        <br>
        <p><b>Improve Your CV get higher ATS (Applicant Tracking System) ratings.</b></p>
        <p>Recruiters increasingly rely on Applicant Tracking 
        Systems (ATS) to evaluate and score applicants, enabling them to efficiently narrow the pool to a more 
        manageable group.</p>
        <p>Thus, it's crucial to incorporate the right keywords in your CV that align with the job 
        description. This strategic approach significantly enhances your chances of standing out in the competitive job market.</p>
    </div>
</div> 
""", unsafe_allow_html=True)

jd = st.text_area("Paste the Job Description for the job you are applying for", height=200)
uploaded_file = st.file_uploader("Upload Your CV here", type="pdf", help="Please upload the PDF")

submit = st.button("Submit")

if submit:
    if uploaded_file is not None:
        text = input_pdf_text(uploaded_file)
        prompt = input_prompt.format(text=text, jd=jd)
        response = get_gemini_response(prompt)

        response_dict = json.loads(response)

        # Convert the dictionary to a DataFrame
        df = pd.DataFrame([response_dict])

        # Display DataFrame
        st.table(df)

Kind Regards,
Sandra aka SnowY <3

Welcome to the forums! I’m not a python expert, and this isn’t exactly a Gemini problem, but I think I can point you in the right direction.

Looking at your traceback:

Traceback:
File "C:\ProgramData\anaconda3\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 542, in _run_script
    exec(code, module.__dict__)File "C:\Users\svanb\PY_Proj\ATS\ATS_CV.py", line 69, in <module>
    prompt = input_prompt.format(text=text, jd=jd)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

KeyError: ' "JD Match" '

There are two things that jump you here. The first is that you’re using format() and providing two key/value pairs. The second is that it is saying there is a key error somewhere.

Looking at the string your’e formatting, it includes these lines to be replaced indicating that {text} should be replaced with the value of the parmeter text you provide, since format replaces stuff inside brackets:

resume:{text}
description:{jd}

but it also includes these lines:

I want the response in one single string having the structure
{"JD Match":"%","MissingKeywords:[]","Profile Summary":""}

Which, I assume, is the JSON format you want Gemini to mimic.

But look… there’s the “JD Match” from the error message above! And it is inside brackets. So format() was trying to replace that with the contents of the “JD Key” that you passed to format(). But you didn’t pass that key. And you don’t want format() touching that anyway.

The solution to escape those braces as described in the documentation for format():

If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} .

So you can replace that line with something more like

I want the response in one single string having the structure
{{"JD Match":"%","MissingKeywords:[]","Profile Summary":""}}
2 Likes

Thank you so much!

I was at least looking already at the right part that was wrong, but now I also understand what I did wrong. Got it all up and running after I learned about an .env file and a requirement.txt :slight_smile:

This was my first Python project and as I was so impressed by Google Gemini after a 4 day introduction course, I really wanted to create something with it.