mobenta commited on
Commit
5d62c16
·
verified ·
1 Parent(s): 19ad8e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cohere
3
+ from docx import Document
4
+ from docx.shared import Pt
5
+ from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
6
+ import pypandoc
7
+ import os
8
+
9
+ # Initialize Cohere client with your API key
10
+ cohere_api_key = '' # Replace with your actual Cohere API key
11
+ co = cohere.Client(cohere_api_key)
12
+
13
+ def generate_body(job_description, language):
14
+ # Set the language model based on user selection
15
+ model = 'command-xlarge-nightly' # Default to command-xlarge-nightly model for both English and German
16
+
17
+ # Modify the prompt to exclude greetings
18
+ 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}"
19
+
20
+ # Use Cohere's API to generate the body of the application letter
21
+ response = co.generate(
22
+ model=model,
23
+ prompt=prompt,
24
+ max_tokens=250, # Reduced to ensure the content fits on one page
25
+ temperature=0.7,
26
+ )
27
+ return response.generations[0].text.strip()
28
+
29
+ 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):
30
+ # Generate the body using the job description and language
31
+ body = generate_body(job_description, language)
32
+
33
+ # Create a new Document
34
+ doc = Document()
35
+
36
+ # Add header with sender's name, address, email, and phone in a single line
37
+ header = doc.sections[0].header
38
+ header_paragraph = header.paragraphs[0]
39
+ header_paragraph.text = f"{name} | {address} | {email} | {phone}"
40
+ header_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
41
+
42
+ # Adjust the font size for the header
43
+ for run in header_paragraph.runs:
44
+ run.font.size = Pt(10)
45
+
46
+ # Add one blank line above the employer's information
47
+ doc.add_paragraph("\n")
48
+
49
+ # Add the date and employer's information in the main document
50
+ doc.add_paragraph(f"{employer_name}\n{employer_contact_name if greeting_option == 'Known' else ''}\n{employer_address}")
51
+ doc.add_paragraph(f"{address.split(',')[1]}, {start_date}\n\n") # Assuming the city is part of the address
52
+
53
+ # Add the subject
54
+ doc.add_paragraph(f"Bewerbung als {job_position}\nKennnummer {job_id}\n", style='Heading 2')
55
+
56
+ # Add the greeting based on the selected option
57
+ if language == "German":
58
+ if greeting_option == "Known" and employer_contact_name:
59
+ doc.add_paragraph(f"Sehr geehrter Herr {employer_contact_name.split()[0]},\n")
60
+ else:
61
+ doc.add_paragraph("Sehr geehrte Damen und Herren,\n")
62
+ else:
63
+ if greeting_option == "Known" and employer_contact_name:
64
+ doc.add_paragraph(f"Dear {employer_contact_name},\n")
65
+ else:
66
+ doc.add_paragraph("Dear Sir/Madam,\n")
67
+
68
+ # Add the generated body of the letter
69
+ doc.add_paragraph(body)
70
+
71
+ # Add closing
72
+ closing_text = (
73
+ f"\nIch unterstütze Ihr Team gerne ab dem {start_date} und freue mich über die Einladung zu einem persönlichen Vorstellungsgespräch."
74
+ if language == "German" else
75
+ f"\nI am eager to join your team starting on {start_date} and look forward to the opportunity to discuss my application further."
76
+ )
77
+ doc.add_paragraph(closing_text)
78
+ doc.add_paragraph("\nMit freundlichen Grüßen,\n\n" if language == "German" else "\nSincerely,\n\n")
79
+ doc.add_paragraph(f"{name}\n")
80
+
81
+ # Adjust font size for body to ensure it fits on one page
82
+ for paragraph in doc.paragraphs:
83
+ for run in paragraph.runs:
84
+ run.font.size = Pt(11) # Slightly reduced to ensure one-page fit
85
+
86
+ # Save the document
87
+ output_filename_docx = f'{name}_application_letter.docx'
88
+ doc.save(output_filename_docx)
89
+
90
+ # Convert to PDF if requested
91
+ if output_format == "PDF":
92
+ output_filename_pdf = f'{name}_application_letter.pdf'
93
+ pypandoc.convert_file(output_filename_docx, 'pdf', outputfile=output_filename_pdf)
94
+ os.remove(output_filename_docx) # Optionally remove the DOCX file after conversion
95
+ return output_filename_pdf
96
+ else:
97
+ return output_filename_docx
98
+
99
+ 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):
100
+ # Generate the application letter
101
+ 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)
102
+ # Return the file for download
103
+ return output_filename
104
+
105
+ # Define the Gradio interface
106
+ with gr.Blocks() as demo:
107
+ name = gr.Textbox(label="Name", placeholder="Enter your full name", value="Claire Waßer")
108
+ address = gr.Textbox(label="Address", placeholder="Enter your address", value="Musterstraße 78, 23456 Musterstadt")
109
+ email = gr.Textbox(label="Email", placeholder="Enter your email", value="[email protected]")
110
+ phone = gr.Textbox(label="Phone", placeholder="Enter your phone number", value="0171 23456789")
111
+ job_position = gr.Textbox(label="Job Position", placeholder="Enter the job position", value="Physiotherapeutin")
112
+ employer_name = gr.Textbox(label="Employer Name", placeholder="Enter the employer's name", value="Arbeitgeber GmbH")
113
+ greeting_option = gr.Dropdown(choices=["Known", "Unknown"], label="Is the recipient's name known?", value="Unknown")
114
+ employer_contact_name = gr.Textbox(label="Employer Contact Name", placeholder="Enter the contact person's name (if known)", value="Name Nachname", visible=False)
115
+ employer_address = gr.Textbox(label="Employer Address", placeholder="Enter the employer's address", value="Straße 123, 12345 Musterstadt")
116
+ job_id = gr.Textbox(label="Job ID", placeholder="Enter the job ID", value="123456")
117
+ start_date = gr.Textbox(label="Start Date", placeholder="Enter the start date (e.g., 15.05.2020)", value="18.08.2024")
118
+ 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...")
119
+ language = gr.Dropdown(choices=["English", "German"], label="Select Language", value="German")
120
+ output_format = gr.Dropdown(choices=["DOCX", "PDF"], label="Select Output Format", value="PDF")
121
+
122
+ # Show or hide the employer contact name input based on the greeting option
123
+ def update_visibility(greeting_option):
124
+ return gr.update(visible=(greeting_option == "Known"))
125
+
126
+ greeting_option.change(update_visibility, inputs=greeting_option, outputs=employer_contact_name)
127
+
128
+ generate_button = gr.Button("Generate Application Letter")
129
+ output = gr.File(label="Download your application letter")
130
+
131
+ generate_button.click(generate_and_download,
132
+ 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],
133
+ outputs=output)
134
+
135
+ # Launch the Gradio interface
136
+ demo.launch(debug=True)