Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
-
import random
|
5 |
|
6 |
# Cargar el modelo y el tokenizador
|
7 |
-
model_name = "
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
fallback_responses = [
|
16 |
-
"Interesante. ¿Puedes decirme más sobre eso?",
|
17 |
-
"Entiendo. ¿Cómo te hace sentir eso?",
|
18 |
-
"¿Qué te llevó a pensar en eso?",
|
19 |
-
"Es una perspectiva interesante. ¿Has considerado otras alternativas?",
|
20 |
-
"Me gustaría saber más. ¿Puedes elaborar un poco?",
|
21 |
-
]
|
22 |
-
|
23 |
-
def get_response(input_text, conversation_history):
|
24 |
-
# Verificar si la respuesta está en caché
|
25 |
-
if input_text in response_cache:
|
26 |
-
return response_cache[input_text]
|
27 |
-
|
28 |
-
# Limitar la longitud de la conversación
|
29 |
-
if len(conversation_history) > 5:
|
30 |
-
conversation_history = conversation_history[-5:]
|
31 |
-
|
32 |
-
# Preparar el input para el modelo
|
33 |
-
bot_input_ids = tokenizer.encode(conversation_history + input_text + tokenizer.eos_token, return_tensors='pt')
|
34 |
-
|
35 |
-
# Generar respuesta
|
36 |
-
chat_response_ids = model.generate(
|
37 |
-
bot_input_ids,
|
38 |
-
max_length=1000,
|
39 |
-
pad_token_id=tokenizer.eos_token_id,
|
40 |
-
no_repeat_ngram_size=3,
|
41 |
-
do_sample=True,
|
42 |
-
top_k=100,
|
43 |
-
top_p=0.7,
|
44 |
-
temperature=0.8
|
45 |
-
)
|
46 |
-
|
47 |
-
chat_response = tokenizer.decode(chat_response_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
48 |
-
|
49 |
-
# Si la respuesta es vacía o muy corta, usar una respuesta predefinida
|
50 |
-
if not chat_response or len(chat_response.split()) < 3:
|
51 |
-
chat_response = random.choice(fallback_responses)
|
52 |
-
|
53 |
-
# Guardar en caché
|
54 |
-
response_cache[input_text] = chat_response
|
55 |
-
|
56 |
-
return chat_response
|
57 |
-
|
58 |
-
def chatbot(input_text, history):
|
59 |
history = history or []
|
60 |
-
conversation_history = " ".join([f"{h[0]} {h[1]}" for h in history])
|
61 |
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
history.append((
|
65 |
return history, history
|
66 |
|
67 |
iface = gr.Interface(
|
68 |
fn=chatbot,
|
69 |
inputs=["text", "state"],
|
70 |
outputs=["chatbot", "state"],
|
71 |
-
title="Tu Compañero AI
|
72 |
-
description="Un chatbot de IA
|
73 |
)
|
74 |
|
75 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
|
|
4 |
|
5 |
# Cargar el modelo y el tokenizador
|
6 |
+
model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_8bit=True)
|
9 |
|
10 |
+
def generate_response(prompt, max_length=200):
|
11 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
12 |
+
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model.generate(
|
15 |
+
inputs,
|
16 |
+
max_length=max_length,
|
17 |
+
num_return_sequences=1,
|
18 |
+
temperature=0.7,
|
19 |
+
top_p=0.9,
|
20 |
+
do_sample=True
|
21 |
+
)
|
22 |
+
|
23 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
+
return response.strip()
|
25 |
|
26 |
+
def chatbot(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
history = history or []
|
|
|
28 |
|
29 |
+
# Construir el prompt en el formato que Mixtral espera
|
30 |
+
prompt = "Eres un asistente AI amigable y útil. Responde de manera concisa y coherente.\n\n"
|
31 |
+
for human, ai in history:
|
32 |
+
prompt += f"Human: {human}\nAssistant: {ai}\n"
|
33 |
+
prompt += f"Human: {message}\nAssistant:"
|
34 |
+
|
35 |
+
response = generate_response(prompt)
|
36 |
|
37 |
+
history.append((message, response))
|
38 |
return history, history
|
39 |
|
40 |
iface = gr.Interface(
|
41 |
fn=chatbot,
|
42 |
inputs=["text", "state"],
|
43 |
outputs=["chatbot", "state"],
|
44 |
+
title="Tu Compañero AI con Mixtral",
|
45 |
+
description="Un chatbot de IA avanzado utilizando el modelo Mixtral-8x7B-Instruct-v0.1 para conversaciones coherentes y naturales.",
|
46 |
)
|
47 |
|
48 |
iface.launch()
|