Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,53 @@
|
|
1 |
-
import
|
2 |
-
import io
|
3 |
import google.generativeai as genai
|
4 |
from PIL import Image
|
5 |
-
import
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
else:
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
"data": image_bytes
|
20 |
-
}
|
21 |
-
content = [
|
22 |
-
{"image_blob": image_data},
|
23 |
-
{"text": "What is in this image?"}
|
24 |
-
]
|
25 |
-
response = model.generate_content({"content": content})
|
26 |
-
return response
|
27 |
-
except Exception as e:
|
28 |
-
st.error(f"Error al identificar la imagen: {e}")
|
29 |
-
return None
|
30 |
-
|
31 |
-
def main():
|
32 |
-
st.title("Identificaci贸n de Objetos con IA")
|
33 |
-
|
34 |
-
uploaded_file = st.file_uploader("Sube una imagen", type=["jpg", "jpeg", "png"])
|
35 |
-
|
36 |
-
if uploaded_file is not None:
|
37 |
-
image = Image.open(uploaded_file)
|
38 |
-
st.image(image, caption="Imagen Cargada", use_column_width=True)
|
39 |
-
|
40 |
-
st.write("Identificando...")
|
41 |
-
image_bytes = io.BytesIO()
|
42 |
-
image.save(image_bytes, format='JPEG')
|
43 |
-
image_bytes = image_bytes.getvalue()
|
44 |
-
|
45 |
-
result = identify_object(image_bytes)
|
46 |
-
|
47 |
-
if result:
|
48 |
-
st.write("Resultado:")
|
49 |
-
st.write(result)
|
50 |
-
|
51 |
-
if __name__ == "__main__":
|
52 |
-
main()
|
|
|
1 |
+
import streamlit as st
|
|
|
2 |
import google.generativeai as genai
|
3 |
from PIL import Image
|
4 |
+
import io
|
5 |
|
6 |
+
# Configurar la API de Gemini
|
7 |
+
api_key = os.getenv('GEMINI_API_KEY')
|
8 |
+
genai.configure(api_key=api_key)
|
9 |
+
|
10 |
+
# Funci贸n para generar contenido a partir de texto
|
11 |
+
def generate_text(prompt):
|
12 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
13 |
+
response = model.generate_content(prompt)
|
14 |
+
return response.text
|
15 |
+
|
16 |
+
# Funci贸n para generar contenido a partir de texto e imagen
|
17 |
+
def generate_text_from_image(image, prompt):
|
18 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
19 |
+
response = model.generate_content([prompt, image], stream=True)
|
20 |
+
return response.text
|
21 |
+
|
22 |
+
# Configuraci贸n de la aplicaci贸n Streamlit
|
23 |
+
st.title('Aplicaci贸n con Gemini Vision')
|
24 |
+
st.write('Esta es una aplicaci贸n de demostraci贸n utilizando la API de Gemini Vision.')
|
25 |
+
|
26 |
+
# Cargar imagen
|
27 |
+
uploaded_file = st.file_uploader("Elige una imagen", type=["jpg", "jpeg", "png"])
|
28 |
+
|
29 |
+
if uploaded_file is not None:
|
30 |
+
# Mostrar la imagen cargada
|
31 |
+
image = Image.open(uploaded_file)
|
32 |
+
st.image(image, caption='Imagen Cargada', use_column_width=True)
|
33 |
+
|
34 |
+
# Texto de entrada del usuario
|
35 |
+
prompt = st.text_input("Introduce el texto para generar la respuesta:")
|
36 |
+
|
37 |
+
if st.button("Generar Respuesta"):
|
38 |
+
# Convertir la imagen a bytes
|
39 |
+
img_bytes = io.BytesIO()
|
40 |
+
image.save(img_bytes, format='JPEG')
|
41 |
+
img_bytes = img_bytes.getvalue()
|
42 |
+
|
43 |
+
# Generar respuesta a partir de la imagen y el texto
|
44 |
+
response = generate_text_from_image(img_bytes, prompt)
|
45 |
+
st.markdown(response)
|
46 |
else:
|
47 |
+
# Texto de entrada del usuario
|
48 |
+
prompt = st.text_input("Introduce el texto para generar la respuesta:")
|
49 |
+
|
50 |
+
if st.button("Generar Respuesta"):
|
51 |
+
# Generar respuesta a partir del texto
|
52 |
+
response = generate_text(prompt)
|
53 |
+
st.markdown(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|