Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
|
| 10 |
-
torch_dtype=torch.float32,
|
| 11 |
-
device_map="cpu"
|
| 12 |
-
)
|
| 13 |
|
| 14 |
def responder(pergunta: str) -> str:
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
prompt = (
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
with torch.no_grad():
|
| 28 |
-
|
| 29 |
**inputs,
|
| 30 |
-
max_new_tokens=
|
| 31 |
-
temperature=0.7,
|
| 32 |
-
top_p=0.9,
|
| 33 |
do_sample=True,
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
return resposta
|
| 43 |
|
|
@@ -45,9 +75,12 @@ def responder(pergunta: str) -> str:
|
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=responder,
|
| 47 |
inputs=gr.Textbox(lines=2, label="Sua pergunta sobre Deep Learning"),
|
| 48 |
-
outputs=gr.Textbox(lines=
|
| 49 |
-
title="Professor DL -
|
| 50 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
# Modelo pequeno, viável em CPU grátis do Hugging Face
|
| 6 |
+
MODEL_NAME = "microsoft/DialoGPT-small"
|
| 7 |
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME).to("cpu")
|
| 10 |
+
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def responder(pergunta: str) -> str:
|
| 13 |
+
"""
|
| 14 |
+
Bot de Perguntas e Respostas sobre Deep Learning.
|
| 15 |
+
Recebe uma pergunta em texto e devolve uma resposta em português,
|
| 16 |
+
com tom de professor explicando de forma simples.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
pergunta = (pergunta or "").strip()
|
| 20 |
+
if not pergunta:
|
| 21 |
+
return "Me manda uma pergunta sobre Deep Learning 🙂"
|
| 22 |
+
|
| 23 |
+
# Prompt de sistema: define o "personagem"
|
| 24 |
+
system_prefix = (
|
| 25 |
+
"Você é o Professor DL, um professor de Deep Learning. "
|
| 26 |
+
"Responda SEMPRE em português do Brasil, de forma simples, didática e objetiva, "
|
| 27 |
+
"usando exemplos práticos quando possível. "
|
| 28 |
+
"Explique conceitos como redes neurais, camadas, CNN, RNN, overfitting, "
|
| 29 |
+
"regularização, dropout, etc., sem fórmulas muito pesadas."
|
| 30 |
+
)
|
| 31 |
|
| 32 |
prompt = (
|
| 33 |
+
system_prefix
|
| 34 |
+
+ "\n\n"
|
| 35 |
+
+ f"Aluno: {pergunta}\n"
|
| 36 |
+
+ "Professor DL:"
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Tokenização
|
| 40 |
+
inputs = tokenizer(
|
| 41 |
+
prompt,
|
| 42 |
+
return_tensors="pt",
|
| 43 |
+
truncation=True,
|
| 44 |
+
max_length=512,
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
# Geração
|
| 48 |
with torch.no_grad():
|
| 49 |
+
output_ids = model.generate(
|
| 50 |
**inputs,
|
| 51 |
+
max_new_tokens=160,
|
|
|
|
|
|
|
| 52 |
do_sample=True,
|
| 53 |
+
top_p=0.9,
|
| 54 |
+
temperature=0.7,
|
| 55 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 56 |
)
|
| 57 |
|
| 58 |
+
saida = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 59 |
+
|
| 60 |
+
# Extrai só a parte depois de "Professor DL:"
|
| 61 |
+
if "Professor DL:" in saida:
|
| 62 |
+
resposta = saida.split("Professor DL:")[-1].strip()
|
| 63 |
+
else:
|
| 64 |
+
resposta = saida.strip()
|
| 65 |
|
| 66 |
+
if not resposta:
|
| 67 |
+
resposta = (
|
| 68 |
+
"Boa pergunta! Tenta reformular ou ser um pouco mais específico "
|
| 69 |
+
"sobre o que você quer saber em Deep Learning."
|
| 70 |
+
)
|
| 71 |
|
| 72 |
return resposta
|
| 73 |
|
|
|
|
| 75 |
demo = gr.Interface(
|
| 76 |
fn=responder,
|
| 77 |
inputs=gr.Textbox(lines=2, label="Sua pergunta sobre Deep Learning"),
|
| 78 |
+
outputs=gr.Textbox(lines=8, label="Resposta do Professor DL"),
|
| 79 |
+
title="Professor DL - Bot de Deep Learning",
|
| 80 |
+
description=(
|
| 81 |
+
"Faça perguntas sobre redes neurais, Deep Learning, CNN, RNN, overfitting, "
|
| 82 |
+
"regularização, etc. O Professor DL responde em português, de forma didática."
|
| 83 |
+
),
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|