Spaces:
Build error
Build error
File size: 7,261 Bytes
5d62c16 |
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 |
import gradio as gr
import cohere
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
import pypandoc
import os
# Initialize Cohere client with your API key
cohere_api_key = '' # Replace with your actual Cohere API key
co = cohere.Client(cohere_api_key)
def generate_body(job_description, language):
# Set the language model based on user selection
model = 'command-xlarge-nightly' # Default to command-xlarge-nightly model for both English and German
# Modify the prompt to exclude greetings
prompt = f"Write a professional job application letter in {language} without a greeting. Only generate the body text based on this job description:\n{job_description}"
# Use Cohere's API to generate the body of the application letter
response = co.generate(
model=model,
prompt=prompt,
max_tokens=250, # Reduced to ensure the content fits on one page
temperature=0.7,
)
return response.generations[0].text.strip()
def create_application_letter(name, address, email, phone, job_position, employer_name, greeting_option, employer_contact_name, employer_address, job_id, start_date, job_description, language, output_format):
# Generate the body using the job description and language
body = generate_body(job_description, language)
# Create a new Document
doc = Document()
# Add header with sender's name, address, email, and phone in a single line
header = doc.sections[0].header
header_paragraph = header.paragraphs[0]
header_paragraph.text = f"{name} | {address} | {email} | {phone}"
header_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
# Adjust the font size for the header
for run in header_paragraph.runs:
run.font.size = Pt(10)
# Add one blank line above the employer's information
doc.add_paragraph("\n")
# Add the date and employer's information in the main document
doc.add_paragraph(f"{employer_name}\n{employer_contact_name if greeting_option == 'Known' else ''}\n{employer_address}")
doc.add_paragraph(f"{address.split(',')[1]}, {start_date}\n\n") # Assuming the city is part of the address
# Add the subject
doc.add_paragraph(f"Bewerbung als {job_position}\nKennnummer {job_id}\n", style='Heading 2')
# Add the greeting based on the selected option
if language == "German":
if greeting_option == "Known" and employer_contact_name:
doc.add_paragraph(f"Sehr geehrter Herr {employer_contact_name.split()[0]},\n")
else:
doc.add_paragraph("Sehr geehrte Damen und Herren,\n")
else:
if greeting_option == "Known" and employer_contact_name:
doc.add_paragraph(f"Dear {employer_contact_name},\n")
else:
doc.add_paragraph("Dear Sir/Madam,\n")
# Add the generated body of the letter
doc.add_paragraph(body)
# Add closing
closing_text = (
f"\nIch unterstütze Ihr Team gerne ab dem {start_date} und freue mich über die Einladung zu einem persönlichen Vorstellungsgespräch."
if language == "German" else
f"\nI am eager to join your team starting on {start_date} and look forward to the opportunity to discuss my application further."
)
doc.add_paragraph(closing_text)
doc.add_paragraph("\nMit freundlichen Grüßen,\n\n" if language == "German" else "\nSincerely,\n\n")
doc.add_paragraph(f"{name}\n")
# Adjust font size for body to ensure it fits on one page
for paragraph in doc.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(11) # Slightly reduced to ensure one-page fit
# Save the document
output_filename_docx = f'{name}_application_letter.docx'
doc.save(output_filename_docx)
# Convert to PDF if requested
if output_format == "PDF":
output_filename_pdf = f'{name}_application_letter.pdf'
pypandoc.convert_file(output_filename_docx, 'pdf', outputfile=output_filename_pdf)
os.remove(output_filename_docx) # Optionally remove the DOCX file after conversion
return output_filename_pdf
else:
return output_filename_docx
def generate_and_download(name, address, email, phone, job_position, employer_name, greeting_option, employer_contact_name, employer_address, job_id, start_date, job_description, language, output_format):
# Generate the application letter
output_filename = create_application_letter(name, address, email, phone, job_position, employer_name, greeting_option, employer_contact_name, employer_address, job_id, start_date, job_description, language, output_format)
# Return the file for download
return output_filename
# Define the Gradio interface
with gr.Blocks() as demo:
name = gr.Textbox(label="Name", placeholder="Enter your full name", value="Claire Waßer")
address = gr.Textbox(label="Address", placeholder="Enter your address", value="Musterstraße 78, 23456 Musterstadt")
email = gr.Textbox(label="Email", placeholder="Enter your email", value="[email protected]")
phone = gr.Textbox(label="Phone", placeholder="Enter your phone number", value="0171 23456789")
job_position = gr.Textbox(label="Job Position", placeholder="Enter the job position", value="Physiotherapeutin")
employer_name = gr.Textbox(label="Employer Name", placeholder="Enter the employer's name", value="Arbeitgeber GmbH")
greeting_option = gr.Dropdown(choices=["Known", "Unknown"], label="Is the recipient's name known?", value="Unknown")
employer_contact_name = gr.Textbox(label="Employer Contact Name", placeholder="Enter the contact person's name (if known)", value="Name Nachname", visible=False)
employer_address = gr.Textbox(label="Employer Address", placeholder="Enter the employer's address", value="Straße 123, 12345 Musterstadt")
job_id = gr.Textbox(label="Job ID", placeholder="Enter the job ID", value="123456")
start_date = gr.Textbox(label="Start Date", placeholder="Enter the start date (e.g., 15.05.2020)", value="18.08.2024")
job_description = gr.Textbox(label="Job Description", placeholder="Paste the job description here", lines=7, value="Aktuell stehe ich am Ende meiner Berufsausbildung zur staatlich anerkannten Physiotherapeutin...")
language = gr.Dropdown(choices=["English", "German"], label="Select Language", value="German")
output_format = gr.Dropdown(choices=["DOCX", "PDF"], label="Select Output Format", value="PDF")
# Show or hide the employer contact name input based on the greeting option
def update_visibility(greeting_option):
return gr.update(visible=(greeting_option == "Known"))
greeting_option.change(update_visibility, inputs=greeting_option, outputs=employer_contact_name)
generate_button = gr.Button("Generate Application Letter")
output = gr.File(label="Download your application letter")
generate_button.click(generate_and_download,
inputs=[name, address, email, phone, job_position, employer_name, greeting_option, employer_contact_name, employer_address, job_id, start_date, job_description, language, output_format],
outputs=output)
# Launch the Gradio interface
demo.launch(debug=True)
|