Spaces:
Running
Running
initial commit
Browse files- app.py +73 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import time
|
| 4 |
+
import json
|
| 5 |
+
import base64
|
| 6 |
+
import os
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
|
| 10 |
+
class SuperCanvas:
|
| 11 |
+
def __init__(self, api_key):
|
| 12 |
+
self.base = "https://api.supercanvas.ai/v1/run/dfd23676-575c-498a-84f4-10d19af1e65b"
|
| 13 |
+
self.headers = {
|
| 14 |
+
"accept": "application/json",
|
| 15 |
+
"content-type": "application/json",
|
| 16 |
+
"authorization": f"Bearer {api_key}"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def generate(self, prompt):
|
| 20 |
+
payload = { "properties": {
|
| 21 |
+
"user_prompt": prompt
|
| 22 |
+
}}
|
| 23 |
+
response = requests.post(self.base, json=payload, headers=self.headers)
|
| 24 |
+
return response.json()
|
| 25 |
+
|
| 26 |
+
def wait(self, job):
|
| 27 |
+
job_result = job
|
| 28 |
+
|
| 29 |
+
while job_result['status'] in ['running']:
|
| 30 |
+
time.sleep(0.25)
|
| 31 |
+
|
| 32 |
+
return job_result
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
supercanvas_client = SuperCanvas(api_key=os.getenv("SUPERCANVAS_API_TOKEN"))
|
| 37 |
+
|
| 38 |
+
def get_result(prompt):
|
| 39 |
+
result = supercanvas_client.generate(prompt)
|
| 40 |
+
|
| 41 |
+
job = supercanvas_client.wait(result["run_id"])
|
| 42 |
+
|
| 43 |
+
return job["urls"][0]
|
| 44 |
+
|
| 45 |
+
css = """
|
| 46 |
+
#generate {
|
| 47 |
+
height: 100%;
|
| 48 |
+
}
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
with gr.Blocks(css=css) as demo:
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column(scale=1):
|
| 56 |
+
gr.Markdown(elem_id="powered-by-supercanvas-ai", value="Powered by [SuperCanvas.AI](https://www.supercanvas.ai/).")
|
| 57 |
+
|
| 58 |
+
with gr.Tab("Prompt"):
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column(scale=6, min_width=600):
|
| 61 |
+
prompt = gr.Textbox("hogwarts school", placeholder="Prompt", show_label=False, lines=3)
|
| 62 |
+
with gr.Column():
|
| 63 |
+
text_button = gr.Button("Generate", variant='primary', elem_id="generate")
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
|
| 67 |
+
with gr.Column(scale=2):
|
| 68 |
+
image_output = gr.Image(value="https://storage.googleapis.com/ai-pipeline-app.appspot.com/JGoawqXDF1x2zKuKXj8ZWg/95bdb06c-03a5-4902-a0a0-0d5a206f4ca1-0.png")
|
| 69 |
+
|
| 70 |
+
text_button.click(get_result, inputs=[prompt], outputs=image_output)
|
| 71 |
+
|
| 72 |
+
demo.queue(concurrency_count=24)
|
| 73 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
gradio
|
| 3 |
+
requests
|