Spaces:
Sleeping
Sleeping
File size: 7,527 Bytes
8d3a7b6 146e133 8d3a7b6 a0c8d1c 3083ad8 8b45fde a0c8d1c 8d3a7b6 3beb864 8d3a7b6 3083ad8 8b45fde 3beb864 8b45fde 3beb864 8d3a7b6 8b45fde a0c8d1c 8d3a7b6 |
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
import os
from shiny import reactive, render, ui
from context import (
get_candidate_context,
get_job_context,
get_team_summary,
save_candidate_context,
get_all_jobs,
get_all_candidates
)
from llm_connect import get_response
from fpdf import FPDF
import markdown
import io
def draft_offer_letter(candidate_name, job_title, compensation, start_date, team_summary, job_description, hiring_manager_notes):
prompt = (
f"Candidate Name: {candidate_name}\n"
f"Job Title: {job_title}\n"
f"Compensation: {compensation}\n"
f"Start Date: {start_date}\n\n"
f"Job Description:\n{job_description}\n\n"
f"Team Summary:\n{team_summary}\n\n"
f"Hiring Manager Notes:\n{hiring_manager_notes}\n\n"
"Write a professional, clear, and positive offer letter for this candidate. "
"Include a summary of the role, compensation details, start date, and a warm welcome. "
"Avoid excessive legal language but maintain formality."
)
return get_response(
input=prompt,
template=lambda x: x,
llm="llama",
md=False,
temperature=0.5,
max_tokens=600
).strip()
def generate_full_contract(candidate_name, job_title, compensation, start_date, clauses, company_policies, legal_notes):
prompt = (
f"Candidate Name: {candidate_name}\n"
f"Job Title: {job_title}\n"
f"Compensation: {compensation}\n"
f"Start Date: {start_date}\n\n"
f"Clauses:\n{clauses}\n\n"
f"Company Policies:\n{company_policies}\n\n"
f"Legal Notes:\n{legal_notes}\n\n"
"Draft a complete employment contract using the information above. "
"Structure it with proper headings, include all clauses, and align with common HR compliance standards. "
"Use formal legal language where appropriate."
)
return get_response(
input=prompt,
template=lambda x: x,
llm="llama",
md=False,
temperature=0.4,
max_tokens=1200
).strip()
def server(input, output, session):
print("β
Entered document generation server()")
# === Update job dropdown from context ===
@reactive.effect
def _populate_job_dropdown():
jobs = get_all_jobs()
# value = job_id (UUID), label = title
job_choices = {
k: f"{v.get('title', 'Untitled')} ({k[:8]})"
for k, v in jobs.items()
}
ui.update_select("job_dropdown_doc", choices=job_choices)
# === Update candidate dropdown based on selected job ===
@reactive.effect
def _populate_candidate_dropdown():
job_id = input.job_dropdown_doc()
print("π selected job_id:", job_id)
if not job_id:
ui.update_select("candidate_dropdown_doc", choices={"β¬
οΈ Select a job first": ""})
return
candidates = get_all_candidates()
filtered = {
cid: f"{v.get('Name', cid)} ({v.get('Resume File', 'N/A')})"
for cid, v in candidates.items()
if v.get("job_id") == job_id and v.get("Resume File")
}
print(f"β
Found {len(filtered)} candidates for job {job_id}")
if filtered:
ui.update_select("candidate_dropdown_doc", choices=filtered)
else:
ui.update_select("candidate_dropdown_doc", choices={"β No matching resumes": ""})
# === Offer letter generation ===
@output
@render.text
@reactive.event(input.generate_offer)
def offer_letter_text():
candidate_id = input.candidate_dropdown_doc()
job_id = input.job_dropdown_doc()
print("π¦ candidate_id:", candidate_id)
print("π¦ job_id:", job_id)
if not candidate_id or not job_id:
return "β Select a resume and a job."
ctx = get_candidate_context(candidate_id)
job = get_job_context(job_id)
print("π ctx loaded:", bool(ctx))
print("π job loaded:", bool(job))
if not ctx or not job:
return "β Missing candidate or job context."
comp_override = input.override_compensation().strip()
start_override = input.override_start_date().strip()
notes_override = input.override_notes().strip()
offer = draft_offer_letter(
candidate_name=ctx.get("Name", "Candidate"),
job_title=job.get("title", "Unknown Role"),
compensation=comp_override or job.get("compensation", "TBD"),
start_date=start_override or job.get("start_date", "TBD"),
team_summary=get_team_summary(),
job_description=job.get("job_description", ""),
hiring_manager_notes=notes_override or job.get("notes", "")
)
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Arial", size=12)
for line in offer.split("\n"):
pdf.multi_cell(0, 10, line)
os.makedirs(f'/tmp/data/{job_id}/offers', exist_ok=True)
pdf_path = f'/tmp/data/{job_id}/offers/Offer_Letter_{candidate_id}.pdf'
pdf.output(pdf_path)
return ui.HTML(f"<pre style='font-family: Georgia; font-size: 1rem'>{offer}</pre>")
# === Contract generation ===
@output
@render.text
@reactive.event(input.generate_contract)
def contract_text():
candidate_id = input.candidate_dropdown_doc()
job_id = input.job_dropdown_doc()
if not candidate_id or not job_id:
return "β Select a resume and a job."
ctx = get_candidate_context(candidate_id)
job = get_job_context(job_id)
if not ctx or not job:
return "β Missing candidate or job context."
comp_override = input.override_compensation().strip()
start_override = input.override_start_date().strip()
contract = generate_full_contract(
candidate_name=ctx.get("Name", "Candidate"),
job_title=job.get("title", "Unknown Role"),
compensation=comp_override or job.get("compensation", "TBD"),
start_date=start_override or job.get("start_date", "TBD"),
clauses=job.get("clauses", "Standard IP, termination, arbitration clauses."),
company_policies=job.get("policies", "All standard company HR policies apply."),
legal_notes=job.get("legal_notes", "Subject to U.S. labor law.")
)
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Arial", size=12)
for line in contract.split("\n"):
pdf.multi_cell(0, 10, line)
os.makedirs(f'/tmp/data/{job_id}/contracts', exist_ok=True)
pdf_path = f'/tmp/data/{job_id}/contracts/Contract_{candidate_id}.pdf'
pdf.output(pdf_path)
return ui.HTML(f"<pre style='font-family: Georgia; font-size: 1rem'>{contract}</pre>")
@output
@render.download(filename="Offer_Letter.pdf")
def download_offer():
candidate_id = input.candidate_dropdown_doc()
job_id = input.job_dropdown_doc()
pdf = f'/tmp/data/{job_id}/offers/Offer_Letter_{candidate_id}.pdf'
return pdf
@output
@render.download(filename="Contract.pdf")
def download_contract():
candidate_id = input.candidate_dropdown_doc()
job_id = input.job_dropdown_doc()
pdf = f"/tmp/data/{job_id}/contracts/Contract_{candidate_id}.pdf"
return pdf
|