Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,194 +1,193 @@
|
|
| 1 |
-
# app.py — versão
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
--
|
| 12 |
-
--panel
|
| 13 |
-
--
|
| 14 |
-
--
|
| 15 |
-
--
|
| 16 |
-
--
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
.gradio-container .
|
| 29 |
-
.gradio-container .gr-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
border
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
border:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
.gradio-container .gr-
|
| 54 |
-
.gradio-container .
|
| 55 |
-
.gradio-container .
|
| 56 |
-
.gradio-container .gr-file
|
| 57 |
-
.gradio-container .gr-
|
| 58 |
-
.gradio-container .
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
.gradio-container .gr-
|
| 71 |
-
.gradio-container .
|
| 72 |
-
.gradio-container .
|
| 73 |
-
.gradio-container .gr-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
.gradio-container [data-testid="
|
| 82 |
-
.gradio-container [data-testid="
|
| 83 |
-
.gradio-container [data-testid="
|
| 84 |
-
.gradio-container [data-testid="
|
| 85 |
-
.gradio-container [data-testid="
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
.gradio-container .gr-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
border:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
.gradio-container .fixed.bottom-0,
|
| 100 |
-
.gradio-container div[class*="fixed"][class*="bottom-0"],
|
| 101 |
-
.gradio-container footer,
|
| 102 |
-
body > div.fixed.bottom-0,
|
| 103 |
-
div.fixed.bottom-0 {
|
| 104 |
-
display: none !important;
|
| 105 |
-
visibility: hidden !important;
|
| 106 |
-
height: 0 !important;
|
| 107 |
-
overflow: hidden !important;
|
| 108 |
-
pointer-events: none !important;
|
| 109 |
-
}
|
| 110 |
-
"""
|
| 111 |
-
|
| 112 |
-
THEME = gr.themes.Soft(primary_hue="zinc", neutral_hue="zinc")
|
| 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 |
-
try:
|
| 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 |
-
|
| 179 |
-
|
| 180 |
-
),
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
gr.
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py — versão final p/ Hugging Face (tema + UI padronizados)
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from typing import List
|
| 6 |
+
from processador import processar_misto # devolve [(caminho_img, legenda)], caminho_zip
|
| 7 |
+
|
| 8 |
+
# -------------------- Tema + CSS do portfólio --------------------
|
| 9 |
+
CUSTOM_CSS = """
|
| 10 |
+
:root{
|
| 11 |
+
--bg:#000; /* fundo geral */
|
| 12 |
+
--panel:#0b0b0b; /* blocos/painéis */
|
| 13 |
+
--panel-2:#0e0e0e; /* inputs/dropdowns */
|
| 14 |
+
--border:#2a2a2a; /* borda padrão */
|
| 15 |
+
--text:#e5e5e5; /* texto branco suave */
|
| 16 |
+
--muted:#a3a3a3; /* texto secundário */
|
| 17 |
+
--accent:#6ee7b7; /* cor do foco/seleção (verde menta) */
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
/* fonte geral (system UI) */
|
| 21 |
+
html, body, .gradio-container {
|
| 22 |
+
background: var(--bg)!important;
|
| 23 |
+
color: var(--text)!important;
|
| 24 |
+
font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif !important;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
/* blocos/painéis */
|
| 28 |
+
.gradio-container .block,
|
| 29 |
+
.gradio-container .gr-box,
|
| 30 |
+
.gradio-container .gr-panel {
|
| 31 |
+
background: var(--panel) !important;
|
| 32 |
+
border: 1px solid var(--border) !important;
|
| 33 |
+
border-radius: 12px !important;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
/* remover o bloco atrás do TÍTULO */
|
| 37 |
+
.gradio-container .block:has(h1){
|
| 38 |
+
background: transparent !important;
|
| 39 |
+
border: 0 !important;
|
| 40 |
+
box-shadow: none !important;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/* botões estilo "pílula" */
|
| 44 |
+
button, .gr-button{
|
| 45 |
+
border-radius: 9999px !important;
|
| 46 |
+
border: 1px solid var(--border) !important;
|
| 47 |
+
background: var(--panel-2) !important;
|
| 48 |
+
}
|
| 49 |
+
button:hover{ border-color:#4a4a4a !important; }
|
| 50 |
+
|
| 51 |
+
/* inputs/textarea/file/dropdown */
|
| 52 |
+
input, textarea, select,
|
| 53 |
+
.gradio-container .gr-textbox,
|
| 54 |
+
.gradio-container .gr-input,
|
| 55 |
+
.gradio-container .gradio-dropdown,
|
| 56 |
+
.gradio-container .gr-file,
|
| 57 |
+
.gradio-container .gr-file-download,
|
| 58 |
+
.gradio-container .gr-select-container,
|
| 59 |
+
.gradio-container .wrap .items-center select {
|
| 60 |
+
background: var(--panel-2) !important;
|
| 61 |
+
border: 1px solid var(--border) !important;
|
| 62 |
+
color: var(--text) !important;
|
| 63 |
+
border-radius: 12px !important;
|
| 64 |
+
}
|
| 65 |
+
select > option { background: var(--panel-2); color: var(--text); }
|
| 66 |
+
|
| 67 |
+
/* foco/seleção visível */
|
| 68 |
+
input:focus, textarea:focus, select:focus,
|
| 69 |
+
.gradio-container .gr-textbox:focus-within,
|
| 70 |
+
.gradio-container .gr-input:focus-within,
|
| 71 |
+
.gradio-container .gradio-dropdown:focus-within,
|
| 72 |
+
.gradio-container .gr-file:focus-within,
|
| 73 |
+
.gradio-container .gr-select-container:focus-within {
|
| 74 |
+
outline: none !important;
|
| 75 |
+
border-color: var(--accent) !important;
|
| 76 |
+
box-shadow: 0 0 0 2px rgba(110,231,183,0.18) !important;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/* uploader QUADRADO */
|
| 80 |
+
.gradio-container [data-testid="file"] .rounded-full,
|
| 81 |
+
.gradio-container [data-testid="files"] .rounded-full { border-radius:12px !important; }
|
| 82 |
+
.gradio-container [data-testid="file"] [class*="aspect-"],
|
| 83 |
+
.gradio-container [data-testid="files"] [class*="aspect-"] { aspect-ratio:auto !important; }
|
| 84 |
+
.gradio-container [data-testid="file"] .h-full.w-full,
|
| 85 |
+
.gradio-container [data-testid="files"] .h-full.w-full { border-radius:12px !important; }
|
| 86 |
+
|
| 87 |
+
/* gallery e saídas */
|
| 88 |
+
.gradio-container .gr-gallery,
|
| 89 |
+
.gradio-container .gr-file-download{
|
| 90 |
+
background: var(--panel-2) !important;
|
| 91 |
+
border: 1px solid var(--border) !important;
|
| 92 |
+
border-radius: 12px !important;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
/* badges/labels */
|
| 96 |
+
.badge, .token { background:#0f0f0f !important; border-radius:9999px !important; }
|
| 97 |
+
|
| 98 |
+
/* esconder rodapé do Gradio */
|
| 99 |
+
.gradio-container .fixed.bottom-0,
|
| 100 |
+
.gradio-container div[class*="fixed"][class*="bottom-0"],
|
| 101 |
+
.gradio-container footer,
|
| 102 |
+
body > div.fixed.bottom-0,
|
| 103 |
+
div.fixed.bottom-0 {
|
| 104 |
+
display: none !important;
|
| 105 |
+
visibility: hidden !important;
|
| 106 |
+
height: 0 !important;
|
| 107 |
+
overflow: hidden !important;
|
| 108 |
+
pointer-events: none !important;
|
| 109 |
+
}
|
| 110 |
+
"""
|
| 111 |
+
|
| 112 |
+
THEME = gr.themes.Soft(primary_hue="zinc", neutral_hue="zinc")
|
| 113 |
+
|
| 114 |
+
# -------------------- Util --------------------
|
| 115 |
+
def _normalize_paths(arquivos) -> List[Path]:
|
| 116 |
+
"""gr.Files pode entregar str, Path, objeto com .name ou até dict-like."""
|
| 117 |
+
items = arquivos if isinstance(arquivos, list) else [arquivos]
|
| 118 |
+
paths: List[Path] = []
|
| 119 |
+
for a in items:
|
| 120 |
+
if isinstance(a, (str, Path)):
|
| 121 |
+
paths.append(Path(a))
|
| 122 |
+
else:
|
| 123 |
+
name = getattr(a, "name", None)
|
| 124 |
+
if name is None:
|
| 125 |
+
try:
|
| 126 |
+
name = a["name"] # alguns retornam dict-like
|
| 127 |
+
except Exception:
|
| 128 |
+
name = str(a)
|
| 129 |
+
paths.append(Path(name))
|
| 130 |
+
return paths
|
| 131 |
+
|
| 132 |
+
# -------------------- Função do app --------------------
|
| 133 |
+
def processar(arquivos, modo, paginas_input, formato_opcao):
|
| 134 |
+
if not arquivos:
|
| 135 |
+
return "Por favor, envie ao menos um PDF (ou .zip com PDFs).", None
|
| 136 |
+
|
| 137 |
+
paths = _normalize_paths(arquivos)
|
| 138 |
+
|
| 139 |
+
# pega "jpeg" de "jpeg [Recomendado - ...]"
|
| 140 |
+
formato = formato_opcao.split(" [")[0].strip().lower()
|
| 141 |
+
|
| 142 |
+
paginas = None
|
| 143 |
+
if "específica" in modo.lower(): # aceita "Específicas"/"Específica"
|
| 144 |
+
nums = re.findall(r"\d+", paginas_input or "")
|
| 145 |
+
if not nums:
|
| 146 |
+
return "Nenhuma página válida foi informada.", None
|
| 147 |
+
paginas = [int(n) for n in nums]
|
| 148 |
+
|
| 149 |
+
try:
|
| 150 |
+
galeria, zip_path = processar_misto(paths, modo, paginas, formato)
|
| 151 |
+
return galeria, zip_path # Gallery aceita lista de (caminho, legenda)
|
| 152 |
+
except Exception as e:
|
| 153 |
+
return f"Erro ao processar: {e}", None
|
| 154 |
+
|
| 155 |
+
# opções do dropdown (sem espaços no início)
|
| 156 |
+
FORMATOS = [
|
| 157 |
+
"jpeg [Recomendado - compacto e boa qualidade]",
|
| 158 |
+
"png [Alta qualidade, suporta transparência]",
|
| 159 |
+
"bmp [Sem compressão - ideal para edição bruta]",
|
| 160 |
+
"ico [Favicon para sites, atalhos e apps]",
|
| 161 |
+
]
|
| 162 |
+
|
| 163 |
+
demo = gr.Interface(
|
| 164 |
+
fn=processar,
|
| 165 |
+
inputs=[
|
| 166 |
+
gr.Files(
|
| 167 |
+
label="Envie PDF(s) ou .zip com PDFs",
|
| 168 |
+
file_count="multiple",
|
| 169 |
+
file_types=[".pdf", ".zip"],
|
| 170 |
+
),
|
| 171 |
+
gr.Radio(
|
| 172 |
+
["Extrair todas as páginas", "Extrair páginas específicas"],
|
| 173 |
+
label="Modo",
|
| 174 |
+
value="Extrair todas as páginas",
|
| 175 |
+
),
|
| 176 |
+
gr.Textbox(
|
| 177 |
+
label="Páginas (se usar 'Específicas')",
|
| 178 |
+
placeholder="Ex.: 3 - 5 - 10",
|
| 179 |
+
),
|
| 180 |
+
gr.Dropdown(choices=FORMATOS, value=FORMATOS[0], label="Formato das imagens"),
|
| 181 |
+
],
|
| 182 |
+
outputs=[
|
| 183 |
+
gr.Gallery(label="Imagens"),
|
| 184 |
+
gr.File(label="ZIP com as imagens"),
|
| 185 |
+
],
|
| 186 |
+
title="Extrator de Imagens de PDF",
|
| 187 |
+
allow_flagging="never",
|
| 188 |
+
theme=THEME,
|
| 189 |
+
css=CUSTOM_CSS,
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
if __name__ == "__main__":
|
| 193 |
+
demo.launch()
|
|
|