mgbam commited on
Commit
b82d535
·
verified ·
1 Parent(s): 32b9cc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -6,19 +6,38 @@ import json
6
  from datetime import datetime
7
 
8
  import streamlit as st
9
- from transformers import pipeline
10
  from docx import Document
11
  from PyPDF2 import PdfReader
12
 
13
  # ------------------------------------------------------------------------------
14
- # Model Pipeline Setup
15
  # ------------------------------------------------------------------------------
16
 
17
- # Use an instruction-tuned model (e.g., Flan-T5-xl)
18
- model_name = "google/flan-t5-xl"
19
- instruct_pipeline = pipeline(
20
- "text2text-generation", model=model_name, max_length=1024, truncation=True
21
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # ------------------------------------------------------------------------------
24
  # Helper Functions for File Reading
@@ -49,7 +68,7 @@ def extract_resume_text(file_obj) -> str:
49
  Supports PDF, DOCX, and plain text.
50
  """
51
  file_bytes = file_obj.read()
52
- file_obj.seek(0) # Reset pointer for potential further use
53
  filename = file_obj.name if hasattr(file_obj, "name") else "resume.txt"
54
  ext = os.path.splitext(filename)[-1].lower()
55
 
@@ -61,14 +80,14 @@ def extract_resume_text(file_obj) -> str:
61
  return read_plain(file_bytes)
62
 
63
  # ------------------------------------------------------------------------------
64
- # Core AI Functions
65
  # ------------------------------------------------------------------------------
66
 
67
  def parse_resume(resume_text: str) -> str:
68
  """
69
  Extract candidate details from the resume text.
70
- The model returns a JSON with keys: first_name, last_name, location,
71
- work_experience, school_experience, and skills.
72
  """
73
  prompt = (
74
  "You are an expert resume parser. Extract the following keys from the resume text: "
@@ -76,8 +95,7 @@ def parse_resume(resume_text: str) -> str:
76
  "Return the answer strictly in JSON format with no extra text.\n\n"
77
  f"Resume:\n{resume_text}"
78
  )
79
- result = instruct_pipeline(prompt, max_length=512)[0]["generated_text"]
80
- return result.strip()
81
 
82
  def generate_cover_letter(candidate_json: str, job_description: str, date_str: str) -> str:
83
  """
@@ -95,8 +113,7 @@ def generate_cover_letter(candidate_json: str, job_description: str, date_str: s
95
  candidate_json=candidate_json, job_description=job_description, date=date_str
96
  )
97
  )
98
- result = instruct_pipeline(prompt, max_length=1024)[0]["generated_text"]
99
- return result.strip()
100
 
101
  def generate_resume(first_name: str, last_name: str, location: str,
102
  work_experience: str, school_experience: str, skills: str) -> str:
@@ -119,8 +136,7 @@ def generate_resume(first_name: str, last_name: str, location: str,
119
  "personal details, work experience, education, and skills. Ensure the resume is concise and professional.\n\n"
120
  "Candidate Information:\n" + candidate_str + "\n\nResume:"
121
  )
122
- result = instruct_pipeline(prompt, max_length=1024)[0]["generated_text"]
123
- return result.strip()
124
 
125
  # ------------------------------------------------------------------------------
126
  # Streamlit UI
@@ -128,7 +144,7 @@ def generate_resume(first_name: str, last_name: str, location: str,
128
 
129
  st.set_page_config(page_title="AI-Powered Job Application Assistant", layout="wide")
130
  st.title("AI-Powered Job Application Assistant")
131
- st.markdown("Generate a **Cover Letter** from your resume or **Create a Resume** from scratch using AI.")
132
 
133
  # Create tabs for the two functionalities
134
  tabs = st.tabs(["Cover Letter Generator", "Resume Creator"])
 
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
+ # Groq API Setup
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
+ # Replace the following endpoint with the actual Groq API endpoint if different.
20
+ GROQ_ENDPOINT = "https://api.groq.ai/v1/generate"
21
+
22
+ def generate_text(prompt: str, max_length: int = 1024) -> str:
23
+ """
24
+ Call the Groq API with a prompt and return the generated text.
25
+ """
26
+ headers = {
27
+ "Authorization": f"Bearer {GROQ_API_KEY}",
28
+ "Content-Type": "application/json"
29
+ }
30
+ payload = {
31
+ "prompt": prompt,
32
+ "max_length": max_length
33
+ }
34
+ response = requests.post(GROQ_ENDPOINT, headers=headers, json=payload)
35
+ if response.ok:
36
+ # Adjust according to the actual API response structure.
37
+ return response.json().get("generated_text", "").strip()
38
+ else:
39
+ st.error(f"Error in Groq API call: {response.text}")
40
+ return ""
41
 
42
  # ------------------------------------------------------------------------------
43
  # Helper Functions for File Reading
 
68
  Supports PDF, DOCX, and plain text.
69
  """
70
  file_bytes = file_obj.read()
71
+ file_obj.seek(0) # Reset pointer for further use if needed
72
  filename = file_obj.name if hasattr(file_obj, "name") else "resume.txt"
73
  ext = os.path.splitext(filename)[-1].lower()
74
 
 
80
  return read_plain(file_bytes)
81
 
82
  # ------------------------------------------------------------------------------
83
+ # Core AI Functions Using Groq API
84
  # ------------------------------------------------------------------------------
85
 
86
  def parse_resume(resume_text: str) -> str:
87
  """
88
  Extract candidate details from the resume text.
89
+ The prompt instructs the model to output a JSON with keys:
90
+ first_name, last_name, location, work_experience, school_experience, and skills.
91
  """
92
  prompt = (
93
  "You are an expert resume parser. Extract the following keys from the resume text: "
 
95
  "Return the answer strictly in JSON format with no extra text.\n\n"
96
  f"Resume:\n{resume_text}"
97
  )
98
+ return generate_text(prompt, max_length=512)
 
99
 
100
  def generate_cover_letter(candidate_json: str, job_description: str, date_str: str) -> str:
101
  """
 
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:
 
136
  "personal details, work experience, education, and skills. Ensure the resume is concise and professional.\n\n"
137
  "Candidate Information:\n" + candidate_str + "\n\nResume:"
138
  )
139
+ return generate_text(prompt, max_length=1024)
 
140
 
141
  # ------------------------------------------------------------------------------
142
  # Streamlit UI
 
144
 
145
  st.set_page_config(page_title="AI-Powered Job Application Assistant", layout="wide")
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
  # Create tabs for the two functionalities
150
  tabs = st.tabs(["Cover Letter Generator", "Resume Creator"])