habulaj commited on
Commit
345dd9f
·
verified ·
1 Parent(s): d3e1569

Update routers/inference_createposter.py

Browse files
Files changed (1) hide show
  1. routers/inference_createposter.py +72 -1
routers/inference_createposter.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import logging
3
- from urllib.parse import urlencode, quote
4
  from fastapi import APIRouter, HTTPException
5
  from pydantic import BaseModel
6
  from google import genai
@@ -18,6 +18,71 @@ class PosterResponse(BaseModel):
18
  result: dict
19
  urls: list
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def format_url(base_url: str, endpoint: str, params: dict) -> str:
22
  """
23
  Formata uma URL completa com os parâmetros dados
@@ -47,6 +112,8 @@ def generate_urls_from_result(result: dict, base_url: str = "https://habulaj-new
47
  # Se for notícia simples
48
  if "endpoint" in result and "params" in result:
49
  url = format_url(base_url, result["endpoint"], result["params"])
 
 
50
  urls.append(url)
51
 
52
  # Se for carrossel com capa e slides
@@ -58,6 +125,8 @@ def generate_urls_from_result(result: dict, base_url: str = "https://habulaj-new
58
  result["cover"]["endpoint"],
59
  result["cover"]["params"]
60
  )
 
 
61
  urls.append(cover_url)
62
 
63
  # Adicionar URLs dos slides
@@ -68,6 +137,8 @@ def generate_urls_from_result(result: dict, base_url: str = "https://habulaj-new
68
  slide["endpoint"],
69
  slide["params"]
70
  )
 
 
71
  urls.append(slide_url)
72
 
73
  return urls
 
1
  import os
2
  import logging
3
+ from urllib.parse import urlencode, quote, parse_qs, urlparse, urlunparse
4
  from fastapi import APIRouter, HTTPException
5
  from pydantic import BaseModel
6
  from google import genai
 
18
  result: dict
19
  urls: list
20
 
21
+ def fix_citation_quotes(citation_text: str) -> str:
22
+ """
23
+ Corrige as aspas no texto de citação:
24
+ - Se não tiver aspas no início e fim, adiciona ❝ ❞
25
+ - Se tiver aspas comuns " ", substitui por ❝ ❞
26
+ - Se tiver outras aspas, substitui por ❝ ❞
27
+ """
28
+ if not citation_text or citation_text.strip() == "":
29
+ return citation_text
30
+
31
+ text = citation_text.strip()
32
+
33
+ # Verificar se já tem as aspas corretas
34
+ if text.startswith('❝') and text.endswith('❞'):
35
+ return text
36
+
37
+ # Remover aspas existentes do início e fim
38
+ quote_chars = ['"', "'", '"', '"', ''', ''']
39
+
40
+ # Remover aspas do início
41
+ while text and text[0] in quote_chars:
42
+ text = text[1:]
43
+
44
+ # Remover aspas do fim
45
+ while text and text[-1] in quote_chars:
46
+ text = text[:-1]
47
+
48
+ # Adicionar as aspas corretas
49
+ return f"❝{text.strip()}❞"
50
+
51
+ def fix_url_citation(url: str) -> str:
52
+ """
53
+ Analisa uma URL e corrige o parâmetro citation se presente
54
+ """
55
+ try:
56
+ # Parse da URL
57
+ parsed_url = urlparse(url)
58
+ query_params = parse_qs(parsed_url.query)
59
+
60
+ # Verificar se tem parâmetro citation
61
+ if 'citation' in query_params and query_params['citation']:
62
+ original_citation = query_params['citation'][0]
63
+
64
+ # Corrigir as aspas
65
+ fixed_citation = fix_citation_quotes(original_citation)
66
+
67
+ # Substituir o valor na query
68
+ query_params['citation'] = [fixed_citation]
69
+
70
+ # Reconstruir a query string
71
+ new_query = urlencode(
72
+ {k: v[0] if isinstance(v, list) and len(v) == 1 else v for k, v in query_params.items()},
73
+ quote_via=quote
74
+ )
75
+
76
+ # Reconstruir a URL
77
+ new_parsed_url = parsed_url._replace(query=new_query)
78
+ return urlunparse(new_parsed_url)
79
+
80
+ return url
81
+
82
+ except Exception as e:
83
+ logger.warning(f"Erro ao processar URL para correção de citation: {e}")
84
+ return url
85
+
86
  def format_url(base_url: str, endpoint: str, params: dict) -> str:
87
  """
88
  Formata uma URL completa com os parâmetros dados
 
112
  # Se for notícia simples
113
  if "endpoint" in result and "params" in result:
114
  url = format_url(base_url, result["endpoint"], result["params"])
115
+ # Corrigir citation na URL se presente
116
+ url = fix_url_citation(url)
117
  urls.append(url)
118
 
119
  # Se for carrossel com capa e slides
 
125
  result["cover"]["endpoint"],
126
  result["cover"]["params"]
127
  )
128
+ # Corrigir citation na URL se presente
129
+ cover_url = fix_url_citation(cover_url)
130
  urls.append(cover_url)
131
 
132
  # Adicionar URLs dos slides
 
137
  slide["endpoint"],
138
  slide["params"]
139
  )
140
+ # Corrigir citation na URL se presente
141
+ slide_url = fix_url_citation(slide_url)
142
  urls.append(slide_url)
143
 
144
  return urls