Spaces:
Running on Zero

File size: 5,068 Bytes
63e6dfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""Assemble le CV final : identité, employeurs, dates et diplômes viennent de la base (jamais du LLM)."""

MONTHS = {
    "fr": ["janv.", "févr.", "mars", "avr.", "mai", "juin",
           "juil.", "août", "sept.", "oct.", "nov.", "déc."],
    "en": ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
}

LABELS = {
    "fr": {
        "profile": "PROFIL",
        "exp": "EXPÉRIENCE PROFESSIONNELLE",
        "edu": "FORMATION",
        "skills": "COMPÉTENCES CLÉS",
        "cert": "CERTIFICATIONS",
        "lang": "LANGUES",
        "avail": "Disponibilité : immédiate",
    },
    "en": {
        "profile": "PROFILE",
        "exp": "PROFESSIONAL EXPERIENCE",
        "edu": "EDUCATION",
        "skills": "KEY SKILLS",
        "cert": "CERTIFICATIONS",
        "lang": "LANGUAGES",
        "avail": "Availability: immediate",
    },
}


def fmt_date(date_value, lang):
    """Formate une date YYYY ou YYYY-MM."""
    if not date_value:
        return ""

    date_value = str(date_value)
    if len(date_value) == 4:
        return date_value

    if lang not in MONTHS:
        raise ValueError(f"Langue non prise en charge : {lang!r}")

    try:
        year, month = date_value.split("-", 1)
        month_index = int(month)
        if not 1 <= month_index <= 12:
            raise ValueError
    except (ValueError, TypeError):
        raise ValueError(
            f"Date invalide : {date_value!r}. Format attendu : YYYY ou YYYY-MM."
        )

    return f"{MONTHS[lang][month_index - 1]} {year}"


def build(kb, cv, lang):
    """Assemble le CV ; les données factuelles proviennent exclusivement de la base."""
    if lang not in LABELS:
        raise ValueError(f"Langue non prise en charge : {lang!r}")

    ident = kb["identity"]
    labels = LABELS[lang]

    by_id = {e["id"]: e for e in kb["experiences"]}
    generated = {
        e["id"]: e for e in cv.get("experiences", [])
        if e.get("id") in by_id
    }

    experiences = []
    for e in sorted(by_id.values(), key=lambda item: item["start"], reverse=True):
        if e["id"] not in generated:
            continue

        company = (
            (e.get("company_en") or e["company"])
            if lang == "en"
            else e["company"]
        )
        experiences.append({
            "title": e.get(f"title_{lang}", e.get("title", "")),
            "company": company,
            "loc": e.get(f"loc_{lang}", e.get("loc", "")),
            "period": f'{fmt_date(e["start"], lang)} – {fmt_date(e["end"], lang)}',
            "bullets": [
                b["text"] for b in generated[e["id"]].get("bullets", [])
                if b.get("text")
            ],
        })

    certifications = {
        c["id"]: c for c in kb.get("certifications", [])
    }

    birth_label = (
        "Né le 27/02/1975 | "
        if lang == "fr"
        else "Date of birth: 27/02/1975 | "
    )

    return {
        "lang": lang,
        "L": labels,
        "name": ident["name"],
        "title": cv.get("title", ""),
        "contact": (
            f'{ident["location_" + lang]} | '
            f'{ident["phone"]} | {ident["email"]}'
        ),
        "avail": birth_label + labels["avail"],
        "summary": cv.get("summary", ""),
        "experiences": experiences,
        "education": [
            {
                "deg": d[lang],
                "school": (
                    (d.get("school_en") or d["school"])
                    if lang == "en"
                    else d["school"]
                ),
                "years": d["years"],
            }
            for d in kb["education"]
        ],
        "skills": cv.get("skills", []),
        "certs": [
            f'{certifications[i]["name"]} — {certifications[i]["issuer"]}, '
            f'{certifications[i]["date"][:4]}'
            for i in cv.get("cert_ids", [])
            if i in certifications
        ],
        "languages": kb["languages"][lang],
    }


def to_markdown(r):
    """Convertit le CV assemblé en Markdown."""
    labels = r["L"]
    output = [
        f'# {r["name"]}',
        f'**{r["title"]}**',
        r["contact"],
        r["avail"],
        "",
        f'## {labels["profile"]}',
        r["summary"],
        "",
        f'## {labels["exp"]}',
    ]

    for e in r["experiences"]:
        output.append(
            f'**{e["title"]}** — {e["company"]}, {e["loc"]} ({e["period"]})'
        )
        output.extend(f"- {bullet}" for bullet in e["bullets"])
        output.append("")

    output.extend([
        f'## {labels["skills"]}',
        " · ".join(r["skills"]),
        "",
        f'## {labels["edu"]}',
    ])
    output.extend(
        f'- {d["deg"]} — {d["school"]} ({d["years"]})'
        for d in r["education"]
    )

    if r["certs"]:
        output.extend(["", f'## {labels["cert"]}'])
        output.extend(f"- {cert}" for cert in r["certs"])

    output.extend(["", f'## {labels["lang"]}'])
    output.extend(f"- {language}" for language in r["languages"])

    return "\n".join(output)