-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstream.py
726 lines (614 loc) · 31.5 KB
/
stream.py
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import json
import os
import re
import pandas as pd
import streamlit as st
from datetime import datetime, timedelta
from fpdf import FPDF
from dotenv import load_dotenv
from groq import Groq
from PIL import Image
import io
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import base64
from time import time
def load_templates():
try:
with open("templates.json", "r") as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
st.error("❌ templates.json file is missing or invalid!")
st.stop()
def load_faculty_list():
try:
return pd.read_excel("facultylist.xlsx")
except FileNotFoundError:
st.error("❌ facultylist.xlsx file not found!")
st.stop()
faculty_df = load_faculty_list()
def validate_date(date_obj):
if not date_obj:
return None
return date_obj.strftime("%d-%m-%Y")
def validate_contact(number):
return number if re.fullmatch(r"\d{10,12}", number) else None
def generate_ai_leave_letter(data, faculty_df):
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
return "❌ Error: Missing GROQ API Key!"
client = Groq(api_key=api_key)
faculty_designation = ""
recipient_line = ""
sir_madam = ""
if data['subto'] == "Principal":
recipient_line = "The Principal"
sir_madam = "Sir/Madam"
else:
faculty_info = faculty_df[faculty_df['Faculty'] == data['subto']]
if not faculty_info.empty:
faculty_designation = faculty_info.iloc[0]['Designation']
recipient_line = f"{data['subto']}\n{faculty_designation}\n{faculty_info.iloc[0]['Department']}"
sir_madam = "Sir/Madam"
if 'additional_students' in data and len(data.get('additional_students', [])) > 2:
prompt = f"""
Write a formal leave letter using the following format:
From:
{data['user']}
{data['year_of_study']} {data['programme']} ({data['department']})
St. Joseph's College of Engineering and Technology
Palai
To:
{recipient_line}
St. Joseph's College of Engineering and Technology
Palai
Date: [Current Date]
Subject:
Respected {sir_madam},
Request leave from {data['start_date']} to {data['end_date']}.
Reason: {data['extra_details']}
Format it professionally with a polite tone in 2-3 paragraphs as it is given to college and include proper closing with Thanking you and Yours faithfully. Don't mention the other students in the letter body. In the closing section, mention only the main student's name without department and college name.
"""
else:
prompt = f"""
Write a formal leave letter using the following format:
From:
{data['user']}
{data['year_of_study']} {data['programme']} ({data['department']})
St. Joseph's College of Engineering and Technology
Palai
To:
{recipient_line}
St. Joseph's College of Engineering and Technology
Palai
Date: [Current Date]
Subject:
Respected {sir_madam},
Request leave from {data['start_date']} to {data['end_date']}.
Reason: {data['extra_details']}
Format it professionally with a polite tone in 2-3 paragraphs as it is given to college and include proper closing with Thanking you and Yours faithfully. In the closing section, mention only the main student's name without department and college name.
"""
try:
response = client.chat.completions.create(
messages=[
{"role": "system", "content": "You generate professional leave letters following standard academic letter writing formats."},
{"role": "user", "content": prompt}
],
model="llama-3.3-70b-versatile"
)
return response.choices[0].message.content if response.choices else "❌ AI Response Error!"
except Exception as e:
return f"❌ Error: {str(e)}"
#CSS for buttons
st.markdown("""
<style>
.stButton button {
width: 120px; # Adjust width as needed
white-space: nowrap;
margin-right: 30px;
}
</style>
""", unsafe_allow_html=True)
def chat_interface():
st.title("💬 DutyFree\nGenerate your apolegy/leave letter within 30sec.\n An AI tool for SJCET Students")
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.step = 0
st.session_state.leave_data = {}
st.session_state.messages.append({"role": "assistant", "text": "👋 What's your name?"})
questions = [
"👋 What's your name?",
"📚 Select your programme:",
"🏢 Select your department:",
"📌 To whom is this letter addressed?",
"📚 Which year do you study?",
"📅 Select the start date of your leave:",
"📅 Select the end date of your leave:",
"👥 Do you want to add more students to this letter?"
]
fields = ["user", "programme", "department", "subto", "year_of_study", "start_date", "end_date", "add_students"]
programme_options = ["B.Tech", "M.Tech"]
for msg in st.session_state.messages:
st.chat_message("assistant" if msg["role"] == "assistant" else "user").write(msg["text"])
def go_back():
if st.session_state.step > 0:
if len(st.session_state.messages) >= 2:
st.session_state.messages.pop()
st.session_state.messages.pop()
if fields[st.session_state.step - 1] in st.session_state.leave_data:
del st.session_state.leave_data[fields[st.session_state.step - 1]]
st.session_state.step -= 1
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
if st.session_state.step < len(questions):
field_name = fields[st.session_state.step]
user_input = None
if field_name == "add_students":
user_choice = st.radio("👥 Do you want to add more students to this letter?", ["No", "Yes"], key="add_students_radio")
if user_choice == "Yes":
st.write("Enter additional students' details:")
num_students = st.number_input("Number of additional students", min_value=1, max_value=5, value=1)
additional_students = []
for i in range(num_students):
st.write(f"Student {i+1}")
name = st.text_input(f"Name", key=f"student_name_{i}")
year = st.selectbox(f"Year of Study",
["1st Year", "2nd Year", "3rd Year", "4th Year"]
if st.session_state.leave_data.get("programme") == "B.Tech"
else ["1st Year", "2nd Year"],
key=f"student_year_{i}")
if name and year:
additional_students.append({"name": name, "year": year})
cols = st.columns([1, 1, 1, 5])
with cols[0]:
if st.button("⬅️ Back", key="add_students_back_btn"):
go_back()
st.rerun()
with cols[2]:
if st.button("Next ➡️", key="next_add_students"):
if user_choice == "Yes" and additional_students:
st.session_state.leave_data["additional_students"] = additional_students
st.session_state.messages.append({"role": "user", "text": f"Additional students: {user_choice}"})
st.session_state.step += 1
st.rerun()
elif st.session_state.step > 0:
cols = st.columns([1, 3, 1])
if field_name == "user":
user_input = st.chat_input("")
elif field_name == "programme":
user_input = st.radio(questions[st.session_state.step], programme_options, key="programme_radio")
cols = st.columns([1, 1,1, 5])
with cols[0]:
if st.button("⬅️ Back", key="programme_back"):
go_back()
st.rerun()
with cols[2]:
if st.button("Next ➡️", key="programme_next"):
st.session_state.messages.append({"role": "user", "text": user_input})
st.session_state.leave_data[field_name] = user_input
st.session_state.step += 1
if st.session_state.step < len(questions):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
elif field_name == "department":
selected_programme = st.session_state.leave_data.get("programme", "B.Tech")
department_options = faculty_df[faculty_df['Programme'] == selected_programme]['Department'].unique().tolist()
user_input = st.selectbox(questions[st.session_state.step], department_options, key="department_select")
cols = st.columns([1, 1, 1, 5])
with cols[0]:
if st.button("⬅️ Back", key="department_back"):
go_back()
st.rerun()
with cols[2]:
if st.button("Next ➡️", key="department_next"):
st.session_state.messages.append({"role": "user", "text": user_input})
st.session_state.leave_data[field_name] = user_input
st.session_state.step += 1
if st.session_state.step < len(questions):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
elif field_name == "subto":
recipient_type = st.radio("Select recipient:", ["Principal", "Faculty"], horizontal=True, key="recipient_radio")
if recipient_type == "Principal":
user_input = "Principal"
cols = st.columns([1, 1, 1, 5])
with cols[0]:
if st.button("⬅️ Back", key="principal_back"):
go_back()
st.rerun()
with cols[2]:
if st.button("Next ➡️", key="principal_next"):
st.session_state.messages.append({"role": "user", "text": user_input})
st.session_state.leave_data[field_name] = user_input
st.session_state.step += 1
if st.session_state.step < len(questions):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
else:
selected_department = st.session_state.leave_data.get("department", "")
faculty_options = faculty_df[faculty_df['Department'] == selected_department]['Faculty'].tolist()
selected_faculty = st.selectbox("📜 Select Faculty:", faculty_options, key="faculty_select")
cols = st.columns([1, 1, 1, 5])
with cols[0]:
if st.button("⬅️ Back", key="faculty_back"):
go_back()
st.rerun()
with cols[2]:
if st.button("Next ➡️", key="faculty_next"):
st.session_state.messages.append({"role": "user", "text": selected_faculty})
st.session_state.leave_data[field_name] = selected_faculty
st.session_state.step += 1
if st.session_state.step < len(questions):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
elif field_name == "year_of_study":
selected_programme = st.session_state.leave_data.get("programme", "B.Tech")
year_options = ["1st Year", "2nd Year", "3rd Year", "4th Year"] if selected_programme == "B.Tech" else ["1st Year", "2nd Year"]
user_input = st.radio(questions[st.session_state.step], year_options, key="year_radio")
cols = st.columns([1, 1, 1, 5])
with cols[0]:
if st.button("⬅️ Back", key="year_back"):
go_back()
st.rerun()
with cols[2]:
if st.button("Next ➡️", key="year_next"):
st.session_state.messages.append({"role": "user", "text": user_input})
st.session_state.leave_data[field_name] = user_input
st.session_state.step += 1
if st.session_state.step < len(questions):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
elif field_name in ["start_date", "end_date"]:
if field_name == "start_date":
min_date = datetime.now() - timedelta(days=30)
max_date = datetime.now() + timedelta(days=365)
else:
start_date = datetime.strptime(st.session_state.leave_data.get("start_date", datetime.now().strftime("%d-%m-%Y")), "%d-%m-%Y")
min_date = start_date
max_date = start_date + timedelta(days=365)
if not any(msg["text"] == questions[st.session_state.step] for msg in st.session_state.messages):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
date_container = st.container()
with date_container:
date_value = st.date_input(
"",
min_value=min_date.date() if isinstance(min_date, datetime) else min_date,
max_value=max_date.date() if isinstance(max_date, datetime) else max_date,
key=f"{field_name}_calendar"
)
st.write("")
col1, col2, col3 = st.columns([1, 0.1, 1])
with col1:
if st.button("⬅️ Back", key=f"back_{field_name}"):
go_back()
st.rerun()
with col3:
if st.button("Next ➡️", key=f"next_{field_name}"):
validated_date = validate_date(date_value)
if validated_date:
st.session_state.messages.append({"role": "user", "text": validated_date})
st.session_state.leave_data[field_name] = validated_date
st.session_state.step += 1
if st.session_state.step < len(questions):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
else:
if field_name != "add_students":
if not any(msg["text"] == questions[st.session_state.step] for msg in st.session_state.messages):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
cols = st.columns([1, 3, 1])
with cols[0]:
if st.button("⬅️ Back", key=f"back_{field_name}"):
go_back()
st.rerun()
user_input = st.chat_input("")
# Handle text input validation and progression
if user_input and field_name not in ["programme", "department", "subto", "year_of_study", "start_date", "end_date", "add_students"]:
st.session_state.leave_data[field_name] = user_input
st.session_state.messages.append({"role": "user", "text": user_input})
st.session_state.step += 1
if st.session_state.step < len(questions):
st.session_state.messages.append({"role": "assistant", "text": questions[st.session_state.step]})
st.rerun()
else:
templates = load_templates()
choice = st.radio("📄 Choose a template or AI-generated letter:", ["Template", "AI"], horizontal=True)
if choice == "Template":
selected_template = st.selectbox("📜 Select a template:", list(templates.keys()))
st.session_state.leave_data["template"] = selected_template
else:
st.session_state.leave_data["template"] = "AI-generated"
st.session_state.leave_data["extra_details"] = st.text_area("📝 Describe your reason:")
# Main student signature
signature_path = st.file_uploader("✍️ Upload your signature (optional)", type=["png", "jpg", "jpeg"], key="main_signature")
# Additional students' signatures
if 'additional_students' in st.session_state.leave_data:
st.write("Upload signatures for additional students (optional):")
signatures = {}
for i, student in enumerate(st.session_state.leave_data['additional_students']):
sig = st.file_uploader(
f"✍️ Upload signature for {student['name']} (optional)",
type=["png", "jpg", "jpeg"],
key=f"signature_{i}"
)
if sig:
signatures[student['name']] = sig
st.session_state.leave_data['additional_signatures'] = signatures
cols = st.columns([1, 3, 1])
with cols[0]:
if st.button("⬅️ Back to Questions"):
go_back()
st.rerun()
with cols[2]:
if st.button("✅ Generate Leave Letter"):
return st.session_state.leave_data, signature_path
return None, None
def generate_leave_letter(data, templates, faculty_df, signature_path=None):
current_date = datetime.now().strftime("%d-%m-%Y")
template_data = {
'user': data.get('user', ''),
'year_of_study': data.get('year_of_study', ''),
'programme': data.get('programme', ''),
'department': data.get('department', ''),
'start_date': data.get('start_date', ''),
'end_date': data.get('end_date', ''),
'current_date': current_date,
'signature_date': current_date,
'signature': '[Student Signature]',
'subto': data.get('subto', '')
}
if data.get('subto') == "Principal":
template_data['recipient_address'] = "The Principal\nSt. Joseph's College of Engineering and Technology\nPalai"
else:
faculty_info = faculty_df[faculty_df['Faculty'] == data['subto']]
if not faculty_info.empty:
template_data['recipient_address'] = f"{data['subto']}\n{faculty_info.iloc[0]['Designation']}\n{faculty_info.iloc[0]['Department']}\nSt. Joseph's College of Engineering and Technology\nPalai"
else:
template_data['recipient_address'] = f"{data['subto']}\nSt. Joseph's College of Engineering and Technology\nPalai"
if 'additional_students' in data:
additional_names = "\nAdditional students:\n"
for student in data['additional_students']:
additional_names += f"- {student['name']} ({student['year']})\n"
if data.get('template') == "AI-generated":
data['extra_details'] = f"{data.get('extra_details', '')}\n\n{additional_names}"
else:
template_data['additional_students'] = additional_names
# Generate letter content
if data.get('template') == "AI-generated":
letter_content = generate_ai_leave_letter(data, faculty_df)
else:
try:
template = templates.get(data['template'], '')
if 'additional_students' in data:
template_data['additional_students'] = "\nAdditional students:\n" + "\n".join([f"- {student['name']} ({student['year']})" for student in data['additional_students']])
letter_content = template.format(**template_data)
except KeyError as e:
st.error(f"Template error: Missing field {e}")
return
except Exception as e:
st.error(f"Error generating letter: {str(e)}")
return
def clean_text(text):
cleaned = text.encode('ascii', 'ignore').decode('ascii')
replacements = {
'✅': 'X',
'❌': 'X',
'📝': '-',
'👋': '-',
'📚': '-',
'🏢': '-',
'📌': '-',
'📅': '-',
'👥': '-',
'✍️': '-',
'⬅️': '<-',
'➡️': '->',
'⏳': '-',
'⚠️': '!',
'📧': '-',
'📥': '-'
}
for old, new in replacements.items():
cleaned = cleaned.replace(old, new)
return cleaned
letter_content = clean_text(letter_content)
# PDF Generation
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# Add the letter content
pdf.multi_cell(0, 8, letter_content)
if 'additional_students' in data:
pdf.ln(10)
pdf.set_font("Arial", 'B', 12)
pdf.cell(0, 10, "Student Details:", ln=True)
pdf.ln(5)
# Table settings
col_widths = [70, 60, 60]
row_height = 10
pdf.set_font("Arial", 'B', 10)
pdf.set_fill_color(200, 200, 200)
pdf.cell(col_widths[0], row_height, "Name", 1, 0, 'C', 1)
pdf.cell(col_widths[1], row_height, "Year of Study", 1, 0, 'C', 1)
pdf.cell(col_widths[2], row_height, "Signature", 1, 1, 'C', 1)
pdf.set_font("Arial", '', 10)
pdf.cell(col_widths[0], row_height, data['user'], 1, 0, 'L')
pdf.cell(col_widths[1], row_height, data['year_of_study'], 1, 0, 'C')
sig_cell_y = pdf.get_y()
pdf.cell(col_widths[2], row_height, "", 1, 1, 'C')
#main signature
if signature_path:
signature_img = Image.open(signature_path)
signature_img = signature_img.resize((50, 20), Image.LANCZOS)
temp_path = "temp_signature.png"
signature_img.save(temp_path)
pdf.image(temp_path, x=pdf.get_x() + 145, y=sig_cell_y, w=30, h=8)
os.remove(temp_path)
for student in data['additional_students']:
pdf.cell(col_widths[0], row_height, student['name'], 1, 0, 'L')
pdf.cell(col_widths[1], row_height, student['year'], 1, 0, 'C')
sig_cell_y = pdf.get_y()
pdf.cell(col_widths[2], row_height, "", 1, 1, 'C') # Empty cell for signature
# Addl signatures
if 'additional_signatures' in data and student['name'] in data['additional_signatures']:
sig_path = data['additional_signatures'][student['name']]
if sig_path:
sig_img = Image.open(sig_path)
sig_img = sig_img.resize((50, 20), Image.LANCZOS)
temp_path = f"temp_sig_{student['name']}.png"
sig_img.save(temp_path)
pdf.image(temp_path, x=pdf.get_x() + 145, y=sig_cell_y, w=30, h=8)
os.remove(temp_path)
letter_content = letter_content.split("\n\nStudent Details:")[0]
output_file = f"{data['user'].replace(' ', '_')}_leave_letter.pdf"
pdf_data = pdf.output(dest='S').encode('latin1')
# Store everything in session state
if 'pdf_generated' not in st.session_state:
st.session_state.pdf_data = pdf_data
st.session_state.pdf_filename = output_file
st.session_state.user_data = data
st.session_state.pdf_generated = True
st.session_state.generation_time = time()
col1, col2 = st.columns(2)
if data.get('template') == "AI-generated":
col1, col2, col3 = st.columns(3)
else:
col1, col2 = st.columns(2)
with col1:
st.download_button(
"📥 Download Letter",
st.session_state.pdf_data,
file_name=st.session_state.pdf_filename,
mime="application/pdf",
key="download_btn"
)
with col2:
if st.button("📧 Send to Copy Shop", key="email_btn"):
success = send_to_copy_shop(
st.session_state.pdf_data,
st.session_state.pdf_filename,
st.session_state.user_data['user'],
st.session_state.user_data['department']
)
if success:
st.success("✅ PDF sent to copy shop successfully!")
else:
st.error("❌ Failed to send PDF to copy shop")
# Modify the regenerate button section inside generate_leave_letter function
if data.get('template') == "AI-generated":
with col3:
if st.button("🔄 Regenerate Letter", key="regenerate_btn"):
new_letter_content = generate_ai_leave_letter(data, faculty_df)
# Create new PDF with the regenerated content
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.multi_cell(0, 8, clean_text(new_letter_content))
# Add table if there are additional students
if 'additional_students' in data:
pdf.ln(10)
pdf.set_font("Arial", 'B', 12)
pdf.cell(0, 10, "Student Details:", ln=True)
pdf.ln(5)
# Table settings
col_widths = [70, 60, 60]
row_height = 10
pdf.set_font("Arial", 'B', 10)
pdf.set_fill_color(200, 200, 200)
pdf.cell(col_widths[0], row_height, "Name", 1, 0, 'C', 1)
pdf.cell(col_widths[1], row_height, "Year of Study", 1, 0, 'C', 1)
pdf.cell(col_widths[2], row_height, "Signature", 1, 1, 'C', 1)
pdf.set_font("Arial", '', 10)
pdf.cell(col_widths[0], row_height, data['user'], 1, 0, 'L')
pdf.cell(col_widths[1], row_height, data['year_of_study'], 1, 0, 'C')
sig_cell_y = pdf.get_y()
pdf.cell(col_widths[2], row_height, "", 1, 1, 'C')
# Add main signature if provided
if signature_path:
signature_img = Image.open(signature_path)
signature_img = signature_img.resize((50, 20), Image.LANCZOS)
temp_path = "temp_signature.png"
signature_img.save(temp_path)
pdf.image(temp_path, x=pdf.get_x() + 145, y=sig_cell_y, w=30, h=8)
os.remove(temp_path)
# Add additional students
for student in data['additional_students']:
pdf.cell(col_widths[0], row_height, student['name'], 1, 0, 'L')
pdf.cell(col_widths[1], row_height, student['year'], 1, 0, 'C')
sig_cell_y = pdf.get_y()
pdf.cell(col_widths[2], row_height, "", 1, 1, 'C')
# Add signature if available
if 'additional_signatures' in data and student['name'] in data['additional_signatures']:
sig_path = data['additional_signatures'][student['name']]
if sig_path:
sig_img = Image.open(sig_path)
sig_img = sig_img.resize((50, 20), Image.LANCZOS)
temp_path = f"temp_sig_{student['name']}.png"
sig_img.save(temp_path)
pdf.image(temp_path, x=pdf.get_x() + 145, y=sig_cell_y, w=30, h=8)
os.remove(temp_path)
# Update session state with new PDF
st.session_state.pdf_data = pdf.output(dest='S').encode('latin1')
st.rerun()
# timer display
remaining_time = 180 - int(time() - st.session_state.generation_time)
if remaining_time > 0:
st.info(f"⏳ Session expires in: {remaining_time} seconds")
st.info("⚠️ AI can make mistakes. Please review the letter before sending.")
st.info("⚠️ Regenerate for a letter with new content")
else:
st.warning("⚠️ Session expired! Redirecting to start...")
reset_app()
return
# status indicators
if 'download_complete' in st.session_state:
st.success("✅ Letter downloaded successfully!")
if 'email_sent' in st.session_state:
st.success("✅ Letter sent to copy shop!")
def send_to_copy_shop(pdf_data, filename, student_name, department):
try:
GMAIL_USER = os.getenv('GMAIL_USER')
GMAIL_PASSWORD = os.getenv('GMAIL_APP_PASSWORD')
COPY_SHOP_EMAIL = os.getenv('COPY_SHOP_EMAIL')
# Create message
message = MIMEMultipart()
message['From'] = GMAIL_USER
message['To'] = COPY_SHOP_EMAIL
message['Subject'] = f'Leave Letter - {student_name} ({department})'
body = f'Please find attached the leave letter for {student_name} from {department}.'
message.attach(MIMEText(body, 'plain'))
pdf_attachment = MIMEApplication(pdf_data, _subtype='pdf')
pdf_attachment.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(pdf_attachment)
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(GMAIL_USER, GMAIL_PASSWORD)
server.send_message(message)
server.quit()
return True
except Exception as e:
st.error(f"Error sending email: {str(e)}")
return False
def reset_app():
"""Helper function to reset the application state"""
for key in list(st.session_state.keys()):
del st.session_state[key]
st.rerun()
def main():
if 'pdf_generated' not in st.session_state:
faculty_df = load_faculty_list()
leave_data, signature_path = chat_interface()
if leave_data:
generate_leave_letter(leave_data, load_templates(), faculty_df, signature_path)
else:
generate_leave_letter(
st.session_state.user_data,
load_templates(),
load_faculty_list(),
None
)
if __name__ == "__main__":
main()