Spaces:
Running
Running
File size: 2,620 Bytes
337a0be 6094276 e243225 337a0be 6094276 9a1c304 e243225 9a1c304 6094276 337a0be 6094276 337a0be 6094276 e243225 337a0be 6094276 9a1c304 6094276 337a0be 6094276 337a0be 6094276 337a0be d7d9f0c 6094276 d7d9f0c e243225 d7d9f0c e243225 6094276 d7d9f0c 6094276 337a0be d7d9f0c |
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 81 82 83 84 85 86 87 88 89 |
import google.generativeai as genai
import gradio as gr
import numpy as np
import PIL.Image
import pandas as pd
import io
genai.configure(api_key="AIzaSyA7tPavobVN5_3-BJ0qhFT5HVjO4V19QWk")
def ImageChat(image):
# Configuración de la personalidad del sistema
system_prompt = "Sólo puedes responder en idioma español: Sí o No."
# Lista de preguntas preconfiguradas
questions = [
"¿La persona está utilizando auriculares?",
"¿La persona está utilizando capucha o algo que le cubra la cabeza?",
"¿Existe dinero como monedas o billetes que estén el piso?",
"¿La persona se encuentra interactuando con su teléfono móvil?"
]
# load model
model = genai.GenerativeModel("gemini-1.5-flash")
# check image file and convert to a Numpy array
try:
if isinstance(image, np.ndarray):
img = PIL.Image.fromarray(image)
else:
img = PIL.Image.open(io.BytesIO(image.read()))
except Exception as e:
return str(e)
# Initialize results list
results = []
# Process each question
for question in questions:
full_prompt = f"{system_prompt}\n\n{question}"
response = model.generate_content([full_prompt, img])
results.append(response.text)
# Create a DataFrame to display results as a table
df = pd.DataFrame({"Pregunta": questions, "Respuesta": results})
return df
def load_image(image_path):
with open(image_path, "rb") as image_file:
return image_file.read()
# Preloaded images
preloaded_images = {
"Imagen Ejemplo 1": "chat_with_image.png",
"Imagen Ejemplo 2": "chatwithimg_2.jpg",
"Imagen Ejemplo 3": "chatwithimg3.png"
}
def get_selected_image(image_name):
return load_image(preloaded_images[image_name])
# Define Gradio interface
image_selector = gr.Radio(
choices=list(preloaded_images.keys()),
label="Selecciona una imagen"
)
image_display = gr.Image(label="Imagen", type="file")
def update_image(selected_image_name):
image_data = get_selected_image(selected_image_name)
return gr.Image.update(value=image_data)
image_selector.change(fn=update_image, inputs=image_selector, outputs=image_display)
app = gr.Interface(
fn=ImageChat,
inputs=image_display,
outputs=gr.Dataframe(headers=["Pregunta", "Respuesta"], label="Resultados"),
title="Análisis de Imagen",
theme="Taithrah/Minimal"
)
gr.Interface(
fn=lambda: "Selecciona una imagen del carrusel y haz clic en 'Submit' para analizarla.",
inputs=[image_selector, image_display],
outputs="text"
).launch()
|