Update app.py
Browse files
app.py
CHANGED
|
@@ -2,39 +2,47 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_id,
|
| 12 |
-
torch_dtype=torch.float16,
|
| 13 |
device_map="auto"
|
| 14 |
)
|
| 15 |
|
|
|
|
| 16 |
def chat_godot(message, history):
|
| 17 |
-
#
|
| 18 |
prompt = f"### User: {message}\n### Assistant:"
|
| 19 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
demo = gr.ChatInterface(
|
| 34 |
fn=chat_godot,
|
| 35 |
title="Godot 4 Expert AI",
|
| 36 |
description=f"IA entrenada por {userxd} para resolver dudas de Godot 4 y GDScript.",
|
| 37 |
-
examples=["ΒΏQuiΓ©n es tu creador?", "ΒΏ
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# 1. DEFINIR VARIABLES
|
| 6 |
+
userxd = "OrangyDev"
|
| 7 |
+
model_id = f"{userxd}/godot4-expert-ai"
|
| 8 |
|
| 9 |
+
# 2. CARGAR TOKENIZER Y MODELO
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
+
# Usamos float16 y seleccionamos CPU o GPU automΓ‘ticamente
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_id,
|
| 14 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 15 |
device_map="auto"
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# 3. FUNCIΓN DE CHAT
|
| 19 |
def chat_godot(message, history):
|
| 20 |
+
# Formato de prompt igual al del entrenamiento
|
| 21 |
prompt = f"### User: {message}\n### Assistant:"
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 23 |
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
output = model.generate(
|
| 26 |
+
**inputs,
|
| 27 |
+
max_new_tokens=150,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
repetition_penalty=1.2,
|
| 31 |
+
eos_token_id=tokenizer.eos_token_id
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
full_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 35 |
+
# Cortamos para quedarnos solo con la respuesta de la IA
|
| 36 |
+
response = full_text.split("### Assistant:")[-1].strip()
|
| 37 |
+
return response
|
| 38 |
|
| 39 |
+
# 4. INTERFAZ DE GRADIO
|
| 40 |
demo = gr.ChatInterface(
|
| 41 |
fn=chat_godot,
|
| 42 |
title="Godot 4 Expert AI",
|
| 43 |
description=f"IA entrenada por {userxd} para resolver dudas de Godot 4 y GDScript.",
|
| 44 |
+
examples=["ΒΏQuiΓ©n es tu creador?", "ΒΏQuiΓ©n es Rafa Laguna?", "ΒΏCΓ³mo muevo un personaje en Godot 4?"],
|
| 45 |
+
theme="soft"
|
| 46 |
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|