krishbakshi commited on
Commit
5fe86ee
·
verified ·
1 Parent(s): 7880997

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -31
app.py CHANGED
@@ -1,10 +1,11 @@
1
  import gradio as gr
2
  import os
 
3
  import utils
4
  import config
5
  import pandas as pd
6
 
7
- def output(file, text1, text2, text3, text4, text5, text6, progress: gr.Progress = gr.Progress(track_tqdm=True)):
8
 
9
  output_text = ""
10
 
@@ -16,6 +17,9 @@ def output(file, text1, text2, text3, text4, text5, text6, progress: gr.Progress
16
  if structured_data is None:
17
  return gr.Error("Unsupported file type.")
18
 
 
 
 
19
  prompt = f"""
20
  {{
21
  "Hiring Manager's Name": "{text1}",
@@ -28,27 +32,51 @@ def output(file, text1, text2, text3, text4, text5, text6, progress: gr.Progress
28
  }}
29
  """
30
  progress(0.5)
31
- output_text = utils.generate_email(prompt)
32
  progress(1)
33
 
34
  return output_text, output_text
35
 
36
- def bulk_output(excel, pdf, progress: gr.Progress = gr.Progress(track_tqdm=True)):
 
37
  if excel is None:
38
- return gr.Error("No file uploaded.")
39
-
40
- excel_path = excel.name
41
- df = pd.read_excel(excel_path)
42
-
43
  if pdf is None:
44
- return gr.Error("No file uploaded.")
45
-
46
- file_path = pdf.name
47
- structured_data = utils.process_resume(file_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  if structured_data is None:
49
- return gr.Error("Unsupported file type.")
50
-
51
- for index, row in df.iterrows():
 
 
 
 
52
  prompt = f"""
53
  {{
54
  "Hiring Manager's Name": "{row['Hiring Managers Name']}",
@@ -61,43 +89,67 @@ def bulk_output(excel, pdf, progress: gr.Progress = gr.Progress(track_tqdm=True)
61
  }}
62
  """
63
 
64
- email = utils.generate_email(prompt)
65
- config.write_email(row['Hiring Manager\'s Name'], row['Company Name'], email)
66
- progress(index + 1, len(df))
 
 
67
 
68
  return "Emails have been drafted and saved as Word documents."
69
 
70
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
71
  with gr.Tabs():
72
  with gr.TabItem("Single Email"):
73
  with gr.Row():
74
- with gr.Column(scale=0.5):
75
  text1 = gr.Textbox(label="Hiring Manager's Name")
76
  text2 = gr.Textbox(label="Company Name")
77
  text3 = gr.Textbox(label="Industry/Field")
78
  text4 = gr.Textbox(label="Specific Projects, Innovations, or Company Achievements")
79
  text5 = gr.Textbox(label="Specific Skills or Tools Relevant to the Job")
80
  text6 = gr.Textbox(label="Specific Roles, Teams, or Projects at the Company")
81
- submit_btn = gr.Button("Submit")
82
- with gr.Column(scale=0.5):
83
  pdf_input = gr.File(label="Upload PDF", file_types=[".pdf", ".docx"], interactive=True, scale=1)
 
 
84
  output_box = gr.Textbox(label="Output", lines=10, autoscroll=True)
85
- write_btn = gr.Button("Draft Email")
86
-
 
 
 
 
87
 
88
  state = gr.State()
89
- submit_btn.click(output, inputs=[pdf_input, text1, text2, text3, text4, text5, text6], outputs=[output_box, state])
90
- write_btn.click(config.write_email, inputs=[text1, text2, state])
91
 
92
  with gr.TabItem("Batch Email"):
93
  with gr.Row():
94
- with gr.Column(scale=0.5):
95
  pdf_input = gr.File(label="Upload PDF", file_types=[".pdf", ".docx"], interactive=True, scale=1)
96
- excel_input = gr.File(label="Upload Excel", file_types=[".xlsx"], interactive=True, scale=1)
 
 
97
  submit_btn = gr.Button("Submit and Draft")
98
- with gr.Column(scale=0.5):
99
  output_box = gr.Textbox(label="Output", lines=10, autoscroll=True)
100
-
101
- submit_btn.click(bulk_output, inputs=[excel_input,pdf_input], outputs=[output_box])
 
 
 
 
 
 
102
 
103
  demo.launch()
 
1
  import gradio as gr
2
  import os
3
+ from tqdm import tqdm
4
  import utils
5
  import config
6
  import pandas as pd
7
 
8
+ def output(file, text1, text2, text3, text4, text5, text6, template, progress: gr.Progress = gr.Progress(track_tqdm=True)):
9
 
10
  output_text = ""
11
 
 
17
  if structured_data is None:
18
  return gr.Error("Unsupported file type.")
19
 
20
+ # Get the selected template value
21
+ selected_template = config.template[template]
22
+
23
  prompt = f"""
24
  {{
25
  "Hiring Manager's Name": "{text1}",
 
32
  }}
33
  """
34
  progress(0.5)
35
+ output_text = utils.generate_email(prompt, selected_template)
36
  progress(1)
37
 
38
  return output_text, output_text
39
 
40
+ def bulk_output(excel, pdf, template, progress: gr.Progress = gr.Progress(track_tqdm=True)):
41
+ # Check for uploaded files
42
  if excel is None:
43
+ return gr.Error("No Excel file uploaded.")
 
 
 
 
44
  if pdf is None:
45
+ return gr.Error("No PDF file uploaded.")
46
+
47
+ # Determine file extension and read accordingly
48
+ file_ext = os.path.splitext(excel.name)[-1].lower()
49
+ try:
50
+ if file_ext == '.csv':
51
+ df = pd.read_csv(excel.name)
52
+ elif file_ext in ['.xls', '.xlsx']:
53
+ df = pd.read_excel(excel.name)
54
+ else:
55
+ return gr.Error("Unsupported file type. Please upload a .csv or .xlsx file.")
56
+ except Exception as e:
57
+ return gr.Error(f"Failed to read the file: {str(e)}")
58
+
59
+ # Check required columns
60
+ required_columns = [
61
+ 'Hiring Managers Name', 'Company Name', 'Industry/Field',
62
+ 'Specific Projects, Innovations, or Company Achievements',
63
+ 'Specific Skills or Tools Relevant to the Job',
64
+ 'Specific Roles, Teams, or Projects at the Company'
65
+ ]
66
+ missing_columns = [col for col in required_columns if col not in df.columns]
67
+ if missing_columns:
68
+ return gr.Error(f"The uploaded Excel file is missing required columns: {', '.join(missing_columns)}")
69
+
70
+ # Process the resume
71
+ structured_data = utils.process_resume(pdf.name)
72
  if structured_data is None:
73
+ return gr.Error("Unsupported or unreadable resume file.")
74
+
75
+ # Get the selected template value
76
+ selected_template = config.template[template]
77
+
78
+ # Loop through each row and generate emails
79
+ for _, row in tqdm(df.iterrows(), total=len(df), desc="Generating emails"):
80
  prompt = f"""
81
  {{
82
  "Hiring Manager's Name": "{row['Hiring Managers Name']}",
 
89
  }}
90
  """
91
 
92
+ try:
93
+ email = utils.generate_email(prompt, selected_template)
94
+ config.write_email(row['Hiring Managers Name'], row['Company Name'], email)
95
+ except Exception as e:
96
+ return gr.Error(f"Error generating email for {row['Company Name']}: {str(e)}")
97
 
98
  return "Emails have been drafted and saved as Word documents."
99
 
100
+ def generate_and_download(name, company, ai_email):
101
+ file_path = config.write_email(name, company, ai_email)
102
+ return file_path
103
+
104
+ def bulk_generate_and_download():
105
+ zip_file_path = config.return_bulk_file()
106
+ return zip_file_path
107
+
108
+ with gr.Blocks(css="body { background: #f8fafc; font-family: 'Inter'; }") as demo:
109
  with gr.Tabs():
110
  with gr.TabItem("Single Email"):
111
  with gr.Row():
112
+ with gr.Column(scale=1):
113
  text1 = gr.Textbox(label="Hiring Manager's Name")
114
  text2 = gr.Textbox(label="Company Name")
115
  text3 = gr.Textbox(label="Industry/Field")
116
  text4 = gr.Textbox(label="Specific Projects, Innovations, or Company Achievements")
117
  text5 = gr.Textbox(label="Specific Skills or Tools Relevant to the Job")
118
  text6 = gr.Textbox(label="Specific Roles, Teams, or Projects at the Company")
119
+ with gr.Row():
120
+ template = gr.Dropdown(choices=list(config.template.keys()), label="Select Email Template", interactive=True)
121
  pdf_input = gr.File(label="Upload PDF", file_types=[".pdf", ".docx"], interactive=True, scale=1)
122
+ submit_btn = gr.Button("Submit")
123
+ with gr.Column(scale=1):
124
  output_box = gr.Textbox(label="Output", lines=10, autoscroll=True)
125
+ with gr.Row():
126
+ with gr.Column(scale=1):
127
+ write_btn = gr.Button("Draft Email")
128
+ with gr.Column(scale=1):
129
+ download_button = gr.File(label="Download Word Document")
130
+
131
 
132
  state = gr.State()
133
+ submit_btn.click(output, inputs=[pdf_input, text1, text2, text3, text4, text5, text6, template], outputs=[output_box, state])
134
+ write_btn.click(generate_and_download, inputs=[text1, text2, state], outputs= [download_button])
135
 
136
  with gr.TabItem("Batch Email"):
137
  with gr.Row():
138
+ with gr.Column(scale=1):
139
  pdf_input = gr.File(label="Upload PDF", file_types=[".pdf", ".docx"], interactive=True, scale=1)
140
+ excel_input = gr.File(label="Upload Excel", file_types=[".xlsx", ".csv"], interactive=True, scale=1)
141
+ with gr.Row():
142
+ template = gr.Dropdown(choices=list(config.template.keys()), label="Select Email Template", interactive=True)
143
  submit_btn = gr.Button("Submit and Draft")
144
+ with gr.Column(scale=1):
145
  output_box = gr.Textbox(label="Output", lines=10, autoscroll=True)
146
+ with gr.Row():
147
+ with gr.Column(scale=1):
148
+ Zip_btn = gr.Button("Make Zip File")
149
+ with gr.Column(scale=1):
150
+ download_button = gr.File(label="Download ZIP File")
151
+
152
+ submit_btn.click(bulk_output, inputs=[excel_input, pdf_input, template], outputs=[output_box])
153
+ Zip_btn.click(bulk_generate_and_download, inputs=[], outputs=[download_button])
154
 
155
  demo.launch()