Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
|
|
3 |
import google.generativeai as genai
|
4 |
from gtts import gTTS
|
5 |
import os
|
@@ -11,41 +12,35 @@ st.set_page_config(layout="wide")
|
|
11 |
api_key = "AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k" # API key proporcionada
|
12 |
|
13 |
def procesar_texto(texto):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
return "No se pudo generar una respuesta."
|
26 |
|
27 |
-
def
|
|
|
|
|
|
|
|
|
28 |
try:
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
else:
|
36 |
-
return "No se pudo procesar la imagen correctamente."
|
37 |
-
except Exception as e:
|
38 |
-
st.error(f"Error al procesar la imagen: {e}")
|
39 |
-
return "No se pudo procesar la imagen."
|
40 |
|
41 |
def hablar_texto(texto):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
tts.save(archivo_audio)
|
46 |
-
os.system(f"mpg321 {archivo_audio}") # Puedes usar otro reproductor como afplay en macOS o VLC
|
47 |
-
except Exception as e:
|
48 |
-
st.error(f"Error al generar audio: {e}")
|
49 |
|
50 |
st.title("🤖 ChatBot")
|
51 |
|
@@ -59,7 +54,7 @@ with open("./style.css") as f:
|
|
59 |
col1, col2 = st.columns([1, 3])
|
60 |
|
61 |
with col1:
|
62 |
-
tipo_entrada = st.selectbox("Selecciona el tipo de entrada", ["Haz una pregunta❓", "🖼️ Subir imagen"])
|
63 |
|
64 |
with col2:
|
65 |
if tipo_entrada == "Haz una pregunta❓":
|
@@ -76,7 +71,21 @@ with col2:
|
|
76 |
if entrada_imagen:
|
77 |
imagen = Image.open(entrada_imagen)
|
78 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
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 |
+
respuesta = modelo.generate_content(contexto_completo)
|
25 |
+
return respuesta.text
|
|
|
26 |
|
27 |
+
def reconocer_voz():
|
28 |
+
reconocedor = sr.Recognizer()
|
29 |
+
with sr.Microphone() as fuente:
|
30 |
+
st.write("Escuchando...")
|
31 |
+
audio = reconocedor.listen(fuente)
|
32 |
try:
|
33 |
+
texto = reconocedor.recognize_google(audio)
|
34 |
+
return texto
|
35 |
+
except sr.UnknownValueError:
|
36 |
+
return "El reconocimiento de voz de Google no pudo entender el audio"
|
37 |
+
except sr.RequestError as e:
|
38 |
+
return f"No se pudieron solicitar resultados del servicio de reconocimiento de voz de Google; {e}"
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def hablar_texto(texto):
|
41 |
+
tts = gTTS(text=texto, lang='es')
|
42 |
+
tts.save("respuesta.mp3")
|
43 |
+
os.system("mpg321 respuesta.mp3") # Puedes usar otro reproductor como afplay en macOS o VLC
|
|
|
|
|
|
|
|
|
44 |
|
45 |
st.title("🤖 ChatBot")
|
46 |
|
|
|
54 |
col1, col2 = st.columns([1, 3])
|
55 |
|
56 |
with col1:
|
57 |
+
tipo_entrada = st.selectbox("Selecciona el tipo de entrada", ["Haz una pregunta❓", "🖼️ Subir imagen", "🎤 Usar micrófono"])
|
58 |
|
59 |
with col2:
|
60 |
if tipo_entrada == "Haz una pregunta❓":
|
|
|
71 |
if entrada_imagen:
|
72 |
imagen = Image.open(entrada_imagen)
|
73 |
st.image(imagen, caption='Imagen subida.', use_column_width=True)
|
74 |
+
contexto_imagen = st.text_input("Proporcióname más contexto sobre la imagen")
|
75 |
+
if contexto_imagen:
|
76 |
+
with st.spinner("Procesando imagen..."):
|
77 |
+
respuesta = procesar_imagen(entrada_imagen, contexto_imagen)
|
78 |
+
espacio_contenido_generado.write(respuesta)
|
79 |
|
80 |
+
elif tipo_entrada == "🎤 Usar micrófono":
|
81 |
+
if st.button("Grabar"):
|
82 |
+
with st.spinner("Escuchando y procesando..."):
|
83 |
+
texto_de_voz = reconocer_voz()
|
84 |
+
if texto_de_voz:
|
85 |
+
entrada_texto = st.text_input("Habla", value=texto_de_voz)
|
86 |
+
if entrada_texto:
|
87 |
+
with st.spinner("Generando respuesta..."):
|
88 |
+
resultado = procesar_texto(entrada_texto)
|
89 |
+
espacio_contenido_generado.write(resultado)
|
90 |
+
if st.button("🔊 Hablar", key="hablar_entrada_voz"):
|
91 |
+
hablar_texto(resultado)
|