Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,75 @@
|
|
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 = "
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
chat_history += f"Human: {message['content']}\n"
|
18 |
-
else:
|
19 |
-
chat_history += f"AI: {message['content']}\n"
|
20 |
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
max_length=max_length,
|
32 |
-
num_return_sequences=1,
|
33 |
-
no_repeat_ngram_size=2,
|
34 |
-
temperature=0.7
|
35 |
-
)
|
36 |
|
37 |
-
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
gradio_history = [[m["content"], h["content"]] for m, h in zip(history[::2], history[1::2])]
|
43 |
|
44 |
-
|
|
|
45 |
|
46 |
iface = gr.Interface(
|
47 |
fn=chatbot,
|
48 |
inputs=["text", "state"],
|
49 |
outputs=["chatbot", "state"],
|
50 |
-
title="Tu Compañero AI
|
51 |
-
description="Un chatbot de IA diseñado para simular conversaciones personales
|
52 |
)
|
53 |
|
54 |
iface.launch()
|
|
|
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 = "microsoft/DialoGPT-small"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
|
11 |
+
# Caché simple para respuestas frecuentes
|
12 |
+
response_cache = {}
|
13 |
+
|
14 |
+
# Lista de respuestas predefinidas para variar la conversación
|
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 |
+
response = get_response(input_text, conversation_history)
|
|
|
63 |
|
64 |
+
history.append((input_text, response))
|
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 Mejorado",
|
72 |
+
description="Un chatbot de IA diseñado para simular conversaciones personales de manera rápida y coherente.",
|
73 |
)
|
74 |
|
75 |
iface.launch()
|