Spaces:
Running
Running
import gradio as gr | |
import requests | |
import time | |
import json | |
import base64 | |
import os | |
from PIL import Image | |
from io import BytesIO | |
class SuperCanvas: | |
def __init__(self, api_key): | |
self.base = "https://api.supercanvas.ai/v1/run/dfd23676-575c-498a-84f4-10d19af1e65b" | |
self.headers = { | |
"accept": "application/json", | |
"content-type": "application/json", | |
"authorization": f"Bearer {api_key}" | |
} | |
def generate(self, prompt): | |
payload = { "properties": { | |
"user_prompt": prompt | |
}} | |
response = requests.post(self.base, json=payload, headers=self.headers) | |
return response.json() | |
def wait(self, job): | |
job_result = job | |
while job_result['status'] in ['running']: | |
time.sleep(0.25) | |
return job_result | |
supercanvas_client = SuperCanvas(api_key=os.getenv("SUPERCANVAS_API_TOKEN")) | |
def get_result(prompt): | |
result = supercanvas_client.generate(prompt) | |
job = supercanvas_client.wait(result["run_id"]) | |
return job["urls"][0] | |
css = """ | |
#generate { | |
height: 100%; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
with gr.Row(): | |
with gr.Column(scale=1): | |
gr.Markdown(elem_id="powered-by-supercanvas-ai", value="Powered by [SuperCanvas.AI](https://www.supercanvas.ai/).") | |
with gr.Tab("Prompt"): | |
with gr.Row(): | |
with gr.Column(scale=6, min_width=600): | |
prompt = gr.Textbox("hogwarts school", placeholder="Prompt", show_label=False, lines=3) | |
with gr.Column(): | |
text_button = gr.Button("Generate", variant='primary', elem_id="generate") | |
with gr.Row(): | |
with gr.Column(scale=2): | |
image_output = gr.Image(value="https://storage.googleapis.com/ai-pipeline-app.appspot.com/JGoawqXDF1x2zKuKXj8ZWg/95bdb06c-03a5-4902-a0a0-0d5a206f4ca1-0.png") | |
text_button.click(get_result, inputs=[prompt], outputs=image_output) | |
demo.queue(concurrency_count=24) | |
demo.launch() |