Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import speech_recognition as sr
|
4 |
-
|
5 |
-
from pymongo import MongoClient
|
6 |
-
from bson.objectid import ObjectId
|
7 |
import pyttsx3
|
8 |
|
9 |
# Configuraci贸n de la p谩gina de Streamlit
|
@@ -12,58 +10,35 @@ st.set_page_config(layout="wide")
|
|
12 |
# Configuraci贸n de la API key para Google Generative AI
|
13 |
api_key = "AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k" # API key proporcionada
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
try:
|
18 |
-
cliente = MongoClient("mongodb://localhost:27017/")
|
19 |
-
cliente.server_info() # Verifica la conexi贸n
|
20 |
-
return cliente
|
21 |
-
except Exception as e:
|
22 |
-
st.error(f"Error: No se pudo conectar a MongoDB. Verifica que el servidor est茅 en ejecuci贸n. Detalles: {e}")
|
23 |
-
return None
|
24 |
-
|
25 |
-
cliente = obtener_cliente_mongo()
|
26 |
-
db = None
|
27 |
-
coleccion_historial = None
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
coleccion_historial = db["historial"]
|
32 |
|
33 |
def guardar_historial(modo_entrada, entrada_usuario, respuesta):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
}
|
40 |
-
coleccion_historial.insert_one(entrada_historial)
|
41 |
|
42 |
def cargar_historial():
|
43 |
-
|
44 |
-
entradas_historial = list(coleccion_historial.find().sort("_id", -1))
|
45 |
-
return entradas_historial
|
46 |
-
return []
|
47 |
|
48 |
-
def eliminar_historial(
|
49 |
-
if
|
50 |
-
|
51 |
|
52 |
def procesar_texto(texto):
|
53 |
-
|
54 |
-
|
55 |
-
respuesta
|
56 |
-
resultado = respuesta.text
|
57 |
-
guardar_historial("Texto", texto, resultado)
|
58 |
-
return resultado
|
59 |
|
60 |
def procesar_imagen(imagen):
|
61 |
-
|
62 |
-
|
63 |
-
respuesta
|
64 |
-
resultado = respuesta.text
|
65 |
-
guardar_historial("Imagen", imagen.name, resultado)
|
66 |
-
return resultado
|
67 |
|
68 |
def reconocer_voz():
|
69 |
reconocedor = sr.Recognizer()
|
@@ -85,27 +60,25 @@ def hablar_texto(texto):
|
|
85 |
|
86 |
st.sidebar.title("Historial")
|
87 |
|
88 |
-
historial = cargar_historial()
|
89 |
-
respuesta_seleccionada = None
|
90 |
-
|
91 |
# Mostrar historial en la barra lateral
|
92 |
-
for entrada in
|
93 |
with st.sidebar.expander(f"{entrada['entrada_usuario']}"):
|
94 |
col1, col2 = st.columns([1, 1])
|
95 |
with col1:
|
96 |
-
if st.button("Respuesta", key=
|
97 |
-
respuesta_seleccionada = entrada['respuesta']
|
98 |
with col2:
|
99 |
-
if st.button("Eliminar", key=f"eliminar_{
|
100 |
-
eliminar_historial(
|
101 |
-
st.
|
102 |
|
103 |
st.title("馃 ChatBot")
|
104 |
|
105 |
espacio_contenido_generado = st.empty()
|
106 |
|
107 |
# Mostrar respuesta seleccionada
|
108 |
-
if respuesta_seleccionada:
|
|
|
109 |
espacio_contenido_generado.write(respuesta_seleccionada)
|
110 |
if st.button("馃攰 Hablar"):
|
111 |
hablar_texto(respuesta_seleccionada)
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import speech_recognition as sr
|
4 |
+
from transformers import pipeline
|
|
|
|
|
5 |
import pyttsx3
|
6 |
|
7 |
# Configuraci贸n de la p谩gina de Streamlit
|
|
|
10 |
# Configuraci贸n de la API key para Google Generative AI
|
11 |
api_key = "AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k" # API key proporcionada
|
12 |
|
13 |
+
# Inicializaci贸n de los pipelines de Hugging Face
|
14 |
+
generator = pipeline("text-generation", model="gpt-3.5-turbo") # Usa el modelo de Hugging Face para generaci贸n de texto
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Lista para almacenar el historial de interacci贸n
|
17 |
+
historial = []
|
|
|
18 |
|
19 |
def guardar_historial(modo_entrada, entrada_usuario, respuesta):
|
20 |
+
historial.append({
|
21 |
+
"modo_entrada": modo_entrada,
|
22 |
+
"entrada_usuario": entrada_usuario,
|
23 |
+
"respuesta": respuesta
|
24 |
+
})
|
|
|
|
|
25 |
|
26 |
def cargar_historial():
|
27 |
+
return historial
|
|
|
|
|
|
|
28 |
|
29 |
+
def eliminar_historial(indice):
|
30 |
+
if 0 <= indice < len(historial):
|
31 |
+
historial.pop(indice)
|
32 |
|
33 |
def procesar_texto(texto):
|
34 |
+
respuesta = generator(texto, max_length=100)[0]['generated_text']
|
35 |
+
guardar_historial("Texto", texto, respuesta)
|
36 |
+
return respuesta
|
|
|
|
|
|
|
37 |
|
38 |
def procesar_imagen(imagen):
|
39 |
+
respuesta = generator(f"Process image {imagen.name}", max_length=100)[0]['generated_text']
|
40 |
+
guardar_historial("Imagen", imagen.name, respuesta)
|
41 |
+
return respuesta
|
|
|
|
|
|
|
42 |
|
43 |
def reconocer_voz():
|
44 |
reconocedor = sr.Recognizer()
|
|
|
60 |
|
61 |
st.sidebar.title("Historial")
|
62 |
|
|
|
|
|
|
|
63 |
# Mostrar historial en la barra lateral
|
64 |
+
for i, entrada in enumerate(cargar_historial()):
|
65 |
with st.sidebar.expander(f"{entrada['entrada_usuario']}"):
|
66 |
col1, col2 = st.columns([1, 1])
|
67 |
with col1:
|
68 |
+
if st.button("Respuesta", key=f"respuesta_{i}"):
|
69 |
+
st.session_state.respuesta_seleccionada = entrada['respuesta']
|
70 |
with col2:
|
71 |
+
if st.button("Eliminar", key=f"eliminar_{i}"):
|
72 |
+
eliminar_historial(i)
|
73 |
+
st.experimental_rerun()
|
74 |
|
75 |
st.title("馃 ChatBot")
|
76 |
|
77 |
espacio_contenido_generado = st.empty()
|
78 |
|
79 |
# Mostrar respuesta seleccionada
|
80 |
+
if 'respuesta_seleccionada' in st.session_state:
|
81 |
+
respuesta_seleccionada = st.session_state.respuesta_seleccionada
|
82 |
espacio_contenido_generado.write(respuesta_seleccionada)
|
83 |
if st.button("馃攰 Hablar"):
|
84 |
hablar_texto(respuesta_seleccionada)
|