Spaces:
Sleeping
Sleeping
| 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)}"} |