Spaces:
Runtime error
Runtime error
import asyncio | |
import io | |
import json | |
import os | |
import aiohttp | |
from PIL import Image | |
from dotenv import load_dotenv | |
load_dotenv() | |
import PIL | |
API_URL = os.getenv("API_URL") | |
API_KEY = os.getenv("API_KEY") | |
print(os.getenv("API_URL")) | |
print(os.getenv("API_KEY")) | |
import gradio as gr | |
showDetailsBool = False | |
showSeedBool = False | |
async def CleanBlock(): | |
return gr.update(value='') | |
async def ChangeBlock(): | |
global showDetailsBool | |
if not showDetailsBool: | |
showDetailsBool = not showDetailsBool | |
return gr.Button.update(value="Hide Details"), gr.update(visible=True) | |
else: | |
showDetailsBool = not showDetailsBool | |
return gr.Button.update(value="Show Details"), gr.update(visible=False) | |
async def ChangeSeedBlock(): | |
global showSeedBool | |
if not showSeedBool: | |
showSeedBool = not showSeedBool | |
return gr.update(visible=True) | |
else: | |
showSeedBool = not showSeedBool | |
return gr.update(value='-1', visible=False) | |
async def load_image_from_url(url): | |
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session: | |
async with session.get(url) as response: | |
image_data = await response.read() | |
return Image.open(io.BytesIO(image_data)) | |
async def greet(my_prompt, control_weight, correct_level, padding_ratio, point_style, my_seed, size): | |
url = API_URL | |
headers = { | |
'x-qrbtf-key': f'{API_KEY}', | |
} | |
full_response: str = "" | |
point_style_out = 0 | |
if correct_level == "L(7%)": | |
correct_level_out = 1 | |
elif correct_level == "M(15%)": | |
correct_level_out = 0 | |
else: | |
if correct_level == "Q(25%)": | |
correct_level_out = 3 | |
else: | |
correct_level_out = 2 | |
if size == "768*768": | |
width_out, height_out = 768, 768 | |
else: | |
width_out, height_out = 1024, 1024 | |
if point_style == "Normal": | |
point_style_out = 0 | |
elif point_style == "circle": | |
point_style_out = 1 | |
else: | |
if point_style == "minimum": | |
point_style_out = 2 | |
payload = { | |
'url': 'https://qrbtf.com/', | |
'prompt': my_prompt, | |
'controlnet_weight': control_weight, | |
'correct_level': correct_level_out, | |
'padding_ratio': padding_ratio, | |
'point_style': point_style_out, | |
'seed': my_seed, | |
'width': width_out, | |
'height': height_out, | |
} | |
async with aiohttp.ClientSession(headers=headers) as session: | |
async with session.post(url, json=payload, ssl=False) as response: | |
await asyncio.sleep(0) | |
async for (chunk, _) in response.content.iter_chunks(): | |
chunk = json.loads(chunk.decode("utf-8")) | |
if chunk["type"] == "result": | |
data = chunk["data"] | |
url = data["download_url"] | |
print(url) | |
return await load_image_from_url(url) | |
return full_response | |
with gr.Blocks() as demo: | |
gr.Markdown(""" | |
# QRBTF.AI API Demo | |
- [Join Discord](https://discord.gg/V9CNuqYfte) | |
- [Official website](https://qrbtf.com/) | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
url = gr.Textbox( | |
label="URL", | |
placeholder="https://", | |
value="https://qrbtf.com/", | |
interactive=False | |
) | |
prompt = gr.Textbox( | |
label="Prompt", | |
placeholder="Enter a prompt here", | |
value="1girl, flowers, birds", | |
interactive=True | |
) | |
showDetails = gr.Button("Show Details") | |
with gr.Column(visible=False) as detailColumn: | |
with gr.Row(): | |
randomSeed = gr.Checkbox(label="Random Seed?", value=True, interactive=True) | |
promptsTuning = gr.Checkbox(label="Prompts Tuning?", value=True, interactive=True) | |
seed = gr.Textbox( | |
label="Seed", | |
placeholder="Seed", | |
value="-1", | |
interactive=True, | |
visible=False | |
) | |
ControlWeight = gr.Slider(0, 2, value=1.5, label="ControlWeight", | |
info="Controls the weight of QR_Controlnet", interactive=True) # 滑动条 | |
marginScale = gr.Slider(0, 0.5, value=0.2, label="Margin Scale", | |
info="Controls how far margin is", interactive=True) # 滑动条 | |
SizeSelection = gr.Dropdown( | |
["768*768", "1024*1024"], value="768*768", label="Size", interactive=True) | |
errorRate = gr.Dropdown( | |
["L(7%)", "M(15%)", "Q(25%)", "H(30%)"], value="H(30%)", label="Error Correction Rate", | |
interactive=True) | |
anchorStyle = gr.Dropdown( | |
["Normal", "circle", "minimum"], value="Normal", label="AnchorStyle", interactive=True) | |
showDetails.click(ChangeBlock, [], [showDetails, detailColumn]) | |
with gr.Column(): | |
with gr.Row(): | |
clear_btn = gr.Button( | |
"Clear", | |
) | |
btn = gr.Button( | |
"Call API", | |
variant="primary" | |
) | |
out = gr.Image(shape=(1, 1)) | |
btn.click(greet, [prompt, ControlWeight, errorRate, marginScale, anchorStyle, seed, SizeSelection], out) | |
clear_btn.click(CleanBlock, [], prompt) | |
randomSeed.change(ChangeSeedBlock, [], seed) | |
demo.launch() | |