-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
235 lines (200 loc) · 7.97 KB
/
Copy pathapp.py
File metadata and controls
235 lines (200 loc) · 7.97 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# app.py
import os
import base64
from io import BytesIO
import streamlit as st
from PyPDF2 import PdfReader
from docx import Document
from prompts import generate_cover_letter_prompt
from hf_inference import query_llama3
# ---------------- PAGE CONFIG (FULL WIDTH) ----------------
st.set_page_config(
page_title="InkApply – AI Cover Letter Generator",
page_icon="Inkapply-logo.png" if os.path.exists("Inkapply-logo.png") else "📝",
layout="wide",
)
# ---------------- CACHE MODEL (anti-buffering fix) ----------------
@st.cache_resource(show_spinner="Loading AI model… (first run only)")
def get_llm():
return True
get_llm()
# ---------------- SESSION STATE DEFAULTS ----------------
st.session_state.setdefault("resume_bytes", None)
st.session_state.setdefault("resume_content", "")
st.session_state.setdefault("uploaded_file_name", "")
st.session_state.setdefault("cover_letter", "")
# ---------------- GLOBAL STYLING (LEFT, FULL-WIDTH) ----------------
st.markdown(
"""
<style>
.block-container {
padding-top: 2.75rem !important;
max-width: 100% !important;
padding-left: 2.5rem !important;
padding-right: 2.5rem !important;
}
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
html, body, [class*="css"] { font-family: 'Inter', sans-serif; }
section[data-testid="stSidebar"] { padding-top: 1rem; }
.sidebar-logo { display: flex; justify-content: center; padding: 0.5rem 0 1rem 0; }
.sidebar-logo img { border-radius: 50%; width: 88px; height: 88px; object-fit: cover; border: 1px solid #2a2a2a; }
textarea, input {
background-color: #1f1f1f !important;
border: 1px solid #2b2b2b !important;
border-radius: 12px !important;
padding: 0.75rem !important;
color: #ffffff !important;
}
/* Hide Streamlit branding */
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
textarea::placeholder, input::placeholder { color: #9aa0a6 !important; }
textarea:focus, input:focus {
border-color: #10A37F !important;
box-shadow: 0 0 0 1px rgba(16,163,127,0.45);
outline: none !important;
}
.ink-nb {
margin-top: 0.5rem;
font-size: 0.78rem;
color: #9aa0a6;
font-style: italic;
}
.ink-nb span {
color: #f0a500;
font-weight: 600;
font-style: normal;
}
</style>
""",
unsafe_allow_html=True,
)
# ---------------- SIDEBAR LOGO ----------------
logo_path = "Inkapply-logo.png"
if os.path.exists(logo_path):
with open(logo_path, "rb") as f:
logo_base64 = base64.b64encode(f.read()).decode()
st.sidebar.markdown(
f'<div class="sidebar-logo"><img src="data:image/png;base64,{logo_base64}" /></div>',
unsafe_allow_html=True
)
else:
st.sidebar.markdown("### InkApply")
st.sidebar.markdown("<p style='color:#9aa0a6; text-align:center;'>AI Resume & Cover Letter Generator</p>", unsafe_allow_html=True)
# ---------------- HERO ----------------
st.markdown(
"<h3 style='margin-bottom:0.35rem;'>AI Cover Letter Generator</h3>"
"<p style='color:#9aa0a6; margin-top:0;'>Create tailored cover letters in seconds.</p>",
unsafe_allow_html=True,
)
# ---------------- INPUTS ----------------
job_title = st.text_input("", placeholder="Job title (e.g. Senior Embedded Software Engineer)")
job_description = st.text_area("", placeholder="Paste the job description here (optional but recommended)", height=160)
# ---------------- FILE PARSER ----------------
def parse_uploaded_file(file_bytes: bytes, filename: str) -> str:
"""Return text from PDF, DOCX, or TXT in a Streamlit-safe way."""
try:
fn = filename.lower()
if fn.endswith(".pdf"):
reader = PdfReader(BytesIO(file_bytes))
text = [page.extract_text() for page in reader.pages if page.extract_text()]
return "\n".join(text).strip()
if fn.endswith(".docx"):
doc = Document(BytesIO(file_bytes))
text = [p.text for p in doc.paragraphs if p.text.strip()]
return "\n".join(text).strip()
if fn.endswith(".txt"):
return file_bytes.decode("utf-8", errors="ignore").strip()
return ""
except Exception as e:
st.error(f"Failed to read file: {e}")
return ""
# ---------------- FILE UPLOADER (with KEY) ----------------
uploaded_file = st.file_uploader(
"Upload your resume (PDF preferred, DOCX/TXT supported)",
type=["pdf", "docx", "txt"],
key="resume_uploader",
)
if uploaded_file is not None:
try:
file_bytes = uploaded_file.read()
st.session_state.resume_bytes = file_bytes
st.session_state.uploaded_file_name = uploaded_file.name
parsed_text = parse_uploaded_file(file_bytes, uploaded_file.name)
if parsed_text:
st.session_state.resume_content = parsed_text
st.success(f"Resume '{uploaded_file.name}' loaded successfully!")
else:
st.warning("Could not extract text. Please paste your resume below.")
except Exception as e:
st.error(f"Upload failed: {e}")
if uploaded_file is None and st.session_state.get("resume_bytes") and not st.session_state.get("resume_content"):
parsed_text = parse_uploaded_file(st.session_state.resume_bytes, st.session_state.uploaded_file_name or "resume")
if parsed_text:
st.session_state.resume_content = parsed_text
if st.session_state.get("uploaded_file_name"):
st.info(f"Uploaded: {st.session_state.uploaded_file_name}")
# ---------------- MANUAL RESUME INPUT (preview + editable) ----------------
new_text = st.text_area(
label=(
f"Resume content (from uploaded file: {st.session_state.uploaded_file_name})"
if st.session_state.uploaded_file_name else "Resume content"
),
value=st.session_state.get("resume_content", ""),
placeholder="Or paste your resume content here…",
height=260,
key="resume_textarea",
)
if new_text != st.session_state.get("resume_content", ""):
st.session_state.resume_content = new_text
# ---------------- GENERATE COVER LETTER ----------------
if st.button("Generate cover letter ✨"):
if not job_title.strip():
st.warning("Please enter a job title.")
elif not st.session_state.get("resume_content", "").strip():
st.warning("Please upload or paste your resume.")
else:
trimmed_resume = " ".join(st.session_state.resume_content.split()[:300])
trimmed_desc = " ".join((job_description or "").split()[:200])
prompt = generate_cover_letter_prompt(
job_title.strip(),
trimmed_desc,
trimmed_resume,
)
with st.spinner("Generating your cover letter…"):
try:
generated_text = query_llama3(prompt)
st.session_state.cover_letter = generated_text.strip()
except Exception as e:
st.error(f"Generation failed: {e}")
# ---------------- N.B NOTE ----------------
st.markdown(
"<p class='ink-nb'><span>•</span> N.B Review the cover letter output and regenerate if any errors occurred "
"before downloading. InkApply can make mistakes.</p>",
unsafe_allow_html=True,
)
# ---------------- RESULT (persists across reruns) ----------------
if st.session_state.cover_letter:
st.markdown("### Your cover letter")
st.text_area("", value=st.session_state.cover_letter, height=360)
# TXT download
st.download_button(
"Download TXT",
st.session_state.cover_letter,
"cover_letter.txt",
"text/plain",
)
# DOCX download
doc = Document()
doc.add_heading(f"Cover Letter – {job_title}", 0)
doc.add_paragraph(st.session_state.cover_letter)
buffer = BytesIO()
doc.save(buffer)
buffer.seek(0)
st.download_button(
"Download Word (.docx)",
buffer,
"cover_letter.docx",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)