Spaces:
Runtime error
Runtime error
mrolando
commited on
Commit
·
e4129b6
1
Parent(s):
3b783d4
first commit
Browse files- .gitignore +3 -0
- Iso_Logotipo_Ceibal.png +0 -0
- app.py +115 -0
- requirements.txt +3 -0
- some_file.txt +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
env
|
| 3 |
+
/venv
|
Iso_Logotipo_Ceibal.png
ADDED
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import gradio as gr
|
| 4 |
+
# Load environment variables from the .env file de forma local
|
| 5 |
+
load_dotenv()
|
| 6 |
+
import base64
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
with open("Iso_Logotipo_Ceibal.png", "rb") as image_file:
|
| 10 |
+
encoded_image = base64.b64encode(image_file.read()).decode()
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
from openai import OpenAI
|
| 14 |
+
|
| 15 |
+
client=OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 16 |
+
api_key=os.environ["OPENAI_API_KEY"]
|
| 17 |
+
|
| 18 |
+
def respond2(image,text):
|
| 19 |
+
# with open('some_file.txt', 'w') as f:
|
| 20 |
+
# f.write(processed_string)
|
| 21 |
+
# Function to encode the image
|
| 22 |
+
def encode_image(image_path):
|
| 23 |
+
with open(image_path, "rb") as image_file:
|
| 24 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 25 |
+
|
| 26 |
+
# Path to your image
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Getting the base64 string
|
| 30 |
+
base64_image = encode_image(image)
|
| 31 |
+
|
| 32 |
+
headers = {
|
| 33 |
+
"Content-Type": "application/json",
|
| 34 |
+
"Authorization": f"Bearer {api_key}"
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
payload = {
|
| 38 |
+
"model": "gpt-4-vision-preview",
|
| 39 |
+
"messages": [
|
| 40 |
+
{
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": [
|
| 43 |
+
{
|
| 44 |
+
"type": "text",
|
| 45 |
+
"text": text
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"type": "image_url",
|
| 49 |
+
"image_url": {
|
| 50 |
+
"url": f"data:image/jpeg;base64,{base64_image}",
|
| 51 |
+
"detail": "low"
|
| 52 |
+
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
]
|
| 56 |
+
}
|
| 57 |
+
],
|
| 58 |
+
"max_tokens": 300
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 63 |
+
|
| 64 |
+
print(response.json())
|
| 65 |
+
return response.json()['choices'][0]['message']['content']
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown(
|
| 72 |
+
"""
|
| 73 |
+
<center>
|
| 74 |
+
<h1>
|
| 75 |
+
Uso de AI para la generación de imagenes a partir de texto.
|
| 76 |
+
</h1>
|
| 77 |
+
<img src='data:image/jpg;base64,{}' width=200px>
|
| 78 |
+
<h2>
|
| 79 |
+
Con este espacio podrás hacer que una AI describa lo que ve en una imagen al responder una pregunta sobre la misma.
|
| 80 |
+
</h2>
|
| 81 |
+
<h2>
|
| 82 |
+
Obtendrás mejores resultados si la pregunta se mantiene simple, por ejemplo, ¿Que se ve en la imagen?
|
| 83 |
+
</h2>
|
| 84 |
+
|
| 85 |
+
</center>
|
| 86 |
+
""".format(
|
| 87 |
+
encoded_image
|
| 88 |
+
)
|
| 89 |
+
)
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column():
|
| 92 |
+
with gr.Row():
|
| 93 |
+
gr.Markdown("Primero debes ingresar la pregunta para la imagen imagen:")
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
prompt = gr.Textbox(
|
| 97 |
+
label="Pregunta, debe ser simple."
|
| 98 |
+
)
|
| 99 |
+
with gr.Row():
|
| 100 |
+
image= gr.Image(type="filepath")
|
| 101 |
+
with gr.Row():
|
| 102 |
+
btn= gr.Button()
|
| 103 |
+
|
| 104 |
+
with gr.Column():
|
| 105 |
+
output = gr.TextArea(
|
| 106 |
+
label="Resultado"
|
| 107 |
+
) # Move the output up too
|
| 108 |
+
# examples = gr.Examples(
|
| 109 |
+
# inputs=[prompt]
|
| 110 |
+
# examples=[["Un perro en el parque", "low quality"]],
|
| 111 |
+
# )
|
| 112 |
+
btn.click(respond2,inputs=[image,prompt], outputs=output)
|
| 113 |
+
|
| 114 |
+
demo.queue()
|
| 115 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|
| 3 |
+
python-dotenv
|
some_file.txt
ADDED
|
File without changes
|