Spaces:
Running
Running
import gradio as gr | |
import requests | |
import time | |
import json | |
import os | |
import urllib.parse | |
import re | |
import base64 | |
import mimetypes | |
def image_to_data_url(image_path): | |
mimetypes.init() | |
mime_type, _ = mimetypes.guess_type(image_path) | |
if mime_type is None: | |
mime_type = "image/unknown" | |
if image_path.lower().endswith('.webp'): | |
mime_type = "image/webp" | |
with open(image_path, "rb") as image_file: | |
encoded_string = base64.b64encode(image_file.read()).decode('utf-8') | |
data_url = f"data:{mime_type};base64,{encoded_string}" | |
return data_url | |
def convert_url_filename(url): | |
def encode_filename(match): | |
full_filename = match.group(1) | |
return urllib.parse.quote(full_filename) | |
new_url = re.sub(r'/([^/]+)$', lambda m: '/' + encode_filename(m), url) | |
return new_url | |
def process_image(image_path, prompt): | |
data_url = image_to_data_url(image_path) | |
json_data = { | |
'response_format': { | |
'type': 'json_schema', | |
'json_schema': { | |
'name': 'image_critique', | |
'strict': True, | |
'schema': { | |
'type': 'object', | |
'properties': { | |
'critique': { | |
'type': 'string', | |
}, | |
}, | |
'required': [ | |
'critique', | |
], | |
'additionalProperties': False, | |
}, | |
}, | |
}, | |
'chatSettings': { | |
'model': 'gpt-4o-vision', | |
'temperature': 0.7, | |
'contextLength': 16385, | |
'includeProfileContext': False, | |
'includeWorkspaceInstructions': False, | |
'embeddingsProvider': 'openai', | |
}, | |
'messages': [ | |
{ | |
'role': 'user', | |
'content': [ | |
{ | |
'type': 'image_url', | |
'image_url': { | |
'url': data_url | |
}, | |
}, | |
{ | |
'type': 'text', | |
'text': "Напиши на русском языке промпт для создания похожего изображения", | |
}, | |
], | |
}, | |
], | |
'customModelId': '', | |
} | |
api_url = os.environ['api_url'] | |
print(api_url) | |
response = requests.post(api_url, json=json_data) | |
print("--->", response.status_code) | |
print("--->", response.text) | |
return response.json()['critique'] | |
css = """ | |
.gradio-container { | |
min-width: 100% !important; | |
} | |
#image_output { | |
height: 500px; | |
} | |
#generate { | |
width: 100%; | |
background: #e253dd !important; | |
border: none; | |
border-radius: 50px; | |
outline: none !important; | |
color: white; | |
} | |
#generate:hover { | |
background: #de6bda !important; | |
outline: none !important; | |
color: #fff; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
with gr.Row(): | |
with gr.Column(): | |
image_input = gr.Image(show_download_button=False, interactive=True, label='Изображение:', elem_id='image_output', type='filepath') | |
text_button = gr.Button("Запустить нейросеть", variant='primary', elem_id="generate") | |
with gr.Column(): | |
output = gr.Textbox(placeholder="", show_label=True, interactive=False, label='Результат:', lines=7) | |
text_button.click(process_image, inputs=[image_input], outputs=output) | |
demo.queue(default_concurrency_limit=12) | |
demo.launch() |