File size: 2,545 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
 
 
 
 
 
 
 
 
 
 
 
4daa84a
d7d9f0c
74009d7
 
 
 
 
 
 
 
 
4daa84a
74009d7
 
 
4daa84a
 
 
 
 
 
74009d7
4daa84a
74009d7
 
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
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 PIL.Image.open(io.BytesIO(load_image(preloaded_images[image_name])))

with gr.Blocks() as app:
    image_selector = gr.Radio(
        choices=list(preloaded_images.keys()),
        label="Selecciona una imagen"
    )

    image_display = gr.Image(label="Imagen", type="pil")

    def update_image(selected_image_name):
        return get_selected_image(selected_image_name)

    image_selector.change(fn=update_image, inputs=image_selector, outputs=image_display)

    analyze_button = gr.Button("Analizar Imagen")

    results_display = gr.Dataframe(headers=["Pregunta", "Respuesta"], label="Resultados")

    def analyze_image(image):
        return ImageChat(image)

    analyze_button.click(fn=analyze_image, inputs=image_display, outputs=results_display)

app.launch()