File size: 1,628 Bytes
a3b686b
 
 
 
 
b070625
a3b686b
22c7a6d
86aa0dc
 
8eeb059
86aa0dc
 
8eeb059
86aa0dc
 
 
8eeb059
 
5e77219
8eeb059
 
86aa0dc
a3b686b
8eeb059
a3b686b
22c7a6d
 
 
 
 
 
 
 
 
 
a3b686b
22c7a6d
 
 
a3b686b
 
 
 
 
 
 
 
 
 
 
aff7596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)