File size: 2,952 Bytes
e4129b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# import gradio as gr
from dotenv import load_dotenv
import gradio as gr
# Load environment variables from the .env file de forma local
load_dotenv()
import base64
import requests

with open("Iso_Logotipo_Ceibal.png", "rb") as image_file:
    encoded_image = base64.b64encode(image_file.read()).decode()

import os
from openai import OpenAI

client=OpenAI(api_key=os.environ["OPENAI_API_KEY"])
api_key=os.environ["OPENAI_API_KEY"]

def respond2(image,text):
    # with open('some_file.txt', 'w') as f:
    #     f.write(processed_string)
    # Function to encode the image
    def encode_image(image_path):
      with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

    # Path to your image
   

    # Getting the base64 string
    base64_image = encode_image(image)

    headers = {
      "Content-Type": "application/json",
      "Authorization": f"Bearer {api_key}"
    }

    payload = {
    "model": "gpt-4-vision-preview",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": text
          },
          {
            "type": "image_url",
            "image_url": {
              "url":  f"data:image/jpeg;base64,{base64_image}",
              "detail": "low"

            }
          }
        ]
      }
    ],
    "max_tokens": 300
  }


    response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)

    print(response.json())
    return response.json()['choices'][0]['message']['content']




with gr.Blocks() as demo:
    gr.Markdown(
        """
    <center>
    <h1>
    Uso de AI para la generación de imagenes a partir de texto.
    </h1>
    <img src='data:image/jpg;base64,{}' width=200px>
    <h2>
    Con este espacio podrás hacer que una AI describa lo que ve en una imagen al responder una pregunta sobre la misma.
    </h2>
    <h2>
     Obtendrás mejores resultados si la pregunta se mantiene simple, por ejemplo, ¿Que se ve en la imagen?
    </h2>
    
    </center>
    """.format(
            encoded_image
        )
    )
    with gr.Row():
        with gr.Column():
            with gr.Row():
                gr.Markdown("Primero debes ingresar la pregunta para la imagen imagen:")

            with gr.Row():
                prompt = gr.Textbox(
                    label="Pregunta, debe ser simple."
                ) 
            with gr.Row():
                image= gr.Image(type="filepath")
            with gr.Row():
                btn= gr.Button()

        with gr.Column():
            output = gr.TextArea(
                label="Resultado"
            )  # Move the output up too
            # examples = gr.Examples(
            #     inputs=[prompt]
            #     examples=[["Un perro en el parque", "low quality"]],
            # )
    btn.click(respond2,inputs=[image,prompt], outputs=output)

demo.queue()
demo.launch()