Spaces:
Runtime error
Runtime error
File size: 1,912 Bytes
bbe7ddd de0a6cb bbe7ddd 5db7bfe e04ef84 bbe7ddd de0a6cb dfde0fa bbe7ddd dfde0fa bbe7ddd de0a6cb 939f8da 8dd67e8 db7959d de0a6cb 16108ce db7959d de0a6cb db7959d de0a6cb e04ef84 8dd67e8 e04ef84 de0a6cb e04ef84 dfde0fa de0a6cb |
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 |
import os
import requests
import gradio as gr
api_key = os.getenv("API_KEY")
def generate_image(prompt, code):
if code != os.getenv("code"):
raise gr.Error("❗ Не верный ключ!")
return None
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"prompt": prompt,
"num_images": 1,
}
response = requests.post("https://api.openai.com/v1/images/generations", headers=headers, json=data)
response_json = response.json()
# Получаем URL первого сгенерированного изображения
image_url = response_json["data"][0]["url"]
return image_url
css = """
footer {visibility: hidden !important;}
"""
js = """
document.addEventListener('DOMContentLoaded', (event) => {
if (document.cookie.split(';').some((item) => item.trim().startsWith('code='))) {
const codeValue = document.cookie.split('; ').find(row => row.startsWith('code=')).split('=')[1];
document.getElementById('code').value = codeValue;
}
});
function saveCode() {
const codeValue = document.getElementById('code').value;
document.cookie = 'code=' + codeValue + ';max-age=31536000'; // Сохраняем на 1 год
}
"""
# Создание интерфейса с помощью Gradio
with gr.Blocks(css=css, theme='YTheme/Sketch') as demo:
gr.HTML(f"<script>{js}</script>")
with gr.Row():
code = gr.Textbox(label="Ключ доступа", type="password", elem_id="code", change=saveCode)
with gr.Row():
prompt_input = gr.Textbox(label="Описание изображения")
submit_btn = gr.Button("Генерация", variant='primary')
image_output = gr.Image(label="Изображение")
submit_btn.click(fn=generate_image, inputs=[prompt_input, code], outputs=image_output)
demo.launch() |