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65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import os
import re
import json
import requests
import gradio as gr
from pypdf import PdfReader
from docx import Document

OPENWEBNINJA_API_KEY = os.getenv("OPENWEBNINJA_API_KEY")

GEMINI_CHAT_URL = "https://api.openwebninja.com/gemini/chat"
JSEARCH_URL = "https://api.openwebninja.com/jsearch/search-v2"


def extract_text(file):
    if file is None:
        return ""

    path = file if isinstance(file, str) else file.name

    print("FILE PATH:", path)

    try:
        # Try PDF first
        try:
            reader = PdfReader(path)

            text = ""
            for page in reader.pages:
                page_text = page.extract_text()
                if page_text:
                    text += page_text + "\n"

            if text.strip():
                print("Detected PDF")
                return text

        except:
            pass

        # Try DOCX
        try:
            doc = Document(path)

            text = "\n".join(
                p.text for p in doc.paragraphs
            )

            if text.strip():
                print("Detected DOCX")
                return text

        except:
            pass

        # Try TXT
        try:
            with open(path, "r", encoding="utf-8", errors="ignore") as f:
                text = f.read()

            if text.strip():
                print("Detected TXT")
                return text

        except:
            pass

    except Exception as e:
        print("TEXT EXTRACTION ERROR:", e)

    return ""


def clean_text(text):
    text = text.replace("\n", " ")
    text = re.sub(r"([a-z0-9])([A-Z])", r"\1 \2", text)
    text = re.sub(r"\s+", " ", text)
    return text.strip()


def call_openwebninja_gemini(prompt):
    headers = {
        "X-API-Key": OPENWEBNINJA_API_KEY,
        "Content-Type": "application/json"
    }

    payloads = [
        {"message": prompt},
        {"prompt": prompt},
        {"messages": [{"role": "user", "content": prompt}]}
    ]

    for payload in payloads:
        try:
            response = requests.post(
                GEMINI_CHAT_URL,
                headers=headers,
                json=payload,
                timeout=60
            )

            print("GEMINI STATUS:", response.status_code)
            print("GEMINI RESPONSE:", response.text[:1000])

            if response.status_code == 200:
                return response.text

        except Exception as e:
            print("Gemini API error:", e)

    return ""


def extract_json(text):
    if not text:
        return None

    try:
        data = json.loads(text)

        # OpenWebNinja Gemini format
        if isinstance(data, dict):
            if "data" in data and isinstance(data["data"], dict):
                reply_text = data["data"].get("reply_text")
                if reply_text:
                    return extract_json(reply_text)

            for key in ["reply_text", "response", "text", "message", "content", "answer"]:
                if key in data and isinstance(data[key], str):
                    return extract_json(data[key])

            return data

    except:
        pass

    match = re.search(r"\{[\s\S]*\}", text)

    if match:
        try:
            return json.loads(match.group(0))
        except:
            return None

    return None

def analyze_resume_with_ai(resume_text):
    prompt = f"""

You are an AI resume analysis assistant.

Analyze this resume/CV from any career field.

Return ONLY valid JSON in this exact structure:

{{

  "candidate_field": "",

  "seniority_level": "",

  "education": [],

  "work_experience": [],

  "technical_skills": [],

  "soft_skills": [],

  "certifications": [],

  "suitable_job_titles": [],

  "job_search_queries": []

}}

Rules:

Rules:

- The resume can be from any field.

- Generate 15 to 20 suitable_job_titles.

- Include junior, graduate, entry-level and related roles.

- Include alternative titles and synonyms.

- Generate 10 to 15 diverse job_search_queries.

- job_search_queries must not be empty.

- Return at least 15 suitable_job_titles.

- Do not include explanations outside JSON.

Resume:

{resume_text[:7000]}

"""

    raw = call_openwebninja_gemini(prompt)
    profile = extract_json(raw)

    if not profile:
        return None, raw

    # Make sure all expected keys exist
    profile.setdefault("candidate_field", "")
    profile.setdefault("seniority_level", "")
    profile.setdefault("education", [])
    profile.setdefault("work_experience", [])
    profile.setdefault("technical_skills", [])
    profile.setdefault("soft_skills", [])
    profile.setdefault("certifications", [])
    profile.setdefault("suitable_job_titles", [])
    expanded = []

    for role in profile["suitable_job_titles"]:

        expanded.extend([
            role,
            f"Junior {role}",
            f"Graduate {role}",
            f"Entry Level {role}"
        ])

    profile["suitable_job_titles"] = list(
        dict.fromkeys(expanded)
    )
    profile.setdefault("job_search_queries", [])

    # Fallback query generation
    if not profile["job_search_queries"]:
        fallback_queries = []

        for title in profile["suitable_job_titles"]:
            fallback_queries.append(title)

        if profile["candidate_field"]:
            fallback_queries.append(profile["candidate_field"])

        for skill in profile["technical_skills"][:4]:
            fallback_queries.append(skill)

        # Remove duplicates
        seen = set()
        clean_queries = []

        for q in fallback_queries:
            q = str(q).strip()
            if q and q.lower() not in seen:
                seen.add(q.lower())
                clean_queries.append(q)

        profile["job_search_queries"] = clean_queries[:8]

    return profile, raw

def clean_query(query, location):
    query = query.replace("jobs in", "")
    query = query.replace("Jobs in", "")
    query = query.replace(location, "")
    return " ".join(query.split()).strip()


