from io import BytesIO import pandas as pd import requests import streamlit as st import json from tqdm import tqdm from openai import AzureOpenAI import json_repair import backoff import os # API setting constants API_MAX_RETRY = 5 API_RETRY_SLEEP = 10 class GPTAgent: def __init__(self): self.client = AzureOpenAI( api_key=os.getenv("OPENAI_API_KEY"), api_version="2024-02-15-preview", azure_endpoint=os.getenv("END_POINTS") ) self.deployment_name = ("gpt-4o-mini") @backoff.on_exception(backoff.expo, requests.exceptions.RequestException, max_tries=8) def invoke(self, text): prompt = """You are a creative recruitment specialist tasked with generating witty and engaging recruitment potshots. Your goal is to craft short, attention-grabbing statements designed to attract potential candidates for job openings. These potshots should be tailored to the role, audience, and company values provided, and should make the job opportunity stand out in a competitive market.""" temperature = 0.9 max_tokens = 3500 response = self.client.chat.completions.create( model=self.deployment_name, messages=[ {"role": "system", "content": prompt}, {"role": "user", "content": text}, ], temperature=temperature, max_tokens=max_tokens, ) output = response.choices[0].message.content return output def generate_potshot_prompt(batch, role, tone, audience, values): prompt = f"""You are a recruitment specialist tasked with generating witty and engaging recruitment potshots. Your goal is to craft short, attention-grabbing statements designed to attract potential job candidates for the role of {role}. These statements should appeal to {audience} and reflect the company's values: {values}. Please adopt a tone that is {tone}. Your objective is to make the job opportunity stand out and be highly appealing to the intended audience. Provide this list of {batch} recruitment potshots in JSON format, for example: {{"potshots": ["potshot 1", "potshot 2", ..., "potshot n"]}} Remember to focus on being engaging, playful, and sharp in these recruitment potshots.""" return prompt.strip() # Function to get recruitment potshots with customizations def get_potshots(n_repeat=1, batch=20, role="Developer", tone="humorous", audience="tech-savvy candidates", values="innovation, teamwork"): total_potshots = [] desired_count = n_repeat * batch with tqdm(total=desired_count, desc="Generating potshots") as pbar: while len(total_potshots) < desired_count: needed = min(batch, desired_count - len(total_potshots)) prompt = generate_potshot_prompt(batch=needed, role=role, tone=tone, audience=audience, values=values) gpt_agent = GPTAgent() response = gpt_agent.invoke(prompt) try: batch_potshots = json.loads(response).get("potshots", []) total_potshots.extend(batch_potshots) pbar.update(len(batch_potshots)) except json.JSONDecodeError: # Attempt to repair the JSON if decoding fails try: repaired_json = json_repair.repair_json(response, skip_json_loads=True, return_objects=False) batch_potshots = json.loads(repaired_json).get("potshots", []) total_potshots.extend(batch_potshots) pbar.update(len(batch_potshots)) except json.JSONDecodeError: st.error("Failed to decode JSON response even after repair attempt. Skipping this batch.") if len(total_potshots) > desired_count: total_potshots = total_potshots[:desired_count] prompt_list = [potshot for potshot in total_potshots] return {"potshots": prompt_list} # Streamlit App Interface st.title("Customized Recruiting Potshots Generator") # Input Fields for Customization # Input Fields for Full Customization with Default Values role = st.text_input("Job Role", value="Developer", help="Enter the job role you are recruiting for. Default is 'Developer'.") tone = st.text_input("Tone of the Potshots", value="humorous", help="Enter any tone for the potshots (e.g., humorous, serious, edgy). Default is 'humorous'.") audience = st.text_input("Target Audience", value="tech-savvy candidates", help="Define the target audience for the potshots. Default is 'tech-savvy candidates'.") values = st.text_area("Company Values", value="innovation, teamwork, transparency", help="Enter your company values that should be reflected in the potshots. Default is 'innovation, teamwork, transparency'.") batch_size = st.number_input("Batch Size", min_value=1, max_value=100, value=10) repeat_times = st.number_input("Number of Batches", min_value=1, max_value=10, value=1) def to_excel(df): output = BytesIO() writer = pd.ExcelWriter(output, engine='xlsxwriter') df.to_excel(writer, index=False, sheet_name='Potshots') writer.save() processed_data = output.getvalue() return processed_data if st.button("Generate Potshots"): with st.spinner("Generating customized potshots..."): df = get_potshots(n_repeat=repeat_times, batch=batch_size, role=role, tone=tone, audience=audience, values=values) # Display the generated potshots as a table st.write("### Generated Potshots") st.dataframe(df) # Provide a download button for the Excel file st.write("### Download Potshots") excel_data = to_excel(df) st.download_button(label="Download Potshots as Excel", data=excel_data, file_name="potshots.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")