K00B404 commited on
Commit
b58cf97
·
verified ·
1 Parent(s): 2533f72

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import io
3
+ import random
4
+ import os
5
+ import time
6
+ from PIL import Image
7
+ import json
8
+ from dotenv import load_dotenv,find_dotenv
9
+ load_dotenv(find_dotenv())
10
+
11
+
12
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
13
+ API_TOKEN = os.getenv("HF_TOKEN")
14
+
15
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
+ timeout = 100
17
+
18
+ # Function to query the API and return the generated image
19
+ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
20
+ if prompt == "" or prompt is None:
21
+ return None
22
+
23
+ key = random.randint(0, 999)
24
+
25
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
26
+
27
+ # Add some extra flair to the prompt
28
+ prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
29
+ print(f'\033[1mGeneration {key}:\033[0m {prompt}')
30
+
31
+ # Prepare the payload for the API call, including width and height
32
+ payload = {
33
+ "inputs": prompt,
34
+ "is_negative": is_negative,
35
+ "steps": steps,
36
+ "cfg_scale": cfg_scale,
37
+ "seed": seed if seed != -1 else random.randint(1, 1000000000),
38
+ "strength": strength,
39
+ "parameters": {
40
+ "width": width, # Pass the width to the API
41
+ "height": height # Pass the height to the API
42
+ }
43
+ }
44
+
45
+ # Send the request to the API and handle the response
46
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
47
+ if response.status_code != 200:
48
+ print(f"Error: Failed to get image. Response status: {response.status_code}")
49
+ print(f"Response content: {response.text}")
50
+ if response.status_code == 503:
51
+ raise gr.Error(f"{response.status_code} : The model is being loaded")
52
+ raise gr.Error(f"{response.status_code}")
53
+
54
+ try:
55
+ # Convert the response content into an image
56
+ image_bytes = response.content
57
+ image = Image.open(io.BytesIO(image_bytes))
58
+ print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
59
+ return image
60
+ except Exception as e:
61
+ print(f"Error when trying to open the image: {e}")
62
+ return None
63
+
64
+ if __name__ == "__main__":
65
+ prompt="ä vast alien landscape"
66
+ neg="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos"
67
+ width =1024
68
+ height = 1024
69
+ steps = 20
70
+ cfg = 7
71
+ strength = 0.7
72
+ seed = -1
73
+ method = "DPM++ 2M Karras"# "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"
74
+
75
+ result = query(prompt, is_negative=neg, steps=steps, cfg_scale=cfg, sampler=method, seed=seed, strength=strength, width=width, height=height)
76
+
77
+ print(result)