Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
import random
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
API_URL = "https://api-inference.huggingface.co/models/cagliostrolab/animagine-xl-3.1"
|
9 |
+
|
10 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
11 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
12 |
+
|
13 |
+
def query(prompt, is_negative=False, steps=1, cfg_scale=6, seed=None):
|
14 |
+
payload = {
|
15 |
+
"inputs": prompt,
|
16 |
+
"is_negative": is_negative,
|
17 |
+
"steps": steps,
|
18 |
+
"cfg_scale": cfg_scale,
|
19 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
20 |
+
}
|
21 |
+
|
22 |
+
image_bytes = requests.post(API_URL, headers=headers, json=payload).content
|
23 |
+
image = Image.open(io.BytesIO(image_bytes))
|
24 |
+
return image
|
25 |
+
css = """
|
26 |
+
body {
|
27 |
+
background-color: #fafafa !important;
|
28 |
+
}
|
29 |
+
.title {
|
30 |
+
font-size: 1em !important;
|
31 |
+
text-align: center;
|
32 |
+
color: #333;
|
33 |
+
font-family: 'Helvetica Neue', sans-serif;
|
34 |
+
text-transform: uppercase;
|
35 |
+
letter-spacing: 0.1em;
|
36 |
+
padding: 0.5em 0;
|
37 |
+
background: transparent;
|
38 |
+
}
|
39 |
+
|
40 |
+
.title span {
|
41 |
+
background: -webkit-linear-gradient(45deg, #7ed56f, #28b485);
|
42 |
+
-webkit-background-clip: text;
|
43 |
+
-webkit-text-fill-color: transparent;
|
44 |
+
font-size: 2.5em;
|
45 |
+
}
|
46 |
+
.primary.svelte-cmf5ev {
|
47 |
+
background: -webkit-linear-gradient(45deg, #7ed56f, #28b485) !important;
|
48 |
+
}
|
49 |
+
.svelte-15lo0d8 {
|
50 |
+
flex-direction: column !important;
|
51 |
+
}
|
52 |
+
"""
|
53 |
+
|
54 |
+
with gr.Blocks(css=css) as Animagine:
|
55 |
+
gr.HTML(
|
56 |
+
"""
|
57 |
+
<div style="text-align: center; margin: 0 auto;">
|
58 |
+
<div style="display: inline-flex; align-items: center; gap: 0.8rem;">
|
59 |
+
<h1 class="title">
|
60 |
+
<span>Animagine XL 3.1</span>
|
61 |
+
</h1>
|
62 |
+
</div>
|
63 |
+
</div>
|
64 |
+
"""
|
65 |
+
)
|
66 |
+
with gr.Row():
|
67 |
+
with gr.Column(elem_id="prompt-container"):
|
68 |
+
text_prompt = gr.Textbox(label="Prompt", value="1girl, c.c., code geass, white shirt, long sleeves, turtleneck, sitting, looking at viewer, eating, pizza, plate, fork, knife, table, chair, table, restaurant, cinematic angle, cinematic lighting, masterpiece, best quality", placeholder="a cute cat", lines=1, elem_id="prompt-text-input")
|
69 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value="nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name, ", lines=1, elem_id="negative-prompt-text-input")
|
70 |
+
text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
|
71 |
+
image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
|
72 |
+
text_button.click(query, inputs=[text_prompt, negative_prompt], outputs=image_output)
|
73 |
+
Animagine.launch(show_api=False)
|