Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,13 @@ from peft import PeftModel
|
|
| 8 |
# CONFIG
|
| 9 |
# =========================
|
| 10 |
BASE_MODEL = "google/gemma-3-270m-it"
|
| 11 |
-
LORA_MODEL = "
|
| 12 |
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
|
| 16 |
# =========================
|
| 17 |
-
# LOAD
|
| 18 |
# =========================
|
| 19 |
print("🔄 Loading tokenizer...")
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
@@ -22,6 +22,9 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 22 |
token=HF_TOKEN
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
print("🔄 Loading base model...")
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
BASE_MODEL,
|
|
@@ -30,14 +33,37 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 30 |
device_map="cpu"
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
print("🔄 Applying LoRA...")
|
| 34 |
model = PeftModel.from_pretrained(model, LORA_MODEL)
|
| 35 |
model.eval()
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# =========================
|
| 38 |
# CHAT FUNCTION
|
| 39 |
# =========================
|
| 40 |
-
def chat(user_input, system_prompt, temperature, top_p, max_tokens):
|
| 41 |
prompt = (
|
| 42 |
"<bos>\n"
|
| 43 |
"<start_of_turn>system\n"
|
|
@@ -45,6 +71,9 @@ def chat(user_input, system_prompt, temperature, top_p, max_tokens):
|
|
| 45 |
"<start_of_turn>user\n"
|
| 46 |
f"{user_input}\n"
|
| 47 |
"<start_of_turn>model\n"
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
|
@@ -64,9 +93,9 @@ def chat(user_input, system_prompt, temperature, top_p, max_tokens):
|
|
| 64 |
text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 65 |
|
| 66 |
if "<start_of_turn>model" in text:
|
| 67 |
-
|
| 68 |
|
| 69 |
-
return text
|
| 70 |
|
| 71 |
# =========================
|
| 72 |
# GRADIO UI
|
|
@@ -74,13 +103,13 @@ def chat(user_input, system_prompt, temperature, top_p, max_tokens):
|
|
| 74 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 75 |
gr.Markdown(
|
| 76 |
"""
|
| 77 |
-
# 🐕 DogeAI v1.0
|
| 78 |
|
| 79 |
⚠️ **AVISO IMPORTANTE**
|
| 80 |
-
Este modelo é **experimental**, pequeno e pode **
|
| 81 |
-
|
| 82 |
|
| 83 |
-
❌
|
| 84 |
✅ Use para estudo, testes e experimentação
|
| 85 |
"""
|
| 86 |
)
|
|
@@ -90,13 +119,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 90 |
user_input = gr.Textbox(
|
| 91 |
lines=5,
|
| 92 |
label="Mensagem",
|
| 93 |
-
placeholder="
|
| 94 |
)
|
| 95 |
|
| 96 |
submit = gr.Button("Enviar 🚀")
|
| 97 |
|
| 98 |
output = gr.Textbox(
|
| 99 |
-
lines=
|
| 100 |
label="Resposta do modelo"
|
| 101 |
)
|
| 102 |
|
|
@@ -112,12 +141,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 112 |
label="Instruções internas"
|
| 113 |
)
|
| 114 |
|
| 115 |
-
gr.
|
| 116 |
-
|
| 117 |
-
"
|
| 118 |
-
"Valores errados podem causar respostas ruins ou nonsense./Bad values can cause trash or nonsense responses"
|
| 119 |
)
|
| 120 |
|
|
|
|
| 121 |
temperature = gr.Slider(
|
| 122 |
0.1, 1.5, value=0.65, step=0.05, label="Temperature"
|
| 123 |
)
|
|
@@ -132,9 +161,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 132 |
|
| 133 |
submit.click(
|
| 134 |
chat,
|
| 135 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
outputs=output
|
| 137 |
)
|
| 138 |
|
| 139 |
demo.launch()
|
| 140 |
|
|
|
|
|
|
| 8 |
# CONFIG
|
| 9 |
# =========================
|
| 10 |
BASE_MODEL = "google/gemma-3-270m-it"
|
| 11 |
+
LORA_MODEL = "AxionLab-official/DogeAI-v1.0-instruct"
|
| 12 |
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
|
| 16 |
# =========================
|
| 17 |
+
# LOAD TOKENIZER
|
| 18 |
# =========================
|
| 19 |
print("🔄 Loading tokenizer...")
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
| 22 |
token=HF_TOKEN
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# =========================
|
| 26 |
+
# LOAD BASE MODEL
|
| 27 |
+
# =========================
|
| 28 |
print("🔄 Loading base model...")
|
| 29 |
model = AutoModelForCausalLM.from_pretrained(
|
| 30 |
BASE_MODEL,
|
|
|
|
| 33 |
device_map="cpu"
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# =========================
|
| 37 |
+
# APPLY LORA
|
| 38 |
+
# =========================
|
| 39 |
print("🔄 Applying LoRA...")
|
| 40 |
model = PeftModel.from_pretrained(model, LORA_MODEL)
|
| 41 |
model.eval()
|
| 42 |
|
| 43 |
+
# =========================
|
| 44 |
+
# OUTPUT PARSER
|
| 45 |
+
# =========================
|
| 46 |
+
def extract_answer(text, show_reasoning=False):
|
| 47 |
+
if "<think>" in text and "</think>" in text:
|
| 48 |
+
reasoning = text.split("<think>")[1].split("</think>")[0].strip()
|
| 49 |
+
answer = text.split("</think>")[-1].strip()
|
| 50 |
+
|
| 51 |
+
if show_reasoning:
|
| 52 |
+
return (
|
| 53 |
+
"🧠 RACIOCÍNIO INTERNO:\n"
|
| 54 |
+
f"{reasoning}\n\n"
|
| 55 |
+
"✅ RESPOSTA FINAL:\n"
|
| 56 |
+
f"{answer}"
|
| 57 |
+
)
|
| 58 |
+
else:
|
| 59 |
+
return answer
|
| 60 |
+
|
| 61 |
+
return text.strip()
|
| 62 |
+
|
| 63 |
# =========================
|
| 64 |
# CHAT FUNCTION
|
| 65 |
# =========================
|
| 66 |
+
def chat(user_input, system_prompt, temperature, top_p, max_tokens, show_reasoning):
|
| 67 |
prompt = (
|
| 68 |
"<bos>\n"
|
| 69 |
"<start_of_turn>system\n"
|
|
|
|
| 71 |
"<start_of_turn>user\n"
|
| 72 |
f"{user_input}\n"
|
| 73 |
"<start_of_turn>model\n"
|
| 74 |
+
"<think>\n"
|
| 75 |
+
"Explique passo a passo seu raciocínio antes de responder.\n"
|
| 76 |
+
"</think>\n"
|
| 77 |
)
|
| 78 |
|
| 79 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
|
|
|
| 93 |
text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 94 |
|
| 95 |
if "<start_of_turn>model" in text:
|
| 96 |
+
text = text.split("<start_of_turn>model")[-1]
|
| 97 |
|
| 98 |
+
return extract_answer(text, show_reasoning)
|
| 99 |
|
| 100 |
# =========================
|
| 101 |
# GRADIO UI
|
|
|
|
| 103 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 104 |
gr.Markdown(
|
| 105 |
"""
|
| 106 |
+
# 🐕 DogeAI v1.0 — Reasoning Mode
|
| 107 |
|
| 108 |
⚠️ **AVISO IMPORTANTE**
|
| 109 |
+
Este modelo é **experimental**, pequeno e pode **alucinar**,
|
| 110 |
+
inventar fatos ou cometer erros.
|
| 111 |
|
| 112 |
+
❌ Não use como fonte confiável
|
| 113 |
✅ Use para estudo, testes e experimentação
|
| 114 |
"""
|
| 115 |
)
|
|
|
|
| 119 |
user_input = gr.Textbox(
|
| 120 |
lines=5,
|
| 121 |
label="Mensagem",
|
| 122 |
+
placeholder="Converse com o DogeAI 🐶"
|
| 123 |
)
|
| 124 |
|
| 125 |
submit = gr.Button("Enviar 🚀")
|
| 126 |
|
| 127 |
output = gr.Textbox(
|
| 128 |
+
lines=14,
|
| 129 |
label="Resposta do modelo"
|
| 130 |
)
|
| 131 |
|
|
|
|
| 141 |
label="Instruções internas"
|
| 142 |
)
|
| 143 |
|
| 144 |
+
show_reasoning = gr.Checkbox(
|
| 145 |
+
value=False,
|
| 146 |
+
label="Mostrar raciocínio interno (thinking)"
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
+
gr.Markdown("### ⚙️ Hiperparâmetros")
|
| 150 |
temperature = gr.Slider(
|
| 151 |
0.1, 1.5, value=0.65, step=0.05, label="Temperature"
|
| 152 |
)
|
|
|
|
| 161 |
|
| 162 |
submit.click(
|
| 163 |
chat,
|
| 164 |
+
inputs=[
|
| 165 |
+
user_input,
|
| 166 |
+
system_prompt,
|
| 167 |
+
temperature,
|
| 168 |
+
top_p,
|
| 169 |
+
max_tokens,
|
| 170 |
+
show_reasoning
|
| 171 |
+
],
|
| 172 |
outputs=output
|
| 173 |
)
|
| 174 |
|
| 175 |
demo.launch()
|
| 176 |
|
| 177 |
+
|