diff options
| author | diogo464 <[email protected]> | 2025-07-21 15:02:48 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-07-21 15:02:48 +0100 |
| commit | 8c8dabd0ed20679a2dad43a5c239f9fcfe1c1ad7 (patch) | |
| tree | 55abbcfbbff19efa3aaf6cf36540ac7651c54973 /fetch-oneline-description | |
init
Diffstat (limited to 'fetch-oneline-description')
| -rwxr-xr-x | fetch-oneline-description | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/fetch-oneline-description b/fetch-oneline-description new file mode 100755 index 000000000..6f592e681 --- /dev/null +++ b/fetch-oneline-description | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | import os | ||
| 3 | import sys | ||
| 4 | import json | ||
| 5 | import subprocess | ||
| 6 | |||
| 7 | MODEL = "openrouter/anthropic/claude-3.5-haiku" | ||
| 8 | |||
| 9 | |||
| 10 | class LLMClient: | ||
| 11 | """Client for LLM description generation.""" | ||
| 12 | |||
| 13 | def __init__(self, model: str): | ||
| 14 | self.model = model | ||
| 15 | |||
| 16 | def llm_call( | ||
| 17 | self, | ||
| 18 | system_prompt: str, | ||
| 19 | user_prompt: str, | ||
| 20 | ) -> str: | ||
| 21 | proc = subprocess.run( | ||
| 22 | ["llm", "-m", self.model, "-s", system_prompt, user_prompt], | ||
| 23 | timeout=30, | ||
| 24 | check=True, | ||
| 25 | capture_output=True, | ||
| 26 | text=True, | ||
| 27 | ) | ||
| 28 | stdout = proc.stdout.strip() | ||
| 29 | return stdout | ||
| 30 | |||
| 31 | def generate_description(self, text: str) -> str: | ||
| 32 | """Generate short description using LLM.""" | ||
| 33 | system_prompt = """És um assistente especializado em condensar descrições de eventos de corrida em resumos de uma linha em português de Portugal. Deves extrair e resumir apenas a informação mais importante e relevante da descrição fornecida. | ||
| 34 | |||
| 35 | Exemplos de resumos que deves gerar: | ||
| 36 | + Corrida histórica pelas ruas de Lisboa com vista para o Tejo | ||
| 37 | + Trail desafiante pela Serra da Estrela | ||
| 38 | + São Silvestre tradicional no centro histórico do Porto | ||
| 39 | + Meia maratona costeira com paisagens do Atlântico | ||
| 40 | + Corrida solidária organizada pela câmara municipal | ||
| 41 | + Prova de montanha com subidas técnicas | ||
| 42 | + Corrida de Natal pela zona ribeirinha | ||
| 43 | + Trail nocturno por caminhos antigos | ||
| 44 | |||
| 45 | IMPORTANTE: | ||
| 46 | - Responde APENAS com a descrição de uma linha em português de Portugal | ||
| 47 | - Usa apenas informação presente na descrição original | ||
| 48 | - Destaca características especiais do percurso, localização ou organização | ||
| 49 | - Não menciones distâncias se já estão implícitas no tipo de evento | ||
| 50 | - Foca-te no que torna este evento único ou interessante""" | ||
| 51 | |||
| 52 | return self.llm_call( | ||
| 53 | system_prompt, | ||
| 54 | text, | ||
| 55 | ) | ||
| 56 | |||
| 57 | |||
| 58 | client = LLMClient(MODEL) | ||
| 59 | for slug in sys.argv[1:]: | ||
| 60 | data_path = os.path.join("events", slug, "data.json") | ||
| 61 | if not os.path.exists(data_path): | ||
| 62 | continue | ||
| 63 | data = json.load(open(data_path, "r")) | ||
| 64 | description = data["content"]["rendered"].strip() | ||
| 65 | if description == "": | ||
| 66 | continue | ||
| 67 | oneline_path = os.path.join("events", slug, "oneline-description") | ||
| 68 | if os.path.exists(oneline_path): | ||
| 69 | continue | ||
| 70 | oneline = client.generate_description(description) | ||
| 71 | with open(oneline_path, "w") as f: | ||
| 72 | f.write(oneline) | ||
| 73 | |||
