LovnishVerma commited on
Commit
f2baa0b
·
verified ·
1 Parent(s): 51c72d3

Update parser_logic.py

Browse files
Files changed (1) hide show
  1. parser_logic.py +27 -20
parser_logic.py CHANGED
@@ -12,12 +12,11 @@ logger = logging.getLogger(__name__)
12
 
13
  load_dotenv()
14
 
15
- # Secure Configuration
16
  api_key = os.getenv("GEMINI_API_KEY")
17
  if not api_key:
18
  raise ValueError("GEMINI_API_KEY is missing.")
19
 
20
- # Configure the STABLE SDK
21
  genai.configure(api_key=api_key)
22
 
23
  def extract_text_from_stream(file_bytes: bytes) -> str:
@@ -33,30 +32,38 @@ def extract_text_from_stream(file_bytes: bytes) -> str:
33
 
34
  def parse_resume_with_ai(resume_text: str) -> dict:
35
  prompt = """
36
- You are an expert resume parser. Extract the following information as strictly valid JSON:
37
  {
38
  "name": "Full Name",
39
  "email": "email@example.com",
40
  "phone": "123-456-7890",
41
  "skills": ["skill1", "skill2"],
42
- "summary": "Brief summary of the candidate."
43
  }
44
- If information is missing, use null. Do not add Markdown formatting (like ```json).
45
  """
46
 
47
- # We use 'gemini-1.5-flash' which is fully supported in the latest standard SDK
48
- model = genai.GenerativeModel('gemini-1.5-flash')
 
 
49
 
50
- try:
51
- response = model.generate_content(prompt + "\n\nResume Text:\n" + resume_text[:10000])
52
-
53
- # Cleaning: Remove markdown if the model adds it despite instructions
54
- raw_text = response.text.strip()
55
- clean_json = re.sub(r'```json\s*|```', '', raw_text, flags=re.MULTILINE).strip()
56
-
57
- return json.loads(clean_json)
58
-
59
- except Exception as e:
60
- logger.error(f"AI Processing Error: {e}")
61
- # Detailed error for debugging
62
- return {"error": f"AI Error: {str(e)}"}
 
 
 
 
 
 
 
12
 
13
  load_dotenv()
14
 
 
15
  api_key = os.getenv("GEMINI_API_KEY")
16
  if not api_key:
17
  raise ValueError("GEMINI_API_KEY is missing.")
18
 
19
+ # Configure the SDK
20
  genai.configure(api_key=api_key)
21
 
22
  def extract_text_from_stream(file_bytes: bytes) -> str:
 
32
 
33
  def parse_resume_with_ai(resume_text: str) -> dict:
34
  prompt = """
35
+ Extract the following information as strictly valid JSON:
36
  {
37
  "name": "Full Name",
38
  "email": "email@example.com",
39
  "phone": "123-456-7890",
40
  "skills": ["skill1", "skill2"],
41
+ "summary": "Brief summary"
42
  }
43
+ If information is missing, use null.
44
  """
45
 
46
+ # LIST OF MODELS TO TRY (In order)
47
+ # 1. Flash: Best balance of speed/cost
48
+ # 2. Pro: Older, but GUARANTEED to work on v1beta
49
+ models_to_try = ['gemini-1.5-flash', 'gemini-pro']
50
 
51
+ for model_name in models_to_try:
52
+ try:
53
+ logger.info(f"Attempting to use model: {model_name}")
54
+ model = genai.GenerativeModel(model_name)
55
+
56
+ response = model.generate_content(prompt + "\n\nResume Text:\n" + resume_text[:10000])
57
+
58
+ # Clean and Parse
59
+ raw_text = response.text.strip()
60
+ clean_json = re.sub(r'```json\s*|```', '', raw_text, flags=re.MULTILINE).strip()
61
+ return json.loads(clean_json)
62
+
63
+ except Exception as e:
64
+ logger.warning(f"Model {model_name} failed with error: {e}")
65
+ # If this was the last model in the list, raise the error
66
+ if model_name == models_to_try[-1]:
67
+ return {"error": f"All models failed. Last error: {str(e)}"}
68
+ # Otherwise, loop continues to the next model (gemini-pro)
69
+ continue