import os from dotenv import load_dotenv import json import google.genai as genai import time import logging load_dotenv() class Model: def __init__(self,PROMPT,model_inp): API_KEY = os.getenv("GOOGLE_API_KEY") self.client = genai.Client(api_key=API_KEY) self.chat = self.client.chats.create(model = model_inp) self.model_name = model_inp response = self.chat.send_message(PROMPT) def json(self,text): if not text.strip(): return "" s_idx = text.find("{") e_idx = text.rfind("}") + 1 if s_idx == -1 or e_idx == -1 : return "" try: output = json.loads(text[s_idx:e_idx]) except Exception as e: raise ValueError(f"Error Parsing {e}") return output if output else "" def send(self, content, max_retries=2): for attempt in range(max_retries + 1): try: start_time = time.time() logging.info(f"Sending request to {self.model_name} (attempt {attempt + 1})") response = self.chat.send_message(content) elapsed_time = time.time() - start_time logging.info(f"API call completed in {elapsed_time:.2f} seconds") result = self.json(response.text) return result except Exception as e: logging.error(f"API call failed (attempt {attempt + 1}): {str(e)}") if attempt == max_retries: # Return a basic structure if all retries fail logging.warning("All retries failed, returning basic structure") return { "name": "Unknown", "email": "unknown@example.com", "phone": None, "linkedin": None, "location": None, "summary": "Resume parsing failed", "education": [], "experience": [], "projects": [], "skills": { "languages": [], "frameworks": [], "libraries": [], "tools": [], "platforms": [], "design": [] }, "certifications": [], "awards": [] } time.sleep(2) # Wait before retry