Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
-
|
| 6 |
-
MODEL_NAME = "microsoft/DialoGPT-small"
|
| 7 |
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def responder(pergunta: str) -> str:
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 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 |
-
|
| 50 |
**inputs,
|
| 51 |
-
max_new_tokens=
|
| 52 |
-
do_sample=True,
|
| 53 |
-
top_p=0.9,
|
| 54 |
temperature=0.7,
|
| 55 |
-
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
if "Professor DL:" in
|
| 62 |
-
resposta =
|
| 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,14 +45,10 @@ def responder(pergunta: str) -> str:
|
|
| 75 |
demo = gr.Interface(
|
| 76 |
fn=responder,
|
| 77 |
inputs=gr.Textbox(lines=2, label="Sua pergunta sobre Deep Learning"),
|
| 78 |
-
outputs=gr.Textbox(lines=
|
| 79 |
-
title="Professor DL -
|
| 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__":
|
| 87 |
demo.launch()
|
| 88 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
MODEL_NAME = "NousResearch/Nous-Hermes-2-SOLAR-0.8B"
|
|
|
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
MODEL_NAME,
|
| 10 |
+
torch_dtype=torch.float32,
|
| 11 |
+
device_map="cpu"
|
| 12 |
+
)
|
| 13 |
|
| 14 |
def responder(pergunta: str) -> str:
|
| 15 |
+
if not pergunta.strip():
|
| 16 |
+
return "Pode mandar sua pergunta sobre Deep Learning!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
prompt = (
|
| 19 |
+
"Você é o Professor DL, um especialista em Deep Learning. "
|
| 20 |
+
"Explique tudo de maneira simples, objetiva e em português. "
|
| 21 |
+
"Use exemplos práticos. Responda como um professor experiente.\n\n"
|
| 22 |
+
f"Aluno: {pergunta}\nProfessor DL:"
|
| 23 |
)
|
| 24 |
|
| 25 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
with torch.no_grad():
|
| 28 |
+
output = model.generate(
|
| 29 |
**inputs,
|
| 30 |
+
max_new_tokens=200,
|
|
|
|
|
|
|
| 31 |
temperature=0.7,
|
| 32 |
+
top_p=0.9,
|
| 33 |
+
do_sample=True,
|
| 34 |
)
|
| 35 |
|
| 36 |
+
resposta = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 37 |
|
| 38 |
+
# limpa o texto
|
| 39 |
+
if "Professor DL:" in resposta:
|
| 40 |
+
resposta = resposta.split("Professor DL:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
return resposta
|
| 43 |
|
|
|
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=responder,
|
| 47 |
inputs=gr.Textbox(lines=2, label="Sua pergunta sobre Deep Learning"),
|
| 48 |
+
outputs=gr.Textbox(lines=10, label="Resposta do Professor DL"),
|
| 49 |
+
title="Professor DL - Chatbot de Deep Learning",
|
| 50 |
+
description="Pergunte qualquer coisa sobre DL, redes neurais, CNN, RNN, overfitting, regularização, etc.",
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
demo.launch()
|
|
|