Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,21 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
import os
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
# Configuraci贸n de la API key para Google Generative AI
|
12 |
-
api_key = "AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k" # API key proporcionada
|
13 |
-
|
14 |
-
def procesar_texto(texto):
|
15 |
-
genai.configure(api_key=api_key)
|
16 |
-
modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
|
17 |
-
respuesta = modelo.generate_content(texto, language='es') # Aseg煤rate de especificar el idioma
|
18 |
-
return respuesta.text
|
19 |
-
|
20 |
-
def procesar_imagen(imagen, contexto):
|
21 |
-
genai.configure(api_key=api_key)
|
22 |
-
modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
|
23 |
-
contexto_completo = f"Estoy procesando una imagen con el siguiente contexto: {contexto}. La imagen se llama {imagen.name}."
|
24 |
-
try:
|
25 |
-
respuesta = modelo.generate_content(contexto_completo)
|
26 |
-
return respuesta.text
|
27 |
-
except Exception as e:
|
28 |
-
return f"Error al procesar la imagen: {str(e)}"
|
29 |
|
30 |
-
def
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
return texto
|
38 |
-
except sr.UnknownValueError:
|
39 |
-
return "El reconocimiento de voz de Google no pudo entender el audio"
|
40 |
-
except sr.RequestError as e:
|
41 |
-
return f"No se pudieron solicitar resultados del servicio de reconocimiento de voz de Google; {e}"
|
42 |
|
43 |
def hablar_texto(texto):
|
44 |
tts = gTTS(text=texto, lang='es')
|
@@ -68,7 +45,8 @@ with col2:
|
|
68 |
entrada_texto = st.text_input("Ingresa tu pregunta aqu铆")
|
69 |
if entrada_texto:
|
70 |
with st.spinner("Generando respuesta..."):
|
71 |
-
|
|
|
72 |
espacio_contenido_generado.write(resultado)
|
73 |
if st.button("馃攰 Hablar", key="hablar_entrada_texto"):
|
74 |
hablar_texto(resultado)
|
@@ -78,21 +56,20 @@ with col2:
|
|
78 |
if entrada_imagen:
|
79 |
imagen = Image.open(entrada_imagen)
|
80 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
respuesta = procesar_imagen(entrada_imagen, contexto_imagen)
|
85 |
-
espacio_contenido_generado.write(respuesta)
|
86 |
|
87 |
elif tipo_entrada == "馃帳 Usar micr贸fono":
|
88 |
if st.button("Grabar"):
|
89 |
with st.spinner("Escuchando y procesando..."):
|
90 |
-
|
|
|
91 |
if texto_de_voz:
|
92 |
entrada_texto = st.text_input("Habla", value=texto_de_voz)
|
93 |
if entrada_texto:
|
94 |
with st.spinner("Generando respuesta..."):
|
95 |
-
resultado =
|
96 |
espacio_contenido_generado.write(resultado)
|
97 |
if st.button("馃攰 Hablar", key="hablar_entrada_voz"):
|
98 |
hablar_texto(resultado)
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
+
from google.cloud import vision
|
4 |
+
from google.cloud.vision_v1 import types
|
5 |
+
import io
|
6 |
import os
|
7 |
+
from gtts import gTTS
|
8 |
|
9 |
+
# Configura la clave de autenticaci贸n de Google Cloud
|
10 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "ruta_a_tu_archivo_de_credenciales.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def reconocer_imagen(imagen):
|
13 |
+
cliente = vision.ImageAnnotatorClient()
|
14 |
+
contenido = imagen.read()
|
15 |
+
image = types.Image(content=contenido)
|
16 |
+
respuesta = cliente.label_detection(image=image)
|
17 |
+
etiquetas = [etiqueta.description for etiqueta in respuesta.label_annotations]
|
18 |
+
return ', '.join(etiquetas)
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def hablar_texto(texto):
|
21 |
tts = gTTS(text=texto, lang='es')
|
|
|
45 |
entrada_texto = st.text_input("Ingresa tu pregunta aqu铆")
|
46 |
if entrada_texto:
|
47 |
with st.spinner("Generando respuesta..."):
|
48 |
+
# Aqu铆 ir铆a la l贸gica para procesar el texto si es necesario
|
49 |
+
resultado = entrada_texto # Simulaci贸n de respuesta
|
50 |
espacio_contenido_generado.write(resultado)
|
51 |
if st.button("馃攰 Hablar", key="hablar_entrada_texto"):
|
52 |
hablar_texto(resultado)
|
|
|
56 |
if entrada_imagen:
|
57 |
imagen = Image.open(entrada_imagen)
|
58 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
59 |
+
with st.spinner("Procesando imagen..."):
|
60 |
+
etiquetas = reconocer_imagen(entrada_imagen)
|
61 |
+
espacio_contenido_generado.write(f"Etiquetas detectadas: {etiquetas}")
|
|
|
|
|
62 |
|
63 |
elif tipo_entrada == "馃帳 Usar micr贸fono":
|
64 |
if st.button("Grabar"):
|
65 |
with st.spinner("Escuchando y procesando..."):
|
66 |
+
# Aqu铆 ir铆a la l贸gica para reconocer la voz
|
67 |
+
texto_de_voz = "Texto simulado de voz" # Simulaci贸n de texto de voz
|
68 |
if texto_de_voz:
|
69 |
entrada_texto = st.text_input("Habla", value=texto_de_voz)
|
70 |
if entrada_texto:
|
71 |
with st.spinner("Generando respuesta..."):
|
72 |
+
resultado = entrada_texto # Simulaci贸n de respuesta
|
73 |
espacio_contenido_generado.write(resultado)
|
74 |
if st.button("馃攰 Hablar", key="hablar_entrada_voz"):
|
75 |
hablar_texto(resultado)
|