def search_jobs(query, location):
    headers = {
        "X-API-Key": OPENWEBNINJA_API_KEY
    }

    cleaned = clean_query(query, location)
    full_query = f"{cleaned} jobs in {location}"

    response = requests.get(
        JSEARCH_URL,
        params={"query": full_query},
        headers=headers,
        timeout=30
    )

    print("JSEARCH QUERY:", full_query)
    print("JSEARCH STATUS:", response.status_code)
    print("JSEARCH RESPONSE:", response.text[:1000])

    if response.status_code != 200:
        return []

    data = response.json()

    jobs = (
        data.get("jobs")
        or data.get("data")
        or data.get("results")
        or data.get("items")
        or []
    )

    if isinstance(jobs, dict):
        jobs = (
            jobs.get("jobs")
            or jobs.get("results")
            or jobs.get("items")
            or []
        )

    return jobs if isinstance(jobs, list) else []


def get_job_field(job, *keys):
    for key in keys:
        if job.get(key):
            return job.get(key)

    for nested_key in ["job", "company", "employer", "details"]:
        nested = job.get(nested_key)
        if isinstance(nested, dict):
            for key in keys:
                if nested.get(key):
                    return nested.get(key)

    return ""


def score_job(profile, job):
    skills = profile.get("technical_skills", []) + profile.get("soft_skills", [])
    titles = profile.get("suitable_job_titles", [])

    job_text = json.dumps(job).lower()
    matched = []

    score = 0

    for skill in skills:
        if skill.lower() in job_text:
            matched.append(skill)
            score += 8

    for title in titles:
        if title.lower() in job_text:
            score += 15

    score = min(score, 100)

    return score, matched


def analyze_resume(file, location):
    try:

        print("FILE OBJECT:", file)
        print("FILE TYPE:", type(file))


        if not OPENWEBNINJA_API_KEY:
            return "❌ OPENWEBNINJA_API_KEY is missing in Hugging Face Secrets."
            
        resume_text = clean_text(extract_text(file))

        print("EXTRACTED LENGTH:", len(resume_text))
        print("EXTRACTED TEXT:", resume_text[:500])

        if not resume_text:
            return f"""

        ❌ Could not extract text from the resume.

        File:

        {file}

        Type:

        {type(file)}

        """
        profile, raw_ai = analyze_resume_with_ai(resume_text)

        if not profile:
            return f"❌ AI could not analyze the resume.\n\nRaw response:\n\n```text\n{raw_ai}\n```"

        queries = profile.get("job_search_queries", [])

        if not queries:
            return f"""❌ AI did not generate job search queries.

        

        Raw AI response:

        

        ```text

        {raw_ai}

        {json.dumps(profile, indent=2)}

        ```"""
        output = "# CareerMatch AI Results\n\n"
        output += f"**Field:** {profile.get('candidate_field', 'Not detected')}\n\n"
        output += f"**Seniority:** {profile.get('seniority_level', 'Not detected')}\n\n"
        output += f"**Skills:** {', '.join(profile.get('technical_skills', [])) or 'Not detected'}\n\n"
        output += f"**Suitable Jobs:** {', '.join(profile.get('suitable_job_titles', [])) or 'Not detected'}\n\n"
        output += f"**Search Queries:** {', '.join(queries)}\n\n---\n\n"

        all_jobs = []

        for query in queries[:15]:
            jobs = search_jobs(query, location)
            output += f"Search: `{clean_query(query, location)} jobs in {location}` β†’ {len(jobs)} jobs found\n\n"

            for job in jobs[:10]:
                if isinstance(job, dict):
                    all_jobs.append(job)

        if not all_jobs:
            return output + "❌ No jobs found from JSearch."

        unique_jobs = []
        seen = set()

        for job in all_jobs:
            title = get_job_field(job, "title", "job_title", "name")
            company = get_job_field(job, "company_name", "company", "employer_name")
            key = f"{title}-{company}"

            if key not in seen:
                seen.add(key)
                unique_jobs.append(job)

        ranked = []

        for job in unique_jobs[:10]:
            score, matched = score_job(profile, job)
            ranked.append((score, matched, job))

        ranked.sort(key=lambda x: x[0], reverse=True)

        output += "\n---\n\n## Real Job Matches\n\n"

        for score, matched, job in ranked:
            title = get_job_field(job, "title", "job_title", "name") or "Unknown Job"
            company = get_job_field(job, "company_name", "company", "employer_name") or "Unknown Company"
            job_location = get_job_field(job, "location", "job_location", "formatted_location") or "Not specified"
            url = get_job_field(job, "url", "apply_link", "job_apply_link", "link") or "#"

            output += f"### {title} β€” {score}% Match\n\n"
            output += f"**Company:** {company}\n\n"
            output += f"**Location:** {job_location}\n\n"
            output += f"**Matched Skills:** {', '.join(matched) if matched else 'Not detected'}\n\n"
            output += f"[Apply Here]({url})\n\n---\n\n"

        return output

    except Exception as e:
        return f"❌ Error occurred:\n\n```text\n{type(e).__name__}: {str(e)}\n```"


demo = gr.Interface(
    fn=analyze_resume,
    inputs=[
        gr.File(
            label="Upload Resume/CV",
            type="filepath"
        ),
        gr.Textbox(label="Job Location", value="Mauritius")
    ],
    outputs=gr.Markdown(label="Results"),
    title="CareerMatch AI",
    description="Upload any resume and get real job matches using OpenWebNinja Gemini + JSearch."
)


if __name__ == "__main__":
    demo.launch()