jeysshon commited on
Commit
58947a2
·
verified ·
1 Parent(s): 5de271e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -51
app.py CHANGED
@@ -1,12 +1,11 @@
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
@@ -14,46 +13,25 @@ api_key = "AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k" # API key proporcionada
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')
49
- archivo_audio = "respuesta.mp3"
50
  try:
51
- tts.save(archivo_audio)
52
- os.system(f"mpg321 {archivo_audio}") # Puedes usar otro reproductor como afplay en macOS o VLC
 
53
  except Exception as e:
54
- st.error(f"Error al generar audio: {str(e)}")
55
 
56
- st.title("🤖 ChatBot")
57
 
58
  espacio_contenido_generado = st.empty()
59
 
@@ -62,19 +40,21 @@ with open("./style.css") as f:
62
  st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
63
 
64
  # Contenido principal
 
 
65
  col1, col2 = st.columns([1, 3])
66
 
67
  with col1:
68
- tipo_entrada = st.selectbox("Selecciona el tipo de entrada", ["Haz una pregunta❓", "🖼️ Subir imagen", "🎤 Usar micrófono"])
69
 
70
  with col2:
71
- if tipo_entrada == "Haz una pregunta❓":
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)
79
 
80
  elif tipo_entrada == "🖼️ Subir imagen":
@@ -84,17 +64,7 @@ with col2:
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)
 
1
  import streamlit as st
2
  from PIL import Image
 
3
  import google.generativeai as genai
4
  from gtts import gTTS
5
  import os
6
 
7
  # Configuración de la página de Streamlit
8
+ st.set_page_config(page_title="🤖 ChatBot", layout="wide")
9
 
10
  # Configuración de la API key para Google Generative AI
11
  api_key = "AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k" # API key proporcionada
 
13
  def procesar_texto(texto):
14
  genai.configure(api_key=api_key)
15
  modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
16
+ respuesta = modelo.generate_content(texto)
17
+ return respuesta.text
 
 
 
 
18
 
19
  def procesar_imagen(imagen):
20
  genai.configure(api_key=api_key)
21
  modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
22
+ # Procesa la imagen como un archivo binario
23
+ respuesta = modelo.generate_content(imagen.read())
24
+ return respuesta.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def hablar_texto(texto):
 
 
27
  try:
28
+ tts = gTTS(text=texto, lang='es')
29
+ tts.save("respuesta.mp3")
30
+ os.system("mpg321 respuesta.mp3") # Usa mpg321 para reproducir el archivo
31
  except Exception as e:
32
+ st.error(f"Error al generar el audio: {e}")
33
 
34
+ st.title("🤖 ¡Bienvenido a ChatBot!")
35
 
36
  espacio_contenido_generado = st.empty()
37
 
 
40
  st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
41
 
42
  # Contenido principal
43
+ st.write("¡Hola! Soy tu asistente virtual. ¿Cómo puedo ayudarte hoy? 😊")
44
+
45
  col1, col2 = st.columns([1, 3])
46
 
47
  with col1:
48
+ tipo_entrada = st.selectbox("Selecciona el tipo de entrada", ["Haz una pregunta ❓", "🖼️ Subir imagen"])
49
 
50
  with col2:
51
+ if tipo_entrada == "Haz una pregunta ❓":
52
+ entrada_texto = st.text_input("Ingresa tu pregunta aquí:")
53
  if entrada_texto:
54
  with st.spinner("Generando respuesta..."):
55
  resultado = procesar_texto(entrada_texto)
56
+ espacio_contenido_generado.write(f"**Respuesta:** {resultado}")
57
+ if st.button("🔊 Escuchar respuesta"):
58
  hablar_texto(resultado)
59
 
60
  elif tipo_entrada == "🖼️ Subir imagen":
 
64
  st.image(imagen, caption='Imagen subida.', use_column_width=True)
65
  with st.spinner("Procesando imagen..."):
66
  respuesta = procesar_imagen(entrada_imagen)
67
+ espacio_contenido_generado.write(f"**Respuesta de la imagen:** {respuesta}")
68
+
69
+ st.write("¡Gracias por usar el ChatBot! 😊")
70