Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,53 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
-
from transformers import
|
|
|
5 |
|
6 |
-
#
|
7 |
-
huggingface_token = os.
|
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 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
device_map="auto"
|
19 |
)
|
20 |
|
21 |
-
# Funci贸n para
|
22 |
def respond_to_query(user_input):
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Crear la interfaz de Gradio
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|