File size: 3,283 Bytes
a27660d
19afe3a
a27660d
 
 
 
c305acc
ee94b54
ed8db51
 
 
 
 
 
ee94b54
c305acc
4ee5091
c305acc
 
ee94b54
ed8db51
c305acc
ed8db51
 
 
 
 
ee94b54
 
 
ed8db51
ee94b54
ed8db51
 
 
 
 
 
ee94b54
ed8db51
ee94b54
c305acc
ed8db51
a27660d
 
 
ed8db51
a27660d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed8db51
a27660d
 
 
 
 
 
 
 
 
 
 
 
ed8db51
a27660d
 
 
 
ed8db51
a27660d
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
80
81
82
83
84
85
86
87
import os
import json
import requests

# Načtení klíče pro OpenRouter z proměnných prostředí
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")

def analyze_cv(cv_text, job_text, language="cs"):
    lang_map = {
        "cs": "česky (Czech)", 
        "en": "anglicky (English)", 
        "de": "německy (German)"
    }
    target_lang = lang_map.get(language, "česky (Czech)")
    
    prompt = f"""
    Jsi expert na nábor a kariérní kouč. Analyzuj CV a porovnej ho s pozicí.
    CV: {cv_text}
    Pozice: {job_text}
    
    DŮLEŽITÉ: Celá tvá odpověď musí být přeložena do jazyka: {target_lang}.
    
    Kromě analýzy shody přidej sekci 'career_recommendations', kde odpovíš:
    - Na jaké typy pozic a role se tento kandidát s ohledem na své CV nejlépe hodí?
    - Jakou seniority úroveň (junior/medior/senior) podle tebe momentálně má?
    
    Vrať odpověď POUZE jako validní JSON v tomto formátu (struktura klíčů musí zůstat v angličtině, ale hodnoty budou v jazyce {target_lang}):
    {{
        "match_score": 0, 
        "score_explanation": "...", 
        "career_recommendations": "...",
        "top_3_actions": [], 
        "missing_keywords": [
            {{"keyword": "...", "why_it_matters": "..."}}
        ], 
        "weak_sections": [
            {{"original_phrase": "...", "problem": "...", "what_to_convey_instead": "...", "example_direction": "..."}}
        ], 
        "ats_warnings": [], 
        "linkedin_headline_ideas": []
    }}
    """
    
    if not OPENROUTER_API_KEY:
        return {"error": "❌ Chybí OPENROUTER_API_KEY. Přidej ho do proměnných prostředí nebo do Secrets v Hugging Face."}

    try:
        response = requests.post(
            url="https://openrouter.ai/api/v1/chat/completions",
            headers={
                "Authorization": f"Bearer {OPENROUTER_API_KEY}",
                "Content-Type": "application/json"
            },
            json={
                "model": "openrouter/free", # Zde je zpět tvůj model!
                "messages": [
                    {"role": "user", "content": prompt}
                ],
                "response_format": {"type": "json_object"}
            }
        )
        
        # Zkontrolujeme HTTP chyby (např. 401, 500)
        response.raise_for_status()
        
        # Vytáhneme textovou odpověď
        result_text = response.json()['choices'][0]['message']['content']
        
        # Očištění Markdown bloku (občas ho AI přidá i k JSONu)
        result_text = result_text.strip()
        if result_text.startswith("```json"):
            result_text = result_text[7:]
        elif result_text.startswith("```"):
            result_text = result_text[3:]
            
        if result_text.endswith("```"):
            result_text = result_text[:-3]
            
        # Převod na Python slovník
        return json.loads(result_text)
        
    except requests.exceptions.RequestException as e:
        return {"error": f"Chyba sítě při volání OpenRouter: {str(e)}"}
    except json.JSONDecodeError:
        return {"error": "AI nevrátila validní JSON. Zkus to prosím znovu."}
    except Exception as e:
        return {"error": f"Neočekávaná chyba: {str(e)}"}