MJ / app.py
lalashechka's picture
Update app.py
c376c28
import gradio as gr
import requests
import time
import json
from deep_translator import GoogleTranslator
from langdetect import detect
import os
from PIL import Image
from io import BytesIO
def flip_text(prompt, key):
c = 0
url_id1 = os.getenv("url_id1")
url_id2 = os.getenv("url_id2")
url_id3 = os.getenv("url_id3")
key_os = os.getenv("key_os")
if str(key).lower() == str(key_os).lower():
r_m = requests.post("https://myneuralnetworks.ru/get_key_a/", data={"key": url_id1})
key_a = r_m.json()['result']
try:
language = detect(prompt)
if language == 'ru':
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
print(prompt)
except:
pass
headers = {'authorization': key_a}
json_data = {'prompt': prompt, 'model': 'V5', 'size': '1:1'}
response = requests.post(f"{url_id2}", headers=headers, json=json_data)
data_id = response.json()['data']['recordUuid']
while True:
time.sleep(2)
r_check = requests.get(f"{url_id3}{data_id}")
status = r_check.json()['data']['picState']
if status == 'waitingStart':
continue
elif status == 'queuing':
continue
elif status == "generating":
continue
elif status == "fail":
return ["https://myneuralnetworks.ru/static/img/zp3.jpg"]
elif status == "success":
photo = r_check.json()['data']['picUrl']
ph_url = eval(photo)[0]["picUrl"]
response2 = requests.get(ph_url)
img = Image.open(BytesIO(response2.content))
new_size = (1024, 1024)
top_left = img.crop((0, 0, new_size[0], new_size[1]))
top_right = img.crop((new_size[0], 0, new_size[0]*2, new_size[1]))
bottom_left = img.crop((0, new_size[1], new_size[0], new_size[1]*2))
a = [top_left, top_right, bottom_left]
return a
else:
break
else:
return ["https://myneuralnetworks.ru/static/img/zp2.jpg"]
css = """
#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;
}
footer {visibility: hidden !important;}
#image_output {
min-height: 900px !important;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Row():
prompt = gr.Textbox(placeholder="Введите описание изображения...", show_label=True, label='Описание изображения:', lines=3)
with gr.Row():
tg = gr.HTML("Ключ доступа можно найти в моём <a href='https://t.me/myneuralnetworks' target='_blank'>Telegram-канале</a>")
with gr.Row():
key = gr.Textbox(placeholder="Введите ключ доступа...", show_label=True, label='Ключ доступа:', lines=1)
with gr.Row():
text_button = gr.Button("Сгенерировать изображение", variant='primary', elem_id="generate")
with gr.Row():
image_output = gr.Gallery(show_label=True, label='Результат:', elem_id='image_output', columns=[1], rows=[3], object_fit="contain")
text_button.click(flip_text, inputs=[prompt, key], outputs=image_output)
demo.queue(concurrency_count=12)
demo.launch()