Spaces:
Runtime error
Runtime error
Commit
·
864f159
1
Parent(s):
c7f3c86
gradio app v4.1
Browse files- app.py +82 -4
- flagged/log.csv +3 -0
app.py
CHANGED
|
@@ -1,7 +1,85 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def greet(name):
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import aiohttp
|
| 3 |
+
import asyncio
|
| 4 |
+
import random
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import os
|
| 8 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
markdown_link = "For more generations, visit [Imagine Art](https://www.imagine.art/)."
|
| 12 |
+
|
| 13 |
+
TOKEN = os.getenv("TOKEN")
|
| 14 |
+
URL = os.getenv("URL")
|
| 15 |
+
print(f"URL: {URL}, Type: {type(URL)}")
|
| 16 |
+
|
| 17 |
+
if not URL:
|
| 18 |
+
raise ValueError("URL environment variable is not set.")
|
| 19 |
+
|
| 20 |
+
async def async_generation_request(request_data, is_user_premium):
|
| 21 |
+
seed = request_data.get("seed", random.randint(1, 10000000))
|
| 22 |
+
|
| 23 |
+
headers = {
|
| 24 |
+
"bearer": TOKEN,
|
| 25 |
+
"Style-Id": request_data["model_id"]
|
| 26 |
+
}
|
| 27 |
+
if is_user_premium:
|
| 28 |
+
headers["Premium"] = "True"
|
| 29 |
+
|
| 30 |
+
for key, value in headers.items():
|
| 31 |
+
if value is None:
|
| 32 |
+
raise ValueError(f"Header value is missing for key: {key}")
|
| 33 |
+
|
| 34 |
+
async with aiohttp.ClientSession() as session:
|
| 35 |
+
for i in range(3):
|
| 36 |
+
data = aiohttp.FormData()
|
| 37 |
+
data.add_field('prompt', request_data["prompt"], content_type='multipart/form-data')
|
| 38 |
+
data.add_field('style_id', request_data["model_id"])
|
| 39 |
+
data.add_field('seed', str(seed))
|
| 40 |
+
data.add_field('aspect_ratio', request_data["aspect_ratio"])
|
| 41 |
+
data.add_field('negative_prompt', request_data["negative_prompt"])
|
| 42 |
+
data.add_field('cfg', str(request_data["cfg_scale"]))
|
| 43 |
+
|
| 44 |
+
async with session.post(URL, data=data, headers=headers) as response:
|
| 45 |
+
if response.status == 200:
|
| 46 |
+
return await response.read()
|
| 47 |
+
print(f"Failed API Call: {response.status} >>> {await response.text()}")
|
| 48 |
+
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
def run_async_in_thread(func, *args):
|
| 52 |
+
with ThreadPoolExecutor() as executor:
|
| 53 |
+
return executor.submit(asyncio.run, func(*args)).result()
|
| 54 |
+
|
| 55 |
+
def gradio_generation_request(prompt, aspect_ratio, negative_prompt, cfg_scale):
|
| 56 |
+
request_data = {
|
| 57 |
+
"prompt": prompt,
|
| 58 |
+
"model_id": "32",
|
| 59 |
+
"aspect_ratio": aspect_ratio,
|
| 60 |
+
"negative_prompt": negative_prompt,
|
| 61 |
+
"cfg_scale": cfg_scale,
|
| 62 |
+
"seed": random.randint(1, 10000000)
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
output = run_async_in_thread(async_generation_request, request_data, True)
|
| 66 |
+
|
| 67 |
+
if output:
|
| 68 |
+
return Image.open(io.BytesIO(output))
|
| 69 |
+
else:
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
interface = gr.Interface(
|
| 73 |
+
fn=gradio_generation_request,
|
| 74 |
+
inputs=[
|
| 75 |
+
gr.Textbox(label="Prompt"),
|
| 76 |
+
gr.Textbox(label="Aspect Ratio"),
|
| 77 |
+
gr.Textbox(label="Negative Prompt"),
|
| 78 |
+
gr.Slider(minimum=1, maximum=15, value=7.5, label="CFG Scale")
|
| 79 |
+
],
|
| 80 |
+
outputs="image",
|
| 81 |
+
title='IMAGINE V4.1 GENERATION',
|
| 82 |
+
description=markdown_link
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
interface.launch()
|
flagged/log.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Prompt,Model ID,Aspect Ratio,Negative Prompt,CFG Scale,Is User Premium,output,flag,username,timestamp
|
| 2 |
+
girl eating banana,35,,,7.5,True,/home/evobits/rashid/imagineV4.1/flagged/output/723e768be4fee0202a2ee36bde863103fb3fbcda/tmpmxb31ukx.png,,,2024-01-09 19:26:31.119421
|
| 3 |
+
,,,7.5,,,,2024-01-10 11:25:22.338663
|