-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
219 lines (187 loc) · 9.68 KB
/
app.py
File metadata and controls
219 lines (187 loc) · 9.68 KB
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
import gradio as gr
import os
from tqdm import tqdm
import utils
import config
import pandas as pd
def output(file, text1, text2, text3, text4, text5, text6, template, progress: gr.Progress = gr.Progress(track_tqdm=True)):
output_text = ""
if file is None:
return gr.Error("No file uploaded.")
file_path = file.name
structured_data = utils.process_resume(file_path)
if structured_data is None:
return gr.Error("Unsupported file type.")
# Get the selected template value
selected_template = config.template[template]
prompt = f"""
{{
"Hiring Manager's Name": "{text1}",
"Company Name": "{text2}",
"Industry/Field": "{text3}",
"Specific Projects, Innovations, or Company Achievements": "{text4}",
"Specific Skills or Tools Relevant to the Job": "{text5}",
"Specific Roles, Teams, or Projects at the Company": "{text6}",
"resume": {structured_data}
}}
"""
progress(0.5)
output_text = utils.generate_email(prompt, selected_template)
progress(1)
return output_text, output_text
def bulk_output_to_docx(excel, pdf, template, progress: gr.Progress = gr.Progress(track_tqdm=True)):
# Check for uploaded files
if excel is None:
return gr.Error("No Excel file uploaded.")
if pdf is None:
return gr.Error("No PDF file uploaded.")
# Determine file extension and read accordingly
file_ext = os.path.splitext(excel.name)[-1].lower()
try:
if file_ext == '.csv':
df = pd.read_csv(excel.name)
elif file_ext in ['.xls', '.xlsx']:
df = pd.read_excel(excel.name)
else:
return gr.Error("Unsupported file type. Please upload a .csv or .xlsx file.")
except Exception as e:
return gr.Error(f"Failed to read the file: {str(e)}")
# Check required columns
required_columns = [
'Hiring Managers Name', 'Company Name', 'Industry/Field',
'Specific Projects, Innovations, or Company Achievements',
'Specific Skills or Tools Relevant to the Job',
'Specific Roles, Teams, or Projects at the Company'
]
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
return gr.Error(f"The uploaded Excel file is missing required columns: {', '.join(missing_columns)}")
# Process the resume
structured_data = utils.process_resume(pdf.name)
if structured_data is None:
return gr.Error("Unsupported or unreadable resume file.")
# Get the selected template value
selected_template = config.template[template]
# Loop through each row and generate emails
for _, row in tqdm(df.iterrows(), total=len(df), desc="Generating emails"):
prompt = f"""
{{
"Hiring Manager's Name": "{row['Hiring Managers Name']}",
"Company Name": "{row['Company Name']}",
"Industry/Field": "{row['Industry/Field']}",
"Specific Projects, Innovations, or Company Achievements": "{row['Specific Projects, Innovations, or Company Achievements']}",
"Specific Skills or Tools Relevant to the Job": "{row['Specific Skills or Tools Relevant to the Job']}",
"Specific Roles, Teams, or Projects at the Company": "{row['Specific Roles, Teams, or Projects at the Company']}",
"resume": {structured_data}
}}
"""
try:
email = utils.generate_email(prompt, selected_template)
config.write_email(row['Hiring Managers Name'], row['Company Name'], email)
except Exception as e:
return gr.Error(f"Error generating email for {row['Company Name']}: {str(e)}")
return "Emails have been drafted and saved as Word documents."
def bulk_output_to_gmail(excel, pdf, template, progress: gr.Progress = gr.Progress(track_tqdm=True)):
# Check for uploaded files
if excel is None:
return gr.Error("No Excel file uploaded.")
if pdf is None:
return gr.Error("No PDF file uploaded.")
# Determine file extension and read accordingly
file_ext = os.path.splitext(excel.name)[-1].lower()
try:
if file_ext == '.csv':
df = pd.read_csv(excel.name)
elif file_ext in ['.xls', '.xlsx']:
df = pd.read_excel(excel.name)
else:
return gr.Error("Unsupported file type. Please upload a .csv or .xlsx file.")
except Exception as e:
return gr.Error(f"Failed to read the file: {str(e)}")
# Check required columns
required_columns = [
'Hiring Managers Name', 'Company Name', 'Industry/Field',
'Specific Projects, Innovations, or Company Achievements',
'Specific Skills or Tools Relevant to the Job',
'Specific Roles, Teams, or Projects at the Company'
]
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
return gr.Error(f"The uploaded Excel file is missing required columns: {', '.join(missing_columns)}")
# Process the resume
structured_data = utils.process_resume(pdf.name)
if structured_data is None:
return gr.Error("Unsupported or unreadable resume file.")
# Get the selected template value
selected_template = config.template[template]
# Loop through each row and generate emails
for _, row in tqdm(df.iterrows(), total=len(df), desc="Generating emails"):
prompt = f"""
{{
"Hiring Manager's Name": "{row['Hiring Managers Name']}",
"Company Name": "{row['Company Name']}",
"Industry/Field": "{row['Industry/Field']}",
"Specific Projects, Innovations, or Company Achievements": "{row['Specific Projects, Innovations, or Company Achievements']}",
"Specific Skills or Tools Relevant to the Job": "{row['Specific Skills or Tools Relevant to the Job']}",
"Specific Roles, Teams, or Projects at the Company": "{row['Specific Roles, Teams, or Projects at the Company']}",
"resume": {structured_data}
}}
"""
try:
email = utils.generate_email(prompt, selected_template)
utils.save_gmail_draft(email, pdf.name)
except Exception as e:
return gr.Error(f"Error generating Gmail draft for {row['Company Name']}: {str(e)}")
return "Emails have been drafted as Gmail drafts."
def generate_and_download(name, company, ai_email):
file_path = config.write_email(name, company, ai_email)
return file_path
def bulk_generate_and_download():
zip_file_path = config.return_bulk_file()
return zip_file_path
with gr.Blocks(css="body { background: #f8fafc; font-family: 'Inter'; }") as demo:
with gr.Tabs():
with gr.TabItem("Single Email"):
with gr.Row():
with gr.Column(scale=1):
text1 = gr.Textbox(label="Hiring Manager's Name")
text2 = gr.Textbox(label="Company Name")
text3 = gr.Textbox(label="Industry/Field")
text4 = gr.Textbox(label="Specific Projects, Innovations, or Company Achievements")
text5 = gr.Textbox(label="Specific Skills or Tools Relevant to the Job")
text6 = gr.Textbox(label="Specific Roles, Teams, or Projects at the Company")
with gr.Row():
template = gr.Dropdown(choices=list(config.template.keys()), label="Select Email Template", interactive=True)
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf", ".docx"], interactive=True, scale=1)
submit_btn = gr.Button("Submit")
with gr.Column(scale=1):
output_box = gr.Textbox(label="Output", lines=10, autoscroll=True)
with gr.Row():
with gr.Column(scale=1):
write_btn = gr.Button("Draft Word Document")
draft_btn = gr.Button("Draft Email")
with gr.Column(scale=1):
download_button = gr.File(label="Download Word Document")
state = gr.State()
submit_btn.click(output, inputs=[pdf_input, text1, text2, text3, text4, text5, text6, template], outputs=[output_box, state])
write_btn.click(generate_and_download, inputs=[text1, text2, state], outputs= [download_button])
draft_btn.click(utils.save_gmail_draft, inputs=[state, pdf_input], outputs=[output_box])
with gr.TabItem("Batch Email"):
with gr.Row():
with gr.Column(scale=1):
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf", ".docx"], interactive=True, scale=1)
excel_input = gr.File(label="Upload Excel", file_types=[".xlsx", ".csv"], interactive=True, scale=1)
with gr.Row():
template = gr.Dropdown(choices=list(config.template.keys()), label="Select Email Template", interactive=True)
docx_btn = gr.Button("Generate Word Documents")
gmail_btn = gr.Button("Draft Gmail Emails")
with gr.Column(scale=1):
output_box = gr.Textbox(label="Output", lines=10, autoscroll=True)
with gr.Row():
with gr.Column(scale=1):
Zip_btn = gr.Button("Make Zip File")
with gr.Column(scale=1):
download_button = gr.File(label="Download ZIP File")
docx_btn.click(bulk_output_to_docx, inputs=[excel_input, pdf_input, template], outputs=[output_box])
gmail_btn.click(bulk_output_to_gmail, inputs=[excel_input, pdf_input, template], outputs=[output_box])
demo.launch()