File size: 2,752 Bytes
cdfb101
 
 
7fcd5a3
0851006
 
6ba394c
cdfb101
 
 
 
 
 
 
0851006
cdfb101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0851006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdfb101
 
 
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
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