gnosticdev commited on
Commit
c376c52
verified
1 Parent(s): 9eb51ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -4,16 +4,16 @@ import torch
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  from huggingface_hub import login
6
 
7
- # Configurar el token de Hugging Face
8
- huggingface_token = os.environ.get('reparbot') # Mantenemos tu nombre de token original
9
  if huggingface_token is None:
10
- raise ValueError("El token de Hugging Face no est谩 configurado en las variables de entorno. Por favor, configura reparbot2")
11
 
12
- # Iniciar sesi贸n en Hugging Face
13
  login(huggingface_token)
14
 
15
- # Configurar el modelo y tokenizer
16
- model_id = "meta-llama/Llama-3.3-70B-Instruct" # Mantenemos tu modelo original
17
  tokenizer = AutoTokenizer.from_pretrained(model_id)
18
  model = AutoModelForCausalLM.from_pretrained(
19
  model_id,
@@ -21,12 +21,11 @@ model = AutoModelForCausalLM.from_pretrained(
21
  device_map="auto"
22
  )
23
 
24
- # Funci贸n para generar respuestas
25
  def respond_to_query(user_input):
26
- # Preparar el input
27
- inputs = tokenizer.encode(user_input, return_tensors="pt").to(model.device)
 
28
 
29
- # Generar respuesta
30
  outputs = model.generate(
31
  inputs,
32
  max_new_tokens=256,
@@ -34,18 +33,17 @@ def respond_to_query(user_input):
34
  top_p=0.95,
35
  top_k=50,
36
  temperature=0.7,
 
37
  )
38
 
39
- # Decodificar y retornar la respuesta
40
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
41
- return response
42
 
43
- # Crear la interfaz de Gradio
44
  interface = gr.Interface(
45
  fn=respond_to_query,
46
  inputs=gr.Textbox(label="Tu pregunta"),
47
  outputs=gr.Textbox(label="Respuesta"),
48
- title="Chatbot con Llama 3.3",
49
  description="Haz una pregunta y el modelo te responder谩"
50
  )
51
 
 
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  from huggingface_hub import login
6
 
7
+ # Obtener el token directamente del secreto de Hugging Face
8
+ huggingface_token = os.environ.get('reparbot2')
9
  if huggingface_token is None:
10
+ raise ValueError("El token de Hugging Face no est谩 configurado en los secretos del Space")
11
 
12
+ # Iniciar sesi贸n
13
  login(huggingface_token)
14
 
15
+ # Configurar modelo
16
+ model_id = "meta-llama/Llama-2-7b-chat-hf" # Cambiado al modelo de 7B
17
  tokenizer = AutoTokenizer.from_pretrained(model_id)
18
  model = AutoModelForCausalLM.from_pretrained(
19
  model_id,
 
21
  device_map="auto"
22
  )
23
 
 
24
  def respond_to_query(user_input):
25
+ # Formato espec铆fico para Llama 2 Chat
26
+ prompt = f"[INST] {user_input} [/INST]"
27
+ inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
28
 
 
29
  outputs = model.generate(
30
  inputs,
31
  max_new_tokens=256,
 
33
  top_p=0.95,
34
  top_k=50,
35
  temperature=0.7,
36
+ repetition_penalty=1.1
37
  )
38
 
39
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
40
 
41
+ # Interfaz Gradio
42
  interface = gr.Interface(
43
  fn=respond_to_query,
44
  inputs=gr.Textbox(label="Tu pregunta"),
45
  outputs=gr.Textbox(label="Respuesta"),
46
+ title="Chatbot con Llama-2-7b",
47
  description="Haz una pregunta y el modelo te responder谩"
48
  )
49