Delete app.py
Browse files
app.py
DELETED
@@ -1,171 +0,0 @@
|
|
1 |
-
import sqlite3
|
2 |
-
import streamlit as st
|
3 |
-
from PIL import Image
|
4 |
-
import speech_recognition as sr
|
5 |
-
import google.generativeai as genai
|
6 |
-
import pyttsx3
|
7 |
-
from googletrans import Translator # Importa el traductor
|
8 |
-
|
9 |
-
# Clave API para Google Generative AI
|
10 |
-
api_key = 'AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k'
|
11 |
-
params = {'key': api_key}
|
12 |
-
|
13 |
-
# Configuración de la base de datos SQLite
|
14 |
-
def obtener_conexion():
|
15 |
-
conexion = sqlite3.connect('historial_busquedas.db')
|
16 |
-
return conexion
|
17 |
-
|
18 |
-
def crear_tablas():
|
19 |
-
conexion = obtener_conexion()
|
20 |
-
with conexion:
|
21 |
-
conexion.execute("""
|
22 |
-
CREATE TABLE IF NOT EXISTS historial (
|
23 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
24 |
-
modo_entrada TEXT,
|
25 |
-
entrada_usuario TEXT,
|
26 |
-
respuesta TEXT
|
27 |
-
)
|
28 |
-
""")
|
29 |
-
|
30 |
-
def guardar_historial(modo_entrada, entrada_usuario, respuesta):
|
31 |
-
conexion = obtener_conexion()
|
32 |
-
with conexion:
|
33 |
-
conexion.execute("""
|
34 |
-
INSERT INTO historial (modo_entrada, entrada_usuario, respuesta)
|
35 |
-
VALUES (?, ?, ?)
|
36 |
-
""", (modo_entrada, entrada_usuario, respuesta))
|
37 |
-
|
38 |
-
def cargar_historial():
|
39 |
-
conexion = obtener_conexion()
|
40 |
-
with conexion:
|
41 |
-
entradas_historial = conexion.execute("""
|
42 |
-
SELECT * FROM historial ORDER BY id DESC
|
43 |
-
""").fetchall()
|
44 |
-
return entradas_historial
|
45 |
-
|
46 |
-
def eliminar_historial(id_entrada):
|
47 |
-
conexion = obtener_conexion()
|
48 |
-
with conexion:
|
49 |
-
conexion.execute("""
|
50 |
-
DELETE FROM historial WHERE id = ?
|
51 |
-
""", (id_entrada,))
|
52 |
-
|
53 |
-
# Inicializar tablas en SQLite
|
54 |
-
crear_tablas()
|
55 |
-
|
56 |
-
def traducir_a_espanol(texto):
|
57 |
-
traductor = Translator()
|
58 |
-
try:
|
59 |
-
traduccion = traductor.translate(texto, dest='es')
|
60 |
-
return traduccion.text
|
61 |
-
except Exception as e:
|
62 |
-
st.error(f"Error al traducir el texto: {e}")
|
63 |
-
return texto
|
64 |
-
|
65 |
-
def procesar_texto(texto):
|
66 |
-
genai.configure(api_key=api_key)
|
67 |
-
modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
|
68 |
-
respuesta = modelo.generate_content(texto)
|
69 |
-
resultado = respuesta.text
|
70 |
-
resultado_es = traducir_a_espanol(resultado) # Traduce la respuesta al español
|
71 |
-
guardar_historial("Texto", texto, resultado_es)
|
72 |
-
return resultado_es
|
73 |
-
|
74 |
-
def procesar_imagen(imagen):
|
75 |
-
genai.configure(api_key=api_key)
|
76 |
-
modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
|
77 |
-
respuesta = modelo.generate_content(imagen.name)
|
78 |
-
resultado = respuesta.text
|
79 |
-
resultado_es = traducir_a_espanol(resultado) # Traduce la respuesta al español
|
80 |
-
guardar_historial("Imagen", imagen.name, resultado_es)
|
81 |
-
return resultado_es
|
82 |
-
|
83 |
-
def reconocer_voz():
|
84 |
-
reconocedor = sr.Recognizer()
|
85 |
-
with sr.Microphone() as fuente:
|
86 |
-
st.write("Escuchando...")
|
87 |
-
audio = reconocedor.listen(fuente)
|
88 |
-
try:
|
89 |
-
texto = reconocedor.recognize_google(audio)
|
90 |
-
return texto
|
91 |
-
except sr.UnknownValueError:
|
92 |
-
return "El reconocimiento de voz de Google no pudo entender el audio"
|
93 |
-
except sr.RequestError as e:
|
94 |
-
return f"No se pudieron solicitar resultados del servicio de reconocimiento de voz de Google; {e}"
|
95 |
-
|
96 |
-
def hablar_texto(texto):
|
97 |
-
motor = pyttsx3.init()
|
98 |
-
motor.say(texto)
|
99 |
-
motor.runAndWait()
|
100 |
-
|
101 |
-
st.set_page_config(layout="wide")
|
102 |
-
st.sidebar.title("Historial")
|
103 |
-
|
104 |
-
# Cargar el historial
|
105 |
-
historial = cargar_historial()
|
106 |
-
respuesta_seleccionada = None
|
107 |
-
|
108 |
-
# Mostrar historial en la barra lateral
|
109 |
-
for entrada in historial:
|
110 |
-
with st.sidebar.expander(f"{entrada[2]}"):
|
111 |
-
col1, col2 = st.columns([1, 1])
|
112 |
-
with col1:
|
113 |
-
if st.button("Respuesta", key=str(entrada[0])):
|
114 |
-
respuesta_seleccionada = entrada[3]
|
115 |
-
with col2:
|
116 |
-
if st.button("Eliminar", key=f"eliminar_{entrada[0]}"):
|
117 |
-
eliminar_historial(entrada[0])
|
118 |
-
st.experimental_rerun()
|
119 |
-
|
120 |
-
# Configuración del título
|
121 |
-
st.title("🤖 ChatBot")
|
122 |
-
|
123 |
-
espacio_contenido_generado = st.empty()
|
124 |
-
|
125 |
-
# Mostrar respuesta seleccionada
|
126 |
-
if respuesta_seleccionada:
|
127 |
-
espacio_contenido_generado.write(respuesta_seleccionada)
|
128 |
-
if st.button("🔊 Hablar"):
|
129 |
-
hablar_texto(respuesta_seleccionada)
|
130 |
-
|
131 |
-
# Cargar y aplicar CSS personalizado
|
132 |
-
with open("./style.css") as f:
|
133 |
-
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
134 |
-
|
135 |
-
# Contenido principal
|
136 |
-
col1, col2 = st.columns([1, 3])
|
137 |
-
|
138 |
-
with col1:
|
139 |
-
tipo_entrada = st.selectbox("Selecciona el tipo de entrada", ["Haz una pregunta❓", "🖼️ Subir imagen", "🎤 Usar micrófono"])
|
140 |
-
|
141 |
-
with col2:
|
142 |
-
if tipo_entrada == "Haz una pregunta❓":
|
143 |
-
entrada_texto = st.text_input("Ingresa tu pregunta aquí")
|
144 |
-
if entrada_texto:
|
145 |
-
with st.spinner("Generando respuesta..."):
|
146 |
-
resultado = procesar_texto(entrada_texto)
|
147 |
-
espacio_contenido_generado.write(resultado)
|
148 |
-
if st.button("🔊 Hablar", key="hablar_entrada_texto"):
|
149 |
-
hablar_texto(resultado)
|
150 |
-
|
151 |
-
elif tipo_entrada == "🖼️ Subir imagen":
|
152 |
-
entrada_imagen = st.file_uploader("Sube una imagen", type=["jpg", "png", "jpeg"])
|
153 |
-
if entrada_imagen:
|
154 |
-
imagen = Image.open(entrada_imagen)
|
155 |
-
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
156 |
-
with st.spinner("Procesando imagen..."):
|
157 |
-
respuesta = procesar_imagen(entrada_imagen)
|
158 |
-
espacio_contenido_generado.write(respuesta)
|
159 |
-
|
160 |
-
elif tipo_entrada == "🎤 Usar micrófono":
|
161 |
-
if st.button("Grabar"):
|
162 |
-
with st.spinner("Escuchando y procesando..."):
|
163 |
-
texto_de_voz = reconocer_voz()
|
164 |
-
if texto_de_voz:
|
165 |
-
st.text_input("Texto reconocido", value=texto_de_voz) # Mostrar texto reconocido
|
166 |
-
if st.button("Generar respuesta"):
|
167 |
-
with st.spinner("Generando respuesta..."):
|
168 |
-
resultado = procesar_texto(texto_de_voz)
|
169 |
-
espacio_contenido_generado.write(resultado)
|
170 |
-
if st.button("🔊 Hablar", key="hablar_entrada_voz"):
|
171 |
-
hablar_texto(resultado)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|