File size: 2,827 Bytes
6a2680d
 
5de271e
f520770
5de271e
11f9529
5de271e
 
58947a2
5de271e
 
 
 
 
 
 
58947a2
 
6a2680d
5de271e
 
 
11f9529
 
 
 
 
ca85e4c
11f9529
 
 
 
 
 
e50eaad
fe1ed8d
0e2770b
58947a2
 
 
0e2770b
58947a2
6a2680d
58947a2
6a2680d
c0347bb
 
 
 
 
 
 
58947a2
 
c0347bb
 
 
58947a2
fe1ed8d
c0347bb
58947a2
 
c0347bb
 
5de271e
58947a2
 
c0347bb
fe1ed8d
c0347bb
 
 
 
 
f520770
5754f17
58947a2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import streamlit as st
from PIL import Image
import google.generativeai as genai
from gtts import gTTS
import os
import io

# Configuración de la página de Streamlit
st.set_page_config(page_title="🤖 ChatBot", layout="wide")

# Configuración de la API key para Google Generative AI
api_key = "AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k"  # API key proporcionada

def procesar_texto(texto):
    genai.configure(api_key=api_key)
    modelo = genai.GenerativeModel('gemini-1.5-pro-latest')
    respuesta = modelo.generate_content(texto)
    return respuesta.text

def procesar_imagen(imagen):
    genai.configure(api_key=api_key)
    modelo = genai.GenerativeModel('gemini-1.5-pro-latest')

    # Convertir la imagen a bytes
    imagen_bytes = io.BytesIO()
    imagen.save(imagen_bytes, format='PNG')
    imagen_bytes.seek(0)
    
    try:
        # Procesar imagen (esto depende de la API que uses, adaptarlo si es necesario)
        respuesta = modelo.generate_content(imagen_bytes.read())
        return respuesta.text
    except Exception as e:
        return f"Error al procesar la imagen: {e}"

def hablar_texto(texto):
    try:
        tts = gTTS(text=texto, lang='es')
        tts.save("respuesta.mp3")
        os.system("mpg321 respuesta.mp3")  # Usa mpg321 para reproducir el archivo
    except Exception as e:
        st.error(f"Error al generar el audio: {e}")

st.title("🤖 ¡Bienvenido a ChatBot!")

espacio_contenido_generado = st.empty()

# Cargar y aplicar CSS personalizado
with open("./style.css") as f:
    st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

# Contenido principal
st.write("¡Hola! Soy tu asistente virtual. ¿Cómo puedo ayudarte hoy? 😊")

col1, col2 = st.columns([1, 3])

with col1:
    tipo_entrada = st.selectbox("Selecciona el tipo de entrada", ["Haz una pregunta ❓", "🖼️ Subir imagen"])

with col2:
    if tipo_entrada == "Haz una pregunta ❓":
        entrada_texto = st.text_input("Ingresa tu pregunta aquí:")
        if entrada_texto:
            with st.spinner("Generando respuesta..."):
                resultado = procesar_texto(entrada_texto)
            espacio_contenido_generado.write(f"**Respuesta:** {resultado}")
            if st.button("🔊 Escuchar respuesta"):
                hablar_texto(resultado)

    elif tipo_entrada == "🖼️ Subir imagen":
        entrada_imagen = st.file_uploader("Sube una imagen", type=["jpg", "png", "jpeg"])
        if entrada_imagen:
            imagen = Image.open(entrada_imagen)
            st.image(imagen, caption='Imagen subida.', use_column_width=True)
            with st.spinner("Procesando imagen..."):
                respuesta = procesar_imagen(imagen)
            espacio_contenido_generado.write(f"**Respuesta de la imagen:** {respuesta}")

st.write("¡Gracias por usar el ChatBot! 😊")