riabayonaor commited on
Commit
0cd019f
1 Parent(s): b673df1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -48
app.py CHANGED
@@ -1,52 +1,53 @@
1
- import os
2
- import io
3
  import google.generativeai as genai
4
  from PIL import Image
5
- import streamlit as st
6
 
7
- # Configuración de la API
8
- gemini_api_key = os.getenv("GEMINI_API_KEY")
9
- if not gemini_api_key:
10
- st.error("La API Key de Google Generative AI no está configurada.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  else:
12
- genai.configure(api_key=gemini_api_key)
13
- model = genai.GenerativeModel(model_name="gemini-1.5-flash")
14
-
15
- def identify_object(image_bytes):
16
- try:
17
- image_data = {
18
- "mime_type": "image/jpeg",
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)