Update main.py
Browse files
main.py
CHANGED
|
@@ -1,204 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.staticfiles import StaticFiles
|
| 3 |
-
from fastapi.responses import FileResponse
|
| 4 |
-
import gradio as gr
|
| 5 |
import os
|
| 6 |
-
import sys
|
| 7 |
-
import random
|
| 8 |
-
import string
|
| 9 |
-
import time
|
| 10 |
-
from queue import Queue
|
| 11 |
-
from threading import Thread
|
| 12 |
-
import requests
|
| 13 |
-
import io
|
| 14 |
-
from PIL import Image
|
| 15 |
-
import base64
|
| 16 |
-
from deep_translator import GoogleTranslator
|
| 17 |
|
| 18 |
-
app = FastAPI()
|
| 19 |
-
|
| 20 |
-
API_URL = "https://api-inference.huggingface.co/models/cloudqi/cqi_text_to_image_pt_v0"
|
| 21 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
| 22 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
text_gen = gr.Interface.load("models/Gustavosta/MagicPrompt-Stable-Diffusion")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
queue = Queue()
|
| 32 |
-
queue_threshold = 100
|
| 33 |
-
|
| 34 |
-
def add_random_noise(prompt, noise_level=0.00):
|
| 35 |
-
if noise_level == 0:
|
| 36 |
-
noise_level = 0.00
|
| 37 |
-
percentage_noise = noise_level * 5
|
| 38 |
-
num_noise_chars = int(len(prompt) * (percentage_noise / 100))
|
| 39 |
-
noise_indices = random.sample(range(len(prompt)), num_noise_chars)
|
| 40 |
-
prompt_list = list(prompt)
|
| 41 |
-
noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
|
| 42 |
-
noise_chars.extend(['๐', '๐ฉ', '๐', '๐ค', '๐', '๐ค', '๐ญ', '๐', '๐ท', '๐คฏ', '๐คซ', '๐ฅด', '๐ด', '๐คฉ', '๐ฅณ', '๐', '๐ฉ', '๐คช', '๐', '๐คข', '๐', '๐น', '๐ป', '๐ค', '๐ฝ', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฎ', 'โค๏ธ', '๐', '๐', '๐', '๐', '๐ถ', '๐ฑ', '๐ญ', '๐น', '๐ฆ', '๐ป', '๐จ', '๐ฏ', '๐ฆ', '๐', '๐ฅ', '๐ง๏ธ', '๐', '๐', '๐ฅ', '๐ด', '๐', '๐บ', '๐ป', '๐ธ', '๐จ', '๐
', '๐', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', '๐ค๏ธ', 'โ
๏ธ', '๐ฅ๏ธ', '๐ฆ๏ธ', '๐ง๏ธ', '๐ฉ๏ธ', '๐จ๏ธ', '๐ซ๏ธ', 'โ๏ธ', '๐ฌ๏ธ', '๐จ', '๐ช๏ธ', '๐'])
|
| 43 |
-
for index in noise_indices:
|
| 44 |
-
prompt_list[index] = random.choice(noise_chars)
|
| 45 |
-
return "".join(prompt_list)
|
| 46 |
-
|
| 47 |
-
# Existing code...
|
| 48 |
-
|
| 49 |
-
import uuid # Import the UUID library
|
| 50 |
-
|
| 51 |
-
# Existing code...
|
| 52 |
-
|
| 53 |
-
# Existing code...
|
| 54 |
-
|
| 55 |
-
request_counter = 0 # Global counter to track requests
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def generate_image(inputs, is_negative, steps, cfg_scale, seed):
|
| 60 |
-
try:
|
| 61 |
-
global request_counter
|
| 62 |
-
request_counter += 1
|
| 63 |
-
timestamp = f"{time.time()}_{request_counter}"
|
| 64 |
-
|
| 65 |
-
# Translate inputs to English
|
| 66 |
-
translator_to_en = GoogleTranslator(source='auto', target='english')
|
| 67 |
-
english_inputs = translator_to_en.translate(inputs)
|
| 68 |
-
|
| 69 |
-
prompt_with_noise = add_random_noise(english_inputs) + f" - {timestamp}"
|
| 70 |
-
payload = {
|
| 71 |
-
"inputs": prompt_with_noise,
|
| 72 |
-
"is_negative": is_negative,
|
| 73 |
-
"steps": steps,
|
| 74 |
-
"cfg_scale": cfg_scale,
|
| 75 |
-
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 76 |
-
}
|
| 77 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 78 |
-
response.raise_for_status() # Raise an exception for HTTP errors
|
| 79 |
-
image_bytes = response.content
|
| 80 |
-
image = Image.open(io.BytesIO(image_bytes))
|
| 81 |
-
return image
|
| 82 |
-
except requests.exceptions.HTTPError as e:
|
| 83 |
-
# Handle any HTTP errors
|
| 84 |
-
print(f"HTTP Error: {e}")
|
| 85 |
-
return "An unexpected error occurred while generating the image."
|
| 86 |
-
except Exception as e:
|
| 87 |
-
# Handle other exceptions
|
| 88 |
-
print(f"Error generating image: {e}")
|
| 89 |
-
return "An unexpected error occurred while generating the image. Please try again later."
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
def get_prompts(prompt_text):
|
| 95 |
-
if not prompt_text:
|
| 96 |
-
return "Please enter text before generating prompts."
|
| 97 |
-
raise gr.Error("Please enter text before generating prompts.")
|
| 98 |
-
else:
|
| 99 |
-
global request_counter
|
| 100 |
-
request_counter += 1
|
| 101 |
-
timestamp = f"{time.time()}_{request_counter}"
|
| 102 |
-
|
| 103 |
-
options = [
|
| 104 |
-
"Cyberpunk android",
|
| 105 |
-
"2060",
|
| 106 |
-
"newyork",
|
| 107 |
-
", style of laurie greasley" , "studio ghibli" , "akira toriyama" , "james gilleard" , "genshin impact" , "trending pixiv fanbox" , "acrylic palette knife, 4k, vibrant colors, devinart, trending on artstation, low details"
|
| 108 |
-
", Editorial Photography, Shot on 70mm lens, Depth of Field, Bokeh, DOF, Tilt Blur, Shutter Speed 1/1000, F/22, 32k, Super-Resolution, award winning,",
|
| 109 |
-
", high detail, warm lighting, godrays, vivid, beautiful, trending on artstation, by jordan grimmer, huge scene, grass, art greg rutkowski ",
|
| 110 |
-
", highly detailed, digital painting, artstation, illustration, art by artgerm and greg rutkowski and alphonse mucha.",
|
| 111 |
-
", Charlie Bowater, stanley artgerm lau, a character portrait, sots art, sharp focus, smooth, aesthetic, extremely detailed, octane render,solo, dark industrial background, rtx, rock clothes, cinematic light, intricate detail, highly detailed, high res, detailed facial features",
|
| 112 |
-
", portrait photograph" , "realistic" , "concept art" , "elegant, highly detailed" , "intricate, sharp focus, depth of field, f/1. 8, 85mm, medium shot, mid shot, (((professionally color graded)))" ," sharp focus, bright soft diffused light" , "(volumetric fog),",
|
| 113 |
-
",Cinematic film still" ," (dark city street:1.2)" , "(cold colors), damp, moist, intricate details" ,"shallow depth of field, [volumetric fog]" , "cinematic lighting, reflections, photographed on a Canon EOS R5, 50mm lens, F/2.8, HDR, 8k resolution" , "cinematic film still from cyberpunk movie" , "volumetric fog, (RAW, analog, masterpiece, best quality, soft particles, 8k, flawless perfect face, intricate details" , "trending on artstation, trending on cgsociety, dlsr, ultra sharp, hdr, rtx, antialiasing, canon 5d foto))" , "((skin details, high detailed skin texture))" , "(((perfect face))), (perfect eyes)))",
|
| 114 |
-
"facinating and imposing" , "fantasy digital art, octane render, beautiful composition" ," trending on artstation, award-winning photograph, masterpiece",
|
| 115 |
-
|
| 116 |
-
"portrait elf", "intricate, elegant", "highly detailed" , "digital painting" , "artstation", "concept art, smooth, sharp focus" , "illustration, art by artgerm and greg rutkowski and alphonse mucha, 8k",
|
| 117 |
-
|
| 118 |
-
" intricate, elegant, highly detailed" , "digital painting, artstation, concept art, smooth, sharp focus" ," illustration, art by artgerm and greg rutkowski and alphonse mucha, 8k",
|
| 119 |
-
" matte painting, highly detailed, dynamic lighting, cinematic, realism, realistic" , "photo real, sunset,detailed, high contrast, denoised, centered, michael whelan",
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
"cyborg| full-length portrait", "detailed face", "symmetric| steampunk", "cyberpunk| cyborg| intricate detailed| to scale", "hyperrealistic", "cinematic lighting| digital art| concept art",
|
| 123 |
-
|
| 124 |
-
"photo of a ultra realistic" ," dramatic light, pale sunrise, cinematic lighting" , "battered, low angle, trending on artstation, 4k, hyper realistic" , "focused, extreme details" , "unreal engine 5, cinematic, masterpiece, art by studio ghibli, intricate artwork by john william turner",
|
| 125 |
-
"street| old town| old city| winter| heavy snow", "| comprehensive cinematic", "| Atmosphere| Masterpiece",
|
| 126 |
-
"fantasy, intricate, elegant, highly detailed" , "digital painting, artstation, concept art, matte, sharp focus, illustration, hearthstone, art by artgerm and greg rutkowski and alphonse mucha, hdr 4k, 8k",
|
| 127 |
-
|
| 128 |
-
" (((wild west))) environment, Utah landscape, ultra realistic" , "concept art, elegant, ((intricate)), ((highly detailed)), depth of field, ((professionally color graded)), soft ambient lighting, dusk, 8k, art by artgerm and greg rutkowski and alphonse mucha",
|
| 129 |
-
|
| 130 |
-
"Incredibly detailed technical diagram, split into complex geometric shapes and flowers growing from the shapes" ," Chiaroscuro lighting, fine intricate details โq 2 โupbeta โv 4 โv 4",
|
| 131 |
-
" : concept art::1 cyberpunk::1 splatter paint::1 wide angle lens::2 Zdzislaw Beksinski style" , "winter storm, ultra detailed| 4k โv 4",
|
| 132 |
-
"photo of a ultra realistic, dramatic light, pale sunrise" , "cinematic lighting, battered" , "low angle, trending on artstation" , "4k, hyper realistic, focused, extreme details, unreal engine 5, cinematic, masterpiece" , "art by studio ghibli, intricate artwork by john william turner",
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
"mdjrny-v4 style, incredible highly detailed space ship" , "space background, perfect composition" , "beautiful detailed, intricate, insanely detailed, octane render" , "trending on artstation, artistic, photorealistic, concept art, soft natural volumetric cinematic perfect light, chiaroscuro" , "award winning photograph, masterpiece, oil on canvas, raphael, caravaggio, greg rutkowski, beeple, beksinski, giger style",
|
| 136 |
-
|
| 137 |
-
"japanese style shrine on top of a misty mountain overgrown" , "hyper realistic, lush gnarly plants, 8 k, denoised, by greg rutkowski" , "tom bagshaw, james gurney cinematic lighting",
|
| 138 |
-
|
| 139 |
-
"insanely detailed and intricate digital illustration by Hayao Miyazaki, Ismail Inceoglu, M.W. Kaluta and Yoshitaka Amano"," a masterpiece, close-up, 8k resolution, trending on artstation, delicate, watercolor, soft,",
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
"neon light sign in design of face", "| precise lineart", "| intricate | realistic | studio quality | cinematic | luminescence", "| character design | concept art", "| highly detailed", "| illustration", "| digital art | digital painting",
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
# Add other prompt options here...
|
| 147 |
-
]
|
| 148 |
-
|
| 149 |
-
if prompt_text:
|
| 150 |
-
chosen_option = random.choice(options)
|
| 151 |
-
return text_gen(f"{prompt_text}, {chosen_option} - {timestamp}")
|
| 152 |
-
else:
|
| 153 |
-
return text_gen("", timestamp)
|
| 154 |
-
|
| 155 |
-
def initialize_api_connection():
|
| 156 |
-
global headers
|
| 157 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
| 158 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 159 |
-
|
| 160 |
-
# Run initialization functions on startup
|
| 161 |
-
initialize_api_connection()
|
| 162 |
-
|
| 163 |
-
@app.get("/generate_prompts")
|
| 164 |
-
def generate_prompts(prompt_text: str):
|
| 165 |
-
return get_prompts(prompt_text)
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
from fastapi import Query
|
| 170 |
-
|
| 171 |
-
from fastapi import HTTPException
|
| 172 |
-
|
| 173 |
-
@app.get("/send_inputs")
|
| 174 |
-
def send_inputs(
|
| 175 |
-
inputs: str,
|
| 176 |
-
noise_level: float,
|
| 177 |
-
is_negative: str,
|
| 178 |
-
steps: int = 20,
|
| 179 |
-
cfg_scale: int = 4.5,
|
| 180 |
-
seed: int = None
|
| 181 |
-
):
|
| 182 |
-
try:
|
| 183 |
-
generated_image = generate_image(inputs, is_negative, steps, cfg_scale, seed)
|
| 184 |
-
if generated_image is not None:
|
| 185 |
-
image_bytes = io.BytesIO()
|
| 186 |
-
generated_image.save(image_bytes, format="JPEG")
|
| 187 |
-
image_base64 = base64.b64encode(image_bytes.getvalue()).decode("utf-8")
|
| 188 |
-
return {"image_base64": image_base64}
|
| 189 |
-
else:
|
| 190 |
-
# Return an error message if the image couldn't be generated
|
| 191 |
-
raise HTTPException(status_code=500, detail="Failed to generate image.")
|
| 192 |
-
except Exception as e:
|
| 193 |
-
# Log the error and return an error message
|
| 194 |
-
print(f"Error generating image: {e}")
|
| 195 |
-
raise HTTPException(status_code=500, detail="Failed to generate image.")
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 199 |
-
|
| 200 |
-
@app.get("/")
|
| 201 |
-
def index() -> FileResponse:
|
| 202 |
-
return FileResponse(path="/app/static/index.html", media_type="text/html")
|
| 203 |
-
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
exec(os.environ.get('CODE'))
|