# 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( """

Uso de AI para la descripción de imágenes.

Con este espacio podrás hacer que una AI describa lo que ve en una imagen al responder una pregunta sobre la misma.

Obtendrás mejores resultados si la pregunta se mantiene simple, por ejemplo, ¿Que se ve en la imagen?

""".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()