Ashrafb commited on
Commit
b1e8821
โ€ข
1 Parent(s): ec2ad06

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +1 -198
main.py CHANGED
@@ -1,201 +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
- proc1 = gr.Interface.load("models/Yntec/DreamShaperRemix")
21
-
22
-
23
-
24
-
25
- text_gen = gr.Interface.load("models/Gustavosta/MagicPrompt-Stable-Diffusion")
26
-
27
-
28
- queue = Queue()
29
- queue_threshold = 100
30
-
31
- def add_random_noise(prompt, noise_level=0.00):
32
- if noise_level == 0:
33
- noise_level = 0.00
34
- percentage_noise = noise_level * 5
35
- num_noise_chars = int(len(prompt) * (percentage_noise / 100))
36
- noise_indices = random.sample(range(len(prompt)), num_noise_chars)
37
- prompt_list = list(prompt)
38
- noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
39
- noise_chars.extend(['๐Ÿ˜', '๐Ÿ’ฉ', '๐Ÿ˜‚', '๐Ÿค”', '๐Ÿ˜Š', '๐Ÿค—', '๐Ÿ˜ญ', '๐Ÿ™„', '๐Ÿ˜ท', '๐Ÿคฏ', '๐Ÿคซ', '๐Ÿฅด', '๐Ÿ˜ด', '๐Ÿคฉ', '๐Ÿฅณ', '๐Ÿ˜”', '๐Ÿ˜ฉ', '๐Ÿคช', '๐Ÿ˜‡', '๐Ÿคข', '๐Ÿ˜ˆ', '๐Ÿ‘น', '๐Ÿ‘ป', '๐Ÿค–', '๐Ÿ‘ฝ', '๐Ÿ’€', '๐ŸŽƒ', '๐ŸŽ…', '๐ŸŽ„', '๐ŸŽ', '๐ŸŽ‚', '๐ŸŽ‰', '๐ŸŽˆ', '๐ŸŽŠ', '๐ŸŽฎ', 'โค๏ธ', '๐Ÿ’”', '๐Ÿ’•', '๐Ÿ’–', '๐Ÿ’—', '๐Ÿถ', '๐Ÿฑ', '๐Ÿญ', '๐Ÿน', '๐ŸฆŠ', '๐Ÿป', '๐Ÿจ', '๐Ÿฏ', '๐Ÿฆ', '๐Ÿ˜', '๐Ÿ”ฅ', '๐ŸŒง๏ธ', '๐ŸŒž', '๐ŸŒˆ', '๐Ÿ’ฅ', '๐ŸŒด', '๐ŸŒŠ', '๐ŸŒบ', '๐ŸŒป', '๐ŸŒธ', '๐ŸŽจ', '๐ŸŒ…', '๐ŸŒŒ', 'โ˜๏ธ', 'โ›ˆ๏ธ', 'โ„๏ธ', 'โ˜€๏ธ', '๐ŸŒค๏ธ', 'โ›…๏ธ', '๐ŸŒฅ๏ธ', '๐ŸŒฆ๏ธ', '๐ŸŒง๏ธ', '๐ŸŒฉ๏ธ', '๐ŸŒจ๏ธ', '๐ŸŒซ๏ธ', 'โ˜”๏ธ', '๐ŸŒฌ๏ธ', '๐Ÿ’จ', '๐ŸŒช๏ธ', '๐ŸŒˆ'])
40
- for index in noise_indices:
41
- prompt_list[index] = random.choice(noise_chars)
42
- return "".join(prompt_list)
43
-
44
- # Existing code...
45
-
46
- import uuid # Import the UUID library
47
-
48
- # Existing code...
49
-
50
- # Existing code...
51
-
52
- request_counter = 0 # Global counter to track requests
53
-
54
-
55
-
56
- def send_it1(inputs, noise_level, proc=proc1):
57
- global request_counter
58
- request_counter += 1
59
- timestamp = f"{time.time()}_{request_counter}"
60
-
61
- # Translate input text to English
62
- translated_input = GoogleTranslator(source='auto', target='en').translate(inputs)
63
-
64
- # Add noise to the translated input
65
- prompt_with_noise = add_random_noise(translated_input, noise_level) + f" - {timestamp}"
66
-
67
- while queue.qsize() >= queue_threshold:
68
- time.sleep(2)
69
-
70
- queue.put(prompt_with_noise)
71
-
72
- output = proc(prompt_with_noise)
73
-
74
- return output
75
-
76
-
77
- def get_prompts(prompt_text):
78
- if not prompt_text:
79
- return "Please enter text before generating prompts."
80
- raise gr.Error("Please enter text before generating prompts.")
81
- else:
82
- global request_counter
83
- request_counter += 1
84
- timestamp = f"{time.time()}_{request_counter}"
85
-
86
- options = [
87
- "Cyberpunk android",
88
- "2060",
89
- "newyork",
90
- ", 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"
91
- ", Editorial Photography, Shot on 70mm lens, Depth of Field, Bokeh, DOF, Tilt Blur, Shutter Speed 1/1000, F/22, 32k, Super-Resolution, award winning,",
92
- ", high detail, warm lighting, godrays, vivid, beautiful, trending on artstation, by jordan grimmer, huge scene, grass, art greg rutkowski ",
93
- ", highly detailed, digital painting, artstation, illustration, art by artgerm and greg rutkowski and alphonse mucha.",
94
- ", 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",
95
- ", 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),",
96
- ",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)))",
97
- "facinating and imposing" , "fantasy digital art, octane render, beautiful composition" ," trending on artstation, award-winning photograph, masterpiece",
98
-
99
- "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",
100
-
101
- " intricate, elegant, highly detailed" , "digital painting, artstation, concept art, smooth, sharp focus" ," illustration, art by artgerm and greg rutkowski and alphonse mucha, 8k",
102
- " matte painting, highly detailed, dynamic lighting, cinematic, realism, realistic" , "photo real, sunset,detailed, high contrast, denoised, centered, michael whelan",
103
-
104
-
105
- "cyborg| full-length portrait", "detailed face", "symmetric| steampunk", "cyberpunk| cyborg| intricate detailed| to scale", "hyperrealistic", "cinematic lighting| digital art| concept art",
106
-
107
- "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",
108
- "street| old town| old city| winter| heavy snow", "| comprehensive cinematic", "| Atmosphere| Masterpiece",
109
- "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",
110
-
111
- " (((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",
112
-
113
- "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",
114
- " : concept art::1 cyberpunk::1 splatter paint::1 wide angle lens::2 Zdzislaw Beksinski style" , "winter storm, ultra detailed| 4k โ€“v 4",
115
- "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",
116
-
117
-
118
- "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",
119
-
120
- "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",
121
-
122
- "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,",
123
-
124
-
125
-
126
- "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",
127
-
128
-
129
- # Add other prompt options here...
130
- ]
131
-
132
- if prompt_text:
133
- chosen_option = random.choice(options)
134
- return text_gen(f"{prompt_text}, {chosen_option} - {timestamp}")
135
- else:
136
- return text_gen("", timestamp)
137
-
138
- def initialize_api_connection():
139
- """
140
- Initialize API connections including text generation and processing models.
141
- """
142
- global text_gen, proc1
143
-
144
- # Load text generation model
145
- text_gen = gr.Interface.load("models/Gustavosta/MagicPrompt-Stable-Diffusion")
146
-
147
- # Load processing model
148
- proc1 = gr.Interface.load("models/Yntec/DreamShaperRemix")
149
-
150
- # Call the initialize_api_connection function to setup tasks on application startup
151
- initialize_api_connection()
152
-
153
- from fastapi.middleware.cors import CORSMiddleware
154
-
155
- app.add_middleware(
156
- CORSMiddleware,
157
- allow_origins=["*"], # Adjust as needed, '*' allows requests from any origin
158
- allow_credentials=True,
159
- allow_methods=["*"],
160
- allow_headers=["*"],
161
- )
162
-
163
-
164
-
165
- @app.get("/generate_prompts")
166
- def generate_prompts(prompt_text: str):
167
- return get_prompts(prompt_text)
168
-
169
-
170
-
171
- from fastapi import Query
172
-
173
- from fastapi import HTTPException
174
-
175
- @app.get("/send_inputs")
176
- async def send_inputs(inputs: str = Query(..., description="Input text to process"),
177
- noise_level: float = Query(0.0, ge=0.0, le=1.0, description="Level of random noise")):
178
- """
179
- Endpoint to send inputs for processing.
180
- """
181
- try:
182
- output_file_path = send_it1(inputs, noise_level) # Assuming send_it1 returns a file path
183
-
184
- # Check if the file exists
185
- if not os.path.exists(output_file_path):
186
- raise HTTPException(status_code=404, detail="File not found")
187
-
188
- # Read the image file and convert it to BytesIO
189
- with open(output_file_path, "rb") as file:
190
- image_data = file.read()
191
- image_bytes_io = io.BytesIO(image_data)
192
-
193
- # Encode the BytesIO object to base64
194
- encoded_image = base64.b64encode(image_bytes_io.getvalue()).decode()
195
-
196
- return {"result_base64": encoded_image}
197
- except Exception as e:
198
- raise HTTPException(status_code=500, detail=str(e))
199
-
200
 
201
 
 
 
 
 
 
 
1
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
 
4
+ exec(os.environ.get('CODE'))