Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# Cargar el modelo y tokenizador
|
5 |
+
model_name = "bigscience/bloomz-560m" # Modelo multilingüe
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Función para manejar el chatbot
|
10 |
+
def chatbot(input_text, history=[]):
|
11 |
+
# Historial de conversación (opcional)
|
12 |
+
conversation = " ".join([f"Usuario: {turn[0]} Chatbot: {turn[1]}" for turn in history])
|
13 |
+
new_input = f"{conversation} Usuario: {input_text} Chatbot:"
|
14 |
+
|
15 |
+
inputs = tokenizer(new_input, return_tensors="pt", truncation=True)
|
16 |
+
outputs = model.generate(inputs["input_ids"], max_length=200, pad_token_id=tokenizer.eos_token_id)
|
17 |
+
|
18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Chatbot:")[-1].strip()
|
19 |
+
|
20 |
+
# Actualizar historial
|
21 |
+
history.append((input_text, response))
|
22 |
+
return history, history
|
23 |
+
|
24 |
+
# Configuración de la interfaz Gradio
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=chatbot,
|
27 |
+
inputs=["text", "state"], # Input de texto y estado para el historial
|
28 |
+
outputs=["chatbot", "state"], # Output del chatbot y estado
|
29 |
+
title="Neuronpyme Chatbot",
|
30 |
+
description="Un chatbot en español para ayudarte a implementar IA en tu negocio.",
|
31 |
+
theme="compact"
|
32 |
+
)
|
33 |
+
|
34 |
+
# Lanzar la aplicación
|
35 |
+
iface.launch()
|
36 |
+
|