gnosticdev commited on
Commit
cf9dee2
verified
1 Parent(s): b5ab9e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -18
app.py CHANGED
@@ -1,31 +1,53 @@
1
  import os
2
  import gradio as gr
3
  import torch
4
- from transformers import pipeline
 
5
 
6
- # Iniciar sesi贸n en Hugging Face usando el secreto
7
- huggingface_token = os.getenv('reparbot2') # Aseg煤rate de que el nombre coincida
8
  if huggingface_token is None:
9
- raise ValueError("El token de Hugging Face no est谩 configurado en las variables de entorno.")
 
 
10
  login(huggingface_token)
11
 
12
- # Configurar el modelo
13
- model_id = "meta-llama/Llama-3.3-70B-Instruct"
14
- pipeline_model = pipeline(
15
- "text-generation",
16
- model=model_id,
17
- model_kwargs={"torch_dtype": torch.bfloat16},
18
- device_map="auto",
19
  )
20
 
21
- # Funci贸n para responder a la consulta
22
  def respond_to_query(user_input):
23
- messages = [
24
- {"role": "user", "content": user_input},
25
- ]
26
- outputs = pipeline_model(messages, max_new_tokens=256)
27
- return outputs[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # Crear la interfaz de Gradio
30
- gr.Interface(fn=respond_to_query, inputs="text", outputs="text").launch()
 
 
 
 
 
 
31
 
 
 
 
1
  import os
2
  import gradio as gr
3
  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('reparbot2') # 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,
20
+ torch_dtype=torch.bfloat16,
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,
33
+ do_sample=True,
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
 
52
+ if __name__ == "__main__":
53
+ interface.launch()