Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- all_models.py +60 -0
- app.py +148 -0
- requirements.txt +7 -0
all_models.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
models = [
|
2 |
+
'burhansyam/davina',
|
3 |
+
'burhansyam/bulsut',
|
4 |
+
'burhansyam/cipa',
|
5 |
+
'burhansyam/difarina',
|
6 |
+
'burhansyam/lalawidy',
|
7 |
+
'burhansyam/nafa',
|
8 |
+
'burhansyam/catheez',
|
9 |
+
'burhansyam/eca',
|
10 |
+
'burhansyam/vior',
|
11 |
+
'burhansyam/gaby',
|
12 |
+
'burhansyam/livy',
|
13 |
+
'burhansyam/rachel',
|
14 |
+
'burhansyam/kame',
|
15 |
+
'burhansyam/inara',
|
16 |
+
'burhansyam/ranty',
|
17 |
+
'burhansyam/novisasmita',
|
18 |
+
'burhansyam/dara',
|
19 |
+
'burhansyam/shinta',
|
20 |
+
'burhansyam/arlida',
|
21 |
+
'burhansyam/pamela',
|
22 |
+
'burhansyam/berliana',
|
23 |
+
'burhansyam/fbyana',
|
24 |
+
'burhansyam/wulan',
|
25 |
+
'burhansyam/lunmay',
|
26 |
+
'burhansyam/gisel',
|
27 |
+
'burhansyam/pevita',
|
28 |
+
'burhansyam/ariel',
|
29 |
+
'burhansyam/mahal',
|
30 |
+
'burhansyam/sherina',
|
31 |
+
'burhansyam/sandrina',
|
32 |
+
'burhansyam/nagita',
|
33 |
+
'burhansyam/wika',
|
34 |
+
'burhansyam/angel',
|
35 |
+
'burhansyam/sarah',
|
36 |
+
'burhansyam/cupi',
|
37 |
+
'burhansyam/gita',
|
38 |
+
'burhansyam/ayuting',
|
39 |
+
'burhansyam/zoe',
|
40 |
+
'burhansyam/taniayu',
|
41 |
+
'burhansyam/3gp',
|
42 |
+
'burhansyam/djbidy',
|
43 |
+
'burhansyam/busalsa',
|
44 |
+
'burhansyam/fuji',
|
45 |
+
'burhansyam/msbrew',
|
46 |
+
'burhansyam/vandal',
|
47 |
+
'burhansyam/marco',
|
48 |
+
'burhansyam/logo',
|
49 |
+
'burhansyam/bowo',
|
50 |
+
'burhansyam/cristiano',
|
51 |
+
'burhansyam/bikini',
|
52 |
+
'burhansyam/nyemot',
|
53 |
+
'burhansyam/minitoy',
|
54 |
+
'burhansyam/micro',
|
55 |
+
'burhansyam/naturelens',
|
56 |
+
'burhansyam/ghibli',
|
57 |
+
'burhansyam/buncit',
|
58 |
+
'burhansyam/doggy',
|
59 |
+
'burhansyam/uncen'
|
60 |
+
]
|
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import base64
|
4 |
+
from random import randint
|
5 |
+
from all_models import models
|
6 |
+
from io import BytesIO
|
7 |
+
from PIL import Image
|
8 |
+
from fastapi import FastAPI, Request
|
9 |
+
from deep_translator import GoogleTranslator
|
10 |
+
|
11 |
+
css_code = os.getenv("DazDinGo_CSS")
|
12 |
+
|
13 |
+
# Initialize translator
|
14 |
+
translator = GoogleTranslator(source='auto', target='en')
|
15 |
+
|
16 |
+
# Load models
|
17 |
+
models_load = {}
|
18 |
+
for model in models:
|
19 |
+
try:
|
20 |
+
models_load[model] = gr.load(f'models/{model}')
|
21 |
+
except Exception as error:
|
22 |
+
models_load[model] = gr.Interface(lambda txt: None, ['text'], ['image'])
|
23 |
+
|
24 |
+
app = FastAPI()
|
25 |
+
|
26 |
+
def gen_image(model_str, prompt):
|
27 |
+
if model_str == 'NA':
|
28 |
+
return None
|
29 |
+
# Translate prompt to English {noise} {klir}
|
30 |
+
translated_prompt = translator.translate(prompt)
|
31 |
+
noise = str(randint(0, 4294967296))
|
32 |
+
klir = '| ultra detail, ultra elaboration, ultra quality, perfect'
|
33 |
+
return models_load[model_str](f'{translated_prompt} {noise} {klir}')
|
34 |
+
|
35 |
+
def image_to_base64(image):
|
36 |
+
buffered = BytesIO()
|
37 |
+
if isinstance(image, str): # if it's a file path
|
38 |
+
img = Image.open(image)
|
39 |
+
img.save(buffered, format="JPEG")
|
40 |
+
else: # if it's a PIL Image
|
41 |
+
image.save(buffered, format="JPEG")
|
42 |
+
return base64.b64encode(buffered.getvalue()).decode()
|
43 |
+
|
44 |
+
# API endpoint
|
45 |
+
@app.post("/generate")
|
46 |
+
async def api_generate(request: Request):
|
47 |
+
data = await request.json()
|
48 |
+
model = data.get('model', models[0])
|
49 |
+
prompt = data.get('prompt', '')
|
50 |
+
|
51 |
+
if model not in models:
|
52 |
+
return {"error": "Model not found"}
|
53 |
+
|
54 |
+
# Translate prompt to English for API endpoint too
|
55 |
+
translated_prompt = translator.translate(prompt)
|
56 |
+
image = gen_image(model, translated_prompt)
|
57 |
+
if image is None:
|
58 |
+
return {"error": "Image generation failed"}
|
59 |
+
|
60 |
+
base64_str = image_to_base64(image)
|
61 |
+
|
62 |
+
return {
|
63 |
+
"status": "success",
|
64 |
+
"model": model,
|
65 |
+
"original_prompt": prompt,
|
66 |
+
"translated_prompt": translated_prompt,
|
67 |
+
"image_base64": base64_str,
|
68 |
+
"image_format": "jpeg"
|
69 |
+
}
|
70 |
+
|
71 |
+
# Gradio Interface
|
72 |
+
def make_me():
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column(scale=4):
|
75 |
+
txt_input = gr.Textbox(
|
76 |
+
label='Your prompt:',
|
77 |
+
lines=4,
|
78 |
+
container=False,
|
79 |
+
elem_id="custom_textbox",
|
80 |
+
placeholder="Prompt"
|
81 |
+
)
|
82 |
+
|
83 |
+
with gr.Column(scale=1):
|
84 |
+
gen_button = gr.Button('Generate image', elem_id="custom_gen_button")
|
85 |
+
stop_button = gr.Button('Stop', variant='secondary', interactive=False,
|
86 |
+
elem_id="custom_stop_button")
|
87 |
+
|
88 |
+
def on_generate_click():
|
89 |
+
return gr.Button('Generate image', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
|
90 |
+
|
91 |
+
def on_stop_click():
|
92 |
+
return gr.Button('Generate image', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
|
93 |
+
|
94 |
+
gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
|
95 |
+
stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
|
96 |
+
|
97 |
+
with gr.Row():
|
98 |
+
with gr.Column():
|
99 |
+
model_dropdown = gr.Dropdown(models, label="Select Model",
|
100 |
+
value=models[0] if models else None)
|
101 |
+
output_image = gr.Image(
|
102 |
+
label="Generated Image",
|
103 |
+
width=512,
|
104 |
+
height=768,
|
105 |
+
elem_id="custom_image",
|
106 |
+
show_label=True,
|
107 |
+
interactive=False
|
108 |
+
)
|
109 |
+
|
110 |
+
# JSON output
|
111 |
+
json_output = gr.JSON(label="API Response")
|
112 |
+
|
113 |
+
def generate_wrapper(model_str, prompt):
|
114 |
+
# Translate prompt to English
|
115 |
+
translated_prompt = translator.translate(prompt)
|
116 |
+
image = gen_image(model_str, translated_prompt)
|
117 |
+
if image is None:
|
118 |
+
return None, {"error": "Generation failed"}
|
119 |
+
|
120 |
+
base64_str = image_to_base64(image)
|
121 |
+
response = {
|
122 |
+
"status": "success",
|
123 |
+
"model": model_str,
|
124 |
+
"original_prompt": prompt,
|
125 |
+
"translated_prompt": translated_prompt,
|
126 |
+
"image_base64": base64_str,
|
127 |
+
"image_format": "jpeg"
|
128 |
+
}
|
129 |
+
return image, response
|
130 |
+
|
131 |
+
gen_event = gen_button.click(generate_wrapper, [model_dropdown, txt_input],
|
132 |
+
[output_image, json_output])
|
133 |
+
stop_button.click(on_stop_click, inputs=None,
|
134 |
+
outputs=[gen_button, stop_button], cancels=[gen_event])
|
135 |
+
|
136 |
+
# Create Gradio app
|
137 |
+
with gr.Blocks(css=css_code) as demo:
|
138 |
+
make_me()
|
139 |
+
|
140 |
+
# Enable queue before mounting
|
141 |
+
demo.queue()
|
142 |
+
|
143 |
+
# Mount Gradio app to FastAPI
|
144 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
145 |
+
|
146 |
+
if __name__ == "__main__":
|
147 |
+
import uvicorn
|
148 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
wheel
|
2 |
+
fastapi>=0.109.1
|
3 |
+
gradio>=4.19.1
|
4 |
+
Pillow>=10.1.0
|
5 |
+
deep-translator>=1.11.4
|
6 |
+
uvicorn>=0.27.0
|
7 |
+
numpy
|