Spaces:
Runtime error
Runtime error
| import torch | |
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| # Configurações do modelo DeepSeek-R1 | |
| MODEL_NAME = "deepseek-ai/DeepSeek-R1" | |
| # Configuração segura para CPU | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| MODEL_NAME, | |
| trust_remote_code=True | |
| ) | |
| # Carregamento do modelo para CPU | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_NAME, | |
| trust_remote_code=True, | |
| torch_dtype=torch.float32, # Garante que usa float32 em CPU | |
| device_map="cpu", # Força execução na CPU | |
| low_cpu_mem_usage=True, # Otimiza uso de memória | |
| load_in_8bit=False, # Desativa FP8 | |
| load_in_4bit=False, # Garante que não tentará usar FP8/4bit | |
| ) | |
| def summarize_text(text): | |
| prompt = f"Resuma em português ({TARGET_LENGTH} caracteres): {text}" | |
| inputs = tokenizer(prompt, return_tensors="pt", max_length=2048, truncation=True) | |
| with torch.inference_mode(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=MAX_LENGTH, | |
| temperature=0.9, | |
| top_k=50, | |
| no_repeat_ngram_size=3 | |
| ) | |
| summary = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return summary[len(prompt):].strip() | |
| # Interface Gradio | |
| interface = gr.Interface( | |
| fn=summarize_text, | |
| inputs=gr.Textbox(label="Texto", lines=10, placeholder="Digite seu texto aqui..."), | |
| outputs=gr.Textbox(label="Resumo"), | |
| title="Resumidor com DeepSeek-R1", | |
| description="Resumos automáticos em português com ajuste de tamanho (250-262 caracteres)", | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch(share=True) |