Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,48 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
-
|
4 |
-
|
5 |
-
import io
|
6 |
-
import os
|
7 |
from gtts import gTTS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def hablar_texto(texto):
|
21 |
tts = gTTS(text=texto, lang='es')
|
@@ -45,8 +72,7 @@ with col2:
|
|
45 |
entrada_texto = st.text_input("Ingresa tu pregunta aqu铆")
|
46 |
if entrada_texto:
|
47 |
with st.spinner("Generando respuesta..."):
|
48 |
-
|
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)
|
@@ -57,19 +83,18 @@ with col2:
|
|
57 |
imagen = Image.open(entrada_imagen)
|
58 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
59 |
with st.spinner("Procesando imagen..."):
|
60 |
-
|
61 |
-
espacio_contenido_generado.write(
|
62 |
|
63 |
elif tipo_entrada == "馃帳 Usar micr贸fono":
|
64 |
if st.button("Grabar"):
|
65 |
with st.spinner("Escuchando y procesando..."):
|
66 |
-
|
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
|
73 |
espacio_contenido_generado.write(resultado)
|
74 |
if st.button("馃攰 Hablar", key="hablar_entrada_voz"):
|
75 |
hablar_texto(resultado)
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
+
import speech_recognition as sr
|
4 |
+
import google.generativeai as genai
|
|
|
|
|
5 |
from gtts import gTTS
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Configuraci贸n de la p谩gina de Streamlit
|
9 |
+
st.set_page_config(layout="wide")
|
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 |
+
try:
|
18 |
+
respuesta = modelo.generate_content(texto)
|
19 |
+
return respuesta.text
|
20 |
+
except Exception as e:
|
21 |
+
st.error(f"Error al procesar el texto: {str(e)}")
|
22 |
+
return "No se pudo generar una respuesta."
|
23 |
|
24 |
+
def procesar_imagen(imagen):
|
25 |
+
genai.configure(api_key=api_key)
|
26 |
+
modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
|
27 |
+
try:
|
28 |
+
respuesta = modelo.generate_content(imagen.name) # Ajusta seg煤n la API
|
29 |
+
return respuesta.text
|
30 |
+
except Exception as e:
|
31 |
+
st.error(f"Error al procesar la imagen: {str(e)}")
|
32 |
+
return "No se pudo procesar la imagen."
|
33 |
|
34 |
+
def reconocer_voz():
|
35 |
+
reconocedor = sr.Recognizer()
|
36 |
+
try:
|
37 |
+
with sr.Microphone() as fuente:
|
38 |
+
st.write("Escuchando...")
|
39 |
+
audio = reconocedor.listen(fuente)
|
40 |
+
texto = reconocedor.recognize_google(audio)
|
41 |
+
return texto
|
42 |
+
except sr.UnknownValueError:
|
43 |
+
return "El reconocimiento de voz de Google no pudo entender el audio."
|
44 |
+
except sr.RequestError as e:
|
45 |
+
return f"No se pudieron solicitar resultados del servicio de reconocimiento de voz de Google; {e}"
|
46 |
|
47 |
def hablar_texto(texto):
|
48 |
tts = gTTS(text=texto, lang='es')
|
|
|
72 |
entrada_texto = st.text_input("Ingresa tu pregunta aqu铆")
|
73 |
if entrada_texto:
|
74 |
with st.spinner("Generando respuesta..."):
|
75 |
+
resultado = procesar_texto(entrada_texto)
|
|
|
76 |
espacio_contenido_generado.write(resultado)
|
77 |
if st.button("馃攰 Hablar", key="hablar_entrada_texto"):
|
78 |
hablar_texto(resultado)
|
|
|
83 |
imagen = Image.open(entrada_imagen)
|
84 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
85 |
with st.spinner("Procesando imagen..."):
|
86 |
+
respuesta = procesar_imagen(entrada_imagen)
|
87 |
+
espacio_contenido_generado.write(respuesta)
|
88 |
|
89 |
elif tipo_entrada == "馃帳 Usar micr贸fono":
|
90 |
if st.button("Grabar"):
|
91 |
with st.spinner("Escuchando y procesando..."):
|
92 |
+
texto_de_voz = reconocer_voz()
|
|
|
93 |
if texto_de_voz:
|
94 |
entrada_texto = st.text_input("Habla", value=texto_de_voz)
|
95 |
if entrada_texto:
|
96 |
with st.spinner("Generando respuesta..."):
|
97 |
+
resultado = procesar_texto(entrada_texto)
|
98 |
espacio_contenido_generado.write(resultado)
|
99 |
if st.button("馃攰 Hablar", key="hablar_entrada_voz"):
|
100 |
hablar_texto(resultado)
|