Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,204 +1,157 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import os
|
4 |
-
import io
|
5 |
import json
|
6 |
-
from datetime import datetime
|
7 |
-
|
8 |
-
import streamlit as st
|
9 |
import requests
|
|
|
|
|
10 |
from docx import Document
|
11 |
from PyPDF2 import PdfReader
|
12 |
|
13 |
# ------------------------------------------------------------------------------
|
14 |
-
#
|
15 |
# ------------------------------------------------------------------------------
|
16 |
-
|
17 |
-
# Your Groq API key is expected to be saved as a secret in the environment
|
18 |
GROQ_API_KEY = os.getenv("GROQ_API")
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
headers = {
|
27 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
28 |
"Content-Type": "application/json"
|
29 |
}
|
30 |
payload = {
|
31 |
-
"
|
32 |
-
"
|
|
|
|
|
33 |
}
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
return response.json()
|
38 |
-
|
39 |
-
st.error(f"
|
40 |
-
return
|
41 |
|
42 |
# ------------------------------------------------------------------------------
|
43 |
-
#
|
44 |
# ------------------------------------------------------------------------------
|
45 |
-
|
46 |
-
|
47 |
-
"""Extract text from a DOCX file."""
|
48 |
-
document = Document(io.BytesIO(file_bytes))
|
49 |
-
return "\n".join([para.text for para in document.paragraphs])
|
50 |
-
|
51 |
-
def read_pdf(file_bytes: bytes) -> str:
|
52 |
-
"""Extract text from a PDF file."""
|
53 |
-
reader = PdfReader(io.BytesIO(file_bytes))
|
54 |
-
text = []
|
55 |
-
for page in reader.pages:
|
56 |
-
extracted = page.extract_text()
|
57 |
-
if extracted:
|
58 |
-
text.append(extracted)
|
59 |
-
return "\n".join(text)
|
60 |
-
|
61 |
-
def read_plain(file_bytes: bytes) -> str:
|
62 |
-
"""Extract text from a plain text file."""
|
63 |
-
return file_bytes.decode("utf-8", errors="ignore")
|
64 |
-
|
65 |
-
def extract_resume_text(file_obj) -> str:
|
66 |
-
"""
|
67 |
-
Determine file type and extract text accordingly.
|
68 |
-
Supports PDF, DOCX, and plain text.
|
69 |
-
"""
|
70 |
file_bytes = file_obj.read()
|
71 |
-
file_obj.seek(0)
|
72 |
-
|
73 |
-
ext = os.path.splitext(filename)[-1].lower()
|
74 |
|
75 |
if ext == ".pdf":
|
76 |
-
return
|
77 |
elif ext in [".docx", ".doc"]:
|
78 |
-
return
|
79 |
else:
|
80 |
-
return
|
81 |
|
82 |
# ------------------------------------------------------------------------------
|
83 |
-
#
|
84 |
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
""
|
92 |
-
|
93 |
-
"You are
|
94 |
-
"
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
"Job Description:\n{job_description}\n\n"
|
112 |
-
"Cover Letter:".format(
|
113 |
-
candidate_json=candidate_json, job_description=job_description, date=date_str
|
114 |
-
)
|
115 |
-
)
|
116 |
-
return generate_text(prompt, max_length=1024)
|
117 |
-
|
118 |
-
def generate_resume(first_name: str, last_name: str, location: str,
|
119 |
-
work_experience: str, school_experience: str, skills: str) -> str:
|
120 |
-
"""
|
121 |
-
Generate a professional resume using candidate information provided in a form.
|
122 |
-
"""
|
123 |
-
candidate_info = {
|
124 |
"first_name": first_name,
|
125 |
"last_name": last_name,
|
126 |
"location": location,
|
127 |
"work_experience": work_experience,
|
128 |
"school_experience": school_experience,
|
129 |
-
"skills": skills
|
130 |
-
}
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
"
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
# ------------------------------------------------------------------------------
|
142 |
-
#
|
143 |
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
144 |
|
145 |
-
st.
|
146 |
-
st.title("AI-Powered Job Application Assistant")
|
147 |
-
st.markdown("Generate a **Cover Letter** from your resume or **Create a Resume** from scratch using AI via Groq API.")
|
148 |
|
149 |
-
#
|
150 |
-
tabs = st.tabs(["Cover Letter Generator", "Resume Creator"])
|
151 |
-
|
152 |
-
# ----- Tab 1: Cover Letter Generator -----
|
153 |
with tabs[0]:
|
154 |
-
st.header("Cover Letter Generator")
|
155 |
-
st.
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
with col2:
|
163 |
-
job_description = st.text_area("Job Description", height=200, placeholder="Paste the job description here...")
|
164 |
-
|
165 |
-
if st.button("Generate Cover Letter"):
|
166 |
-
if resume_file is None or not job_description.strip():
|
167 |
-
st.error("Please upload a resume and enter a job description.")
|
168 |
-
else:
|
169 |
-
with st.spinner("Processing resume..."):
|
170 |
resume_text = extract_resume_text(resume_file)
|
171 |
candidate_json = parse_resume(resume_text)
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
st.
|
177 |
-
|
178 |
-
|
179 |
-
st.text_area("", cover_letter, height=300)
|
180 |
-
|
181 |
-
# ----- Tab 2: Resume Creator -----
|
182 |
with tabs[1]:
|
183 |
-
st.header("Resume Creator")
|
184 |
-
|
185 |
-
|
186 |
with st.form("resume_form"):
|
187 |
col1, col2 = st.columns(2)
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
skills = st.text_area("Skills", height=100, placeholder="List your key skills...")
|
197 |
-
|
198 |
-
submit = st.form_submit_button("Generate Resume")
|
199 |
-
|
200 |
if submit:
|
201 |
-
with st.spinner("
|
202 |
resume_text = generate_resume(first_name, last_name, location, work_experience, school_experience, skills)
|
203 |
-
st.
|
204 |
-
st.text_area("", resume_text, height=400)
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import json
|
|
|
|
|
|
|
3 |
import requests
|
4 |
+
import streamlit as st
|
5 |
+
from datetime import datetime
|
6 |
from docx import Document
|
7 |
from PyPDF2 import PdfReader
|
8 |
|
9 |
# ------------------------------------------------------------------------------
|
10 |
+
# π CONFIGURATION - API & SETTINGS
|
11 |
# ------------------------------------------------------------------------------
|
|
|
|
|
12 |
GROQ_API_KEY = os.getenv("GROQ_API")
|
13 |
+
GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
|
14 |
+
GROQ_MODEL = "llama-3.3-70b-versatile"
|
15 |
|
16 |
+
# ------------------------------------------------------------------------------
|
17 |
+
# π₯ ADVANCED API CALL FUNCTION
|
18 |
+
# ------------------------------------------------------------------------------
|
19 |
+
def call_groq_api(messages, temperature=0.7):
|
20 |
+
"""Handles API calls to Groq for chat-based completions with robust error handling."""
|
21 |
headers = {
|
22 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
23 |
"Content-Type": "application/json"
|
24 |
}
|
25 |
payload = {
|
26 |
+
"model": GROQ_MODEL,
|
27 |
+
"messages": messages,
|
28 |
+
"temperature": temperature,
|
29 |
+
"max_tokens": 1024
|
30 |
}
|
31 |
+
try:
|
32 |
+
response = requests.post(GROQ_ENDPOINT, headers=headers, json=payload, timeout=15)
|
33 |
+
response.raise_for_status()
|
34 |
+
return response.json()["choices"][0]["message"]["content"].strip()
|
35 |
+
except requests.exceptions.RequestException as e:
|
36 |
+
st.error(f"β API Request Failed: {e}")
|
37 |
+
return None
|
38 |
|
39 |
# ------------------------------------------------------------------------------
|
40 |
+
# π FILE PROCESSING (PDF / DOCX)
|
41 |
# ------------------------------------------------------------------------------
|
42 |
+
def extract_resume_text(file_obj):
|
43 |
+
"""Extracts text from PDF or DOCX resumes."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
file_bytes = file_obj.read()
|
45 |
+
file_obj.seek(0)
|
46 |
+
ext = os.path.splitext(file_obj.name)[-1].lower()
|
|
|
47 |
|
48 |
if ext == ".pdf":
|
49 |
+
return "\n".join(page.extract_text() for page in PdfReader(file_bytes).pages if page.extract_text())
|
50 |
elif ext in [".docx", ".doc"]:
|
51 |
+
return "\n".join([para.text for para in Document(file_bytes).paragraphs])
|
52 |
else:
|
53 |
+
return file_bytes.decode("utf-8", errors="ignore")
|
54 |
|
55 |
# ------------------------------------------------------------------------------
|
56 |
+
# π― AI-Powered Resume Parsing
|
57 |
# ------------------------------------------------------------------------------
|
58 |
+
def parse_resume(resume_text):
|
59 |
+
"""Extracts structured JSON resume details using Groq's Llama 3.3 model."""
|
60 |
+
messages = [
|
61 |
+
{"role": "system", "content": "You are a resume parsing expert. Extract structured data."},
|
62 |
+
{"role": "user", "content": f"Extract structured data from this resume:\n{resume_text}\n\nOutput as JSON."}
|
63 |
+
]
|
64 |
+
return call_groq_api(messages, temperature=0.4)
|
65 |
|
66 |
+
# ------------------------------------------------------------------------------
|
67 |
+
# βοΈ AI-Powered Cover Letter Generator
|
68 |
+
# ------------------------------------------------------------------------------
|
69 |
+
def generate_cover_letter(candidate_json, job_description):
|
70 |
+
"""Generates a professional cover letter using structured candidate data."""
|
71 |
+
date_str = datetime.today().strftime("%d - %b - %Y")
|
72 |
+
messages = [
|
73 |
+
{"role": "system", "content": "You are a top-tier career advisor. Write persuasive cover letters."},
|
74 |
+
{"role": "user", "content": f"""
|
75 |
+
Generate a professional cover letter using:
|
76 |
+
- Candidate Profile: {candidate_json}
|
77 |
+
- Job Description: {job_description}
|
78 |
+
- Date: {date_str}
|
79 |
+
|
80 |
+
The cover letter should be engaging, personalized, and formatted professionally.
|
81 |
+
"""}
|
82 |
+
]
|
83 |
+
return call_groq_api(messages, temperature=0.5)
|
84 |
+
|
85 |
+
# ------------------------------------------------------------------------------
|
86 |
+
# π AI-Powered Resume Creator
|
87 |
+
# ------------------------------------------------------------------------------
|
88 |
+
def generate_resume(first_name, last_name, location, work_experience, school_experience, skills):
|
89 |
+
"""Generates a well-formatted professional resume."""
|
90 |
+
candidate_data = json.dumps({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
"first_name": first_name,
|
92 |
"last_name": last_name,
|
93 |
"location": location,
|
94 |
"work_experience": work_experience,
|
95 |
"school_experience": school_experience,
|
96 |
+
"skills": skills
|
97 |
+
}, indent=2)
|
98 |
+
|
99 |
+
messages = [
|
100 |
+
{"role": "system", "content": "You are a professional resume writer."},
|
101 |
+
{"role": "user", "content": f"""
|
102 |
+
Create a structured, ATS-friendly resume using:
|
103 |
+
{candidate_data}
|
104 |
+
|
105 |
+
Ensure:
|
106 |
+
- Clear sections (Personal Info, Experience, Education, Skills)
|
107 |
+
- Professional formatting
|
108 |
+
- A compelling summary if possible.
|
109 |
+
"""}
|
110 |
+
]
|
111 |
+
return call_groq_api(messages, temperature=0.5)
|
112 |
|
113 |
# ------------------------------------------------------------------------------
|
114 |
+
# π¨ STREAMLIT UI DESIGN
|
115 |
# ------------------------------------------------------------------------------
|
116 |
+
st.set_page_config(page_title="AI-Powered Job Assistant", layout="wide")
|
117 |
+
st.title("π AI-Powered Resume & Cover Letter Generator")
|
118 |
+
st.markdown("#### Generate a **Resume** or a **Cover Letter** using **Llama 3.3-70B** powered by **Groq AI**.")
|
119 |
|
120 |
+
tabs = st.tabs(["π Cover Letter Generator", "π Resume Creator"])
|
|
|
|
|
121 |
|
122 |
+
# ----- COVER LETTER GENERATOR -----
|
|
|
|
|
|
|
123 |
with tabs[0]:
|
124 |
+
st.header("π Cover Letter Generator")
|
125 |
+
resume_file = st.file_uploader("Upload Your Resume", type=["pdf", "docx", "txt"])
|
126 |
+
job_description = st.text_area("Paste Job Description", height=200)
|
127 |
+
|
128 |
+
if st.button("πΉ Generate Cover Letter"):
|
129 |
+
if resume_file and job_description.strip():
|
130 |
+
with st.spinner("β¨ Processing resume..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
resume_text = extract_resume_text(resume_file)
|
132 |
candidate_json = parse_resume(resume_text)
|
133 |
+
cover_letter = generate_cover_letter(candidate_json, job_description)
|
134 |
+
st.success("β
Cover Letter Generated!")
|
135 |
+
st.text_area("π Your Cover Letter:", cover_letter, height=300)
|
136 |
+
else:
|
137 |
+
st.warning("β οΈ Please upload a resume and paste the job description.")
|
138 |
+
|
139 |
+
# ----- RESUME CREATOR -----
|
|
|
|
|
|
|
140 |
with tabs[1]:
|
141 |
+
st.header("π Resume Creator")
|
142 |
+
|
|
|
143 |
with st.form("resume_form"):
|
144 |
col1, col2 = st.columns(2)
|
145 |
+
first_name = col1.text_input("First Name")
|
146 |
+
last_name = col2.text_input("Last Name")
|
147 |
+
location = st.text_input("Location")
|
148 |
+
work_experience = st.text_area("Work Experience", height=150)
|
149 |
+
school_experience = st.text_area("Education", height=150)
|
150 |
+
skills = st.text_area("Skills", height=100)
|
151 |
+
submit = st.form_submit_button("π Generate Resume")
|
152 |
+
|
|
|
|
|
|
|
|
|
153 |
if submit:
|
154 |
+
with st.spinner("π Creating Resume..."):
|
155 |
resume_text = generate_resume(first_name, last_name, location, work_experience, school_experience, skills)
|
156 |
+
st.success("β
Resume Generated!")
|
157 |
+
st.text_area("π Your Resume:", resume_text, height=400)
|