OrangyDev commited on
Commit
9d85df1
Β·
verified Β·
1 Parent(s): 9208eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -2,39 +2,47 @@ import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
- # Tu modelo ya entrenado
6
- model_id = "OrangyDev/godot4-expert-ai"
 
7
 
8
- # Cargamos el tokenizer y el modelo
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
- # Formateamos la pregunta como en el entrenamiento
18
  prompt = f"### User: {message}\n### Assistant:"
19
- inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
20
 
21
- output = model.generate(
22
- **inputs,
23
- max_new_tokens=100,
24
- temperature=0.7,
25
- repetition_penalty=1.2,
26
- eos_token_id=tokenizer.eos_token_id
27
- )
 
 
28
 
29
- response = tokenizer.decode(output[0], skip_special_tokens=True)
30
- return response.split("### Assistant:")[-1].strip()
 
 
31
 
32
- # Creamos la interfaz de chat
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?", "ΒΏCΓ³mo muevo un personaje?", "ΒΏQuiΓ©n es Rafa Laguna?"]
 
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__":