Coder19/interview_system
0
1from openai import OpenAI2from fpdf import FPDF3from io import BytesIO4import re5from dotenv import load_dotenv6import os7 8# Load environment variables from .env file9load_dotenv()10 11SAMBANOVA_API_KEY = os.getenv("SAMBANOVA_API_KEY")12SAMBANOVA_BASE_URL = os.getenv("SAMBANOVA_BASE_URL") # e.g., "https://api.sambanova.ai/v1"13 14if not SAMBANOVA_API_KEY or not SAMBANOVA_BASE_URL:15 raise ValueError("SambaNova API key or base URL not found in environment variables")16 17# Initialize SambaNova client18try:19 client = OpenAI(api_key=SAMBANOVA_API_KEY, base_url=SAMBANOVA_BASE_URL)20except Exception as e:21 raise RuntimeError(f"Failed to initialize SambaNova client: {str(e)}")22 23 24# Function to generate resume text from a prompt using SambaNova25def generate_resume_from_prompt(prompt: str) -> str:26 system_prompt = """27You are a professional resume generator. Given a prompt, return a resume with these sections:28- Name29- Contact Info30- Objective31- Skills32- Experience33- Education34- Projects35Each section should be clearly labeled and contain relevant content.36"""37 response = client.chat.completions.create(38 model="Llama-4-Maverick-17B-128E-Instruct",39 messages=[40 {"role": "system", "content": system_prompt},41 {"role": "user", "content": prompt}42 ]43 )44 45 return response.choices[0].message.content46 47 48# Function to parse resume text into a structured dictionary49def parse_resume_text(resume_text):50 # Adjusted regex to allow for extra spaces, colons, dashes51 section_pattern = re.compile(r'^(Name|Contact Info|Objective|Skills|Experience|Education|Projects)\s*[:\-]?\s*$', re.IGNORECASE)52 resume_dict = {}53 current_section = None54 lines = resume_text.splitlines()55 56 for line in lines:57 line = line.strip()58 if not line:59 continue60 61 match = section_pattern.match(line)62 if match:63 current_section = match.group(1).title()64 resume_dict[current_section] = ""65 elif current_section:66 resume_dict[current_section] += line + "\n"67 68 return resume_dict69 70 71# Canva-style resume PDF class72class ResumePDF(FPDF):73 def header(self):74 # Name75 self.set_font("Helvetica", "B", 24)76 self.set_text_color(33, 37, 41)77 self.cell(0, 12, self.name, ln=True, align="C")78 79 # Contact Info80 self.set_font("Helvetica", "", 12)81 self.set_text_color(100, 100, 100)82 self.cell(0, 10, self.contact, ln=True, align="C")83 84 # Divider85 self.set_draw_color(200, 200, 200)86 self.set_line_width(0.5)87 self.line(10, 38, 200, 38)88 self.ln(10)89 90 def section_title(self, title: str):91 self.set_font("Helvetica", "B", 14)92 self.set_text_color(52, 58, 64)93 self.set_fill_color(240, 240, 240)94 self.cell(0, 10, title.upper(), ln=True, fill=True)95 96 def section_body(self, content: str):97 self.set_font("Helvetica", "", 11)98 self.set_text_color(33, 37, 41)99 lines = content.strip().split('\n')100 for line in lines:101 if line.strip():102 self.cell(5)103 self.multi_cell(0, 8, f"- {line.strip()}")104 105 self.ln(3)106 107 def section(self, title: str, content: str):108 self.section_title(title)109 self.section_body(content)110 111 def build_resume(self, resume_dict: dict):112 self.name = resume_dict.get("Name", "John Doe")113 self.contact = resume_dict.get("Contact Info", "email@example.com | +1 234 567 890")114 self.add_page()115 116 for section in ["Objective", "Skills", "Experience", "Education", "Projects"]:117 if section in resume_dict:118 self.section(section, resume_dict[section])119 120 self.set_auto_page_break(auto=True, margin=15)121 122 123# Function to generate and save the PDF124def create_resume_pdf(prompt: str) -> BytesIO:125 resume_text = generate_resume_from_prompt(prompt)126 resume_data = parse_resume_text(resume_text)127 128 pdf = ResumePDF()129 pdf.build_resume(resume_data)130 131 pdf_bytes = pdf.output(dest='S').encode('latin1') # FPDF returns a string, encode to bytes132 buffer = BytesIO(pdf_bytes)133 buffer.seek(0)134 135 return buffer136 137 138# Example usage139if __name__ == "__main__":140 sample_prompt = "Generate a resume for a software engineer with 5 years of experience in Python, Django, and machine learning. He has worked at Google and Amazon, has a master's degree in computer science from MIT, and built several AI-based projects."141 pdf_buffer = create_resume_pdf(sample_prompt)142 with open("generated_resume.pdf", "wb") as f:143 f.write(pdf_buffer.read())144 print("✅ Resume PDF generated: generated_resume.pdf")