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Ajuste os nomes de funções locais conforme o seu repositório (ex.: decompai.decompile_text)
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import traceback
|
| 6 |
+
from typing import Optional
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# Tenta carregar cliente da Hugging Face (para fallback via Inference API)
|
| 11 |
+
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN") or None
|
| 12 |
+
INFERENCE_MODEL = "tiiuae/falcon-40b-instruct" # exemplo: substitua por um modelo apropriado
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
from huggingface_hub import InferenceClient
|
| 16 |
+
hf_client = InferenceClient(token=hf_token) if hf_token else None
|
| 17 |
+
except Exception:
|
| 18 |
+
hf_client = None
|
| 19 |
+
|
| 20 |
+
# Tenta importar função local do repositório (ajuste o caminho/nome conforme seu projeto)
|
| 21 |
+
# Ex.: seu repositório pode expor `decompai.api.decompile_text` ou similar.
|
| 22 |
+
LOCAL_DECOMPILE_FN = None
|
| 23 |
+
try:
|
| 24 |
+
# tente alguns caminhos comuns — ajuste para o nome real
|
| 25 |
+
try:
|
| 26 |
+
# se o pacote for instalado como editable e expuser decompile_text
|
| 27 |
+
from decompai import decompile_text as _fn # exemplo
|
| 28 |
+
LOCAL_DECOMPILE_FN = _fn
|
| 29 |
+
except Exception:
|
| 30 |
+
# outro palpite: src.decompai.api.decompile_text
|
| 31 |
+
try:
|
| 32 |
+
from src.decompai import decompile_text as _fn2
|
| 33 |
+
LOCAL_DECOMPILE_FN = _fn2
|
| 34 |
+
except Exception:
|
| 35 |
+
LOCAL_DECOMPILE_FN = None
|
| 36 |
+
except Exception:
|
| 37 |
+
LOCAL_DECOMPILE_FN = None
|
| 38 |
+
|
| 39 |
+
def run_local_decompile(text: str) -> str:
|
| 40 |
+
"""Chama a função local (se existir). Deve retornar string com resultado."""
|
| 41 |
+
if not LOCAL_DECOMPILE_FN:
|
| 42 |
+
raise RuntimeError("Função local decompiladora não encontrada.")
|
| 43 |
+
return LOCAL_DECOMPILE_FN(text)
|
| 44 |
+
|
| 45 |
+
def run_hf_inference(prompt: str, model_name: Optional[str] = None) -> str:
|
| 46 |
+
"""Fallback: usa Hugging Face Inference API para gerar uma resposta."""
|
| 47 |
+
model = model_name or INFERENCE_MODEL
|
| 48 |
+
if not hf_client:
|
| 49 |
+
raise RuntimeError("HF client não disponível — verifique HF_TOKEN nas Secrets do Space.")
|
| 50 |
+
# Text generation via InferenceClient
|
| 51 |
+
# Nota: dependendo do modelo, a API pode expor métodos diferentes (text_generation, generate, etc.)
|
| 52 |
+
try:
|
| 53 |
+
# método genérico text_generation
|
| 54 |
+
out = hf_client.text_generation(model=model, inputs=prompt, max_new_tokens=512)
|
| 55 |
+
# InferenceClient pode retornar dict/list — normalize
|
| 56 |
+
if isinstance(out, list):
|
| 57 |
+
return out[0].get("generated_text", str(out[0]))
|
| 58 |
+
if isinstance(out, dict):
|
| 59 |
+
return out.get("generated_text") or str(out)
|
| 60 |
+
return str(out)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
# tentativa alternativa: chamar o endpoint genérico
|
| 63 |
+
try:
|
| 64 |
+
resp = hf_client(model=model, inputs=prompt)
|
| 65 |
+
return str(resp)
|
| 66 |
+
except Exception as e2:
|
| 67 |
+
raise RuntimeError(f"Erro na Inference API: {e}\n{e2}")
|
| 68 |
+
|
| 69 |
+
def prepare_prompt_from_input(file_contents: Optional[str], text_input: Optional[str]) -> str:
|
| 70 |
+
"""
|
| 71 |
+
Compose um prompt razoável para o modelo/função local.
|
| 72 |
+
Se o usuário subiu um binário/hex, a interface já deve pré-processar; aqui assumimos texto.
|
| 73 |
+
"""
|
| 74 |
+
prompt_parts = []
|
| 75 |
+
if file_contents:
|
| 76 |
+
prompt_parts.append("Input (arquivo/hex/binário):\n" + file_contents)
|
| 77 |
+
if text_input:
|
| 78 |
+
prompt_parts.append("Input (texto):\n" + text_input)
|
| 79 |
+
prompt = "\n\n".join(prompt_parts)
|
| 80 |
+
# instrução para decompilar
|
| 81 |
+
prompt = "Por favor, tente decompilar / traduzir o seguinte conteúdo para um código legível. Explique suposições e indique se algo não pôde ser recuperado.\n\n" + prompt
|
| 82 |
+
return prompt
|
| 83 |
+
|
| 84 |
+
def decompile_interface(file_obj, text_input, model_name):
|
| 85 |
+
"""
|
| 86 |
+
Handler chamado pelo Gradio.
|
| 87 |
+
file_obj can be None or uploaded file (gr.File)
|
| 88 |
+
text_input is string
|
| 89 |
+
"""
|
| 90 |
+
try:
|
| 91 |
+
file_contents = None
|
| 92 |
+
if file_obj is not None:
|
| 93 |
+
# file_obj pode ser um caminho/local file object (gr.File produz um dict em Spaces)
|
| 94 |
+
# Normalizamos: se for path, leia como binário e tentamos decodificar (utf-8/latin1/hex)
|
| 95 |
+
try:
|
| 96 |
+
# file_obj in Spaces is a dictionary with "name" and "data" occasionally; handle common cases:
|
| 97 |
+
path = getattr(file_obj, "name", None) or (file_obj if isinstance(file_obj, str) else None)
|
| 98 |
+
if path and os.path.exists(path):
|
| 99 |
+
with open(path, "rb") as f:
|
| 100 |
+
b = f.read()
|
| 101 |
+
# tenta decodificar, se falhar, representa em hex
|
| 102 |
+
try:
|
| 103 |
+
file_contents = b.decode("utf-8")
|
| 104 |
+
except Exception:
|
| 105 |
+
try:
|
| 106 |
+
file_contents = b.decode("latin-1")
|
| 107 |
+
except Exception:
|
| 108 |
+
file_contents = b.hex()
|
| 109 |
+
else:
|
| 110 |
+
# talvez file_obj já seja string contents
|
| 111 |
+
if isinstance(file_obj, (bytes, bytearray)):
|
| 112 |
+
try:
|
| 113 |
+
file_contents = file_obj.decode("utf-8")
|
| 114 |
+
except Exception:
|
| 115 |
+
file_contents = file_obj.hex()
|
| 116 |
+
elif isinstance(file_obj, str):
|
| 117 |
+
file_contents = file_obj
|
| 118 |
+
else:
|
| 119 |
+
file_contents = str(file_obj)
|
| 120 |
+
except Exception:
|
| 121 |
+
file_contents = f"<ERRO lendo arquivo: {traceback.format_exc()}>"
|
| 122 |
+
|
| 123 |
+
prompt = prepare_prompt_from_input(file_contents, text_input)
|
| 124 |
+
|
| 125 |
+
# Prioridade: função local (rápida e preferível)
|
| 126 |
+
if LOCAL_DECOMPILE_FN:
|
| 127 |
+
result = run_local_decompile(prompt)
|
| 128 |
+
return f"--- Resultado (função local) ---\n{result}"
|
| 129 |
+
# Fallback: HF inference
|
| 130 |
+
if hf_client:
|
| 131 |
+
result = run_hf_inference(prompt, model_name=model_name or INFERENCE_MODEL)
|
| 132 |
+
return f"--- Resultado (Hugging Face Inference API) ---\n{result}"
|
| 133 |
+
return "Nenhuma função local disponível e HF client não está configurado. Defina HF_TOKEN nas Secrets do Space."
|
| 134 |
+
except Exception as e:
|
| 135 |
+
tb = traceback.format_exc()
|
| 136 |
+
return f"Erro durante a decompilação:\n{e}\n\nTraceback:\n{tb}"
|
| 137 |
+
|
| 138 |
+
# Construção da interface Gradio
|
| 139 |
+
with gr.Blocks(title="DECOMPAI - Decompiler") as demo:
|
| 140 |
+
gr.Markdown("# DECOMPAI (Exemplo de Space)\nEnvie um arquivo ou cole texto e escolha o modelo (opcional).")
|
| 141 |
+
with gr.Row():
|
| 142 |
+
with gr.Column(scale=2):
|
| 143 |
+
file_in = gr.File(label="Arquivo (binário/hex) — opcional", file_count="single", type="file")
|
| 144 |
+
text_in = gr.Textbox(label="Ou cole conteúdo / hex / texto aqui", lines=8)
|
| 145 |
+
model_in = gr.Textbox(label="Modelo HF (opcional, ex: 'tiiuae/falcon-40b-instruct')", placeholder=INFERENCE_MODEL)
|
| 146 |
+
run_btn = gr.Button("Decompilar")
|
| 147 |
+
with gr.Column(scale=3):
|
| 148 |
+
output = gr.Textbox(label="Resultado", lines=24)
|
| 149 |
+
run_btn.click(fn=decompile_interface, inputs=[file_in, text_in, model_in], outputs=[output])
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
demo.launch()
|