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

Update parser_logic.py

Browse files
Files changed (1) hide show
  1. parser_logic.py +34 -33
parser_logic.py CHANGED
@@ -16,7 +16,7 @@ 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:
@@ -31,39 +31,40 @@ def extract_text_from_stream(file_bytes: bytes) -> str:
31
  return text
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
 
16
  if not api_key:
17
  raise ValueError("GEMINI_API_KEY is missing.")
18
 
19
+ # Configure SDK
20
  genai.configure(api_key=api_key)
21
 
22
  def extract_text_from_stream(file_bytes: bytes) -> str:
 
31
  return text
32
 
33
  def parse_resume_with_ai(resume_text: str) -> dict:
34
+
35
+ # DIAGNOSTIC STEP: Check what models are actually available
36
+ available_models = []
37
+ try:
38
+ for m in genai.list_models():
39
+ if 'generateContent' in m.supported_generation_methods:
40
+ available_models.append(m.name)
41
+ logger.info(f"AVAILABLE MODELS: {available_models}")
42
+ except Exception as e:
43
+ logger.error(f"Could not list models: {e}")
44
+ return {"error": f"API Key Authentication Failed completely. Google says: {str(e)}"}
45
+
46
+ # If the list is empty, the Key is invalid or has no permissions
47
+ if not available_models:
48
+ return {"error": "CRITICAL: The API Key is valid, but Google returned 0 available models. Check your Google AI Studio account."}
49
+
50
+ # Pick the first working model found
51
+ model_name = "models/gemini-1.5-flash"
52
+ if model_name not in available_models:
53
+ # Fallback to whatever is actually there
54
+ model_name = available_models[0]
55
+
56
  prompt = """
57
  Extract the following information as strictly valid JSON:
58
+ { "name": "...", "email": "...", "phone": "...", "skills": [], "summary": "..." }
 
 
 
 
 
 
 
59
  """
60
 
61
+ try:
62
+ model = genai.GenerativeModel(model_name)
63
+ response = model.generate_content(prompt + "\n\nResume Text:\n" + resume_text[:10000])
64
+
65
+ raw_text = response.text.strip()
66
+ clean_json = re.sub(r'```json\s*|```', '', raw_text, flags=re.MULTILINE).strip()
67
+ return json.loads(clean_json)
68
+
69
+ except Exception as e:
70
+ return {"error": f"Used model {model_name} but failed: {str(e)}"}