artificialguybr commited on
Commit
807cf27
·
1 Parent(s): 9a1c0d4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import PIL.Image
5
+ from io import BytesIO
6
+ import os
7
+ import random
8
+ import datetime
9
+
10
+ def generate_image(prompt, negative_prompt, scheduler, steps, width, height, cfg, restore_faces, seed):
11
+ request_time = datetime.datetime.now()
12
+ restore_faces = bool(restore_faces)
13
+ print(f"restore_faces: {restore_faces}, type: {type(restore_faces)}")
14
+ # Define the API endpoint
15
+ apiUrl = os.getenv("API_URL")
16
+ # Define the request headers
17
+ headers = {
18
+ "Content-Type": "application/json",
19
+ "token": os.getenv("API_TOKEN")
20
+ }
21
+
22
+ # Define the request body
23
+ body = {
24
+ "mode": "url",
25
+ "model": "Freedom.safetensors",
26
+ "tiling": False,
27
+ "batch_size": 1,
28
+ "prompt": prompt,
29
+ "negative_prompt": negative_prompt,
30
+ "seed":random.randint(0, 999999999),
31
+ "scheduler": scheduler,
32
+ "n_iter": 1,
33
+ "steps": steps,
34
+ "cfg": cfg,
35
+ "offset_noise": 0.0,
36
+ "width": width,
37
+ "height": height,
38
+ "clip_skip": 1,
39
+ "vae": "vae-ft-mse-840000-ema-pruned.ckpt",
40
+ "restore_faces": restore_faces,
41
+ "fr_model": "CodeFormer",
42
+ "codeformer_weight": 0.5,
43
+ "enable_hr": False,
44
+ "denoising_strength": 0.75,
45
+ "hr_scale": 2,
46
+ "hr_upscale": "None",
47
+ "img2img_ref_img_type": "piece",
48
+ "img2img_resize_mode": 0,
49
+ "img2img_denoising_strength": 0.75,
50
+ }
51
+
52
+ # Send the request
53
+ response = requests.post(apiUrl, headers=headers, data=json.dumps(body), verify=False)
54
+ # Print the response body if the status code is not 200
55
+ if response.status_code != 200:
56
+ print(response.text)
57
+
58
+ # Check the response status
59
+ if response.status_code == 200:
60
+
61
+ # Get the image URL from the response
62
+ response_json = response.json()
63
+ if 'results' in response_json and isinstance(response_json['results'], list) and len(response_json['results']) > 0:
64
+ image_url = response_json['results'][0]
65
+
66
+ # Get the image from the URL
67
+ image_response = requests.get(image_url)
68
+ image = PIL.Image.open(BytesIO(image_response.content))
69
+
70
+ # Log the information together
71
+ print(f"Request time: {request_time}\n"
72
+ f"Prompt: {prompt}\n"
73
+ f"Negative Prompt: {negative_prompt}\n"
74
+ f"Seed: {seed}\n"
75
+ f"Res(width x height): {width} x {height}\n"
76
+ f"Image URL: {image_url}")
77
+
78
+ return image
79
+ else:
80
+ raise Exception("Unexpected API response format")
81
+ else:
82
+ raise Exception("API request failed with status code " + str(response.status_code))
83
+
84
+ # Define the Gradio interface
85
+ iface = gr.Interface(
86
+ fn=generate_image,
87
+ inputs=[
88
+ gr.components.Textbox(label="Prompt"),
89
+ gr.components.Textbox(value="ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, signature, cut off, draft", label="Negative Prompt"),
90
+ gr.components.Dropdown(choices=[
91
+ "Euler a",
92
+ "Euler",
93
+ "LMS",
94
+ "Heun",
95
+ "DPM2",
96
+ "DPM2 a",
97
+ "DPM++ 2S a",
98
+ "DPM++ 2M",
99
+ "DPM++ SDE",
100
+ "DPM fast",
101
+ "DPM adaptive",
102
+ "LMS Karras",
103
+ "DPM2 Karras",
104
+ "DPM2 a Karras",
105
+ "DPM++ 2S a Karras",
106
+ "DPM++ 2M Karras",
107
+ "DPM++ SDE Karras",
108
+ "DDIM",
109
+ "PLMS"
110
+ ], label="Scheduler", value="DPM++ SDE Karras"),
111
+ gr.components.Slider(minimum=10, maximum=100, step=1.0,value=30, label="Steps"),
112
+ gr.components.Slider(minimum=512, maximum=1600, value=512, label="Width"),
113
+ gr.components.Slider(minimum=512, maximum=1600, value=512, label="Height"),
114
+ gr.components.Slider(minimum=4, maximum=12, step=0.5, value=7.0, label="CFG"),
115
+ gr.inputs.Checkbox(label="Restore Faces", default=False),
116
+ ],
117
+ outputs=gr.components.Image(),
118
+ title="Liberte.Redmond Demonstration",
119
+ description = """
120
+ ## Finetuned model of SD 1.5 produced by [@artificialguybr](https://twitter.com/artificialguybr).
121
+ ## Resources
122
+ - The weights were released [here](https://civitai.com/models/87288/freedomredmond) with example prompts in CIVITAI and [here in HF](https://huggingface.co/artificialguybr/liberte).
123
+ ## Demonstration
124
+ This demonstration is running on the [makeai.run API](https://www.makeai.run/).
125
+ ## Acknowledgements
126
+ Thanks to [Redmond.ai](https://redmond.ai/) for providing GPU Time and sponsoring this model.
127
+ """,
128
+ allow_flagging='never'
129
+ )
130
+
131
+ #Adding queue
132
+ iface.queue(concurrency_count=12)
133
+
134
+ # Launch the app
135
+ iface.launch()