vibs08 commited on
Commit
21583b0
·
verified ·
1 Parent(s): b47999a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +347 -268
app.py CHANGED
@@ -1,325 +1,400 @@
1
- from fastapi import FastAPI, File, UploadFile, Form
2
- from fastapi.responses import StreamingResponse
3
- from pydantic import BaseModel
4
- from typing import Optional
5
- import logging
6
- import os
7
- # import boto3
8
- import json
9
- # import shlex
10
- # import subprocess
11
- # import tempfile
12
- # import time
13
- # import base64
14
- # import gradio as gr
15
- # import numpy as np
16
- # import rembg
17
- # import spaces
18
- # import torch
19
- # from PIL import Image
20
- # from functools import partial
21
- # import io
22
- # import datetime
23
 
24
- app = FastAPI()
25
 
26
 
27
- # subprocess.run(shlex.split('pip install wheel/torchmcubes-0.1.0-cp310-cp310-linux_x86_64.whl'))
28
 
29
- # from tsr.system import TSR
30
- # from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
31
 
32
 
33
 
34
- # if torch.cuda.is_available():
35
- # device = "cuda:0"
36
- # else:
37
- # device = "cpu"
38
 
39
- # # torch.cuda.synchronize()
40
 
41
 
42
- # model = TSR.from_pretrained(
43
- # "stabilityai/TripoSR",
44
- # config_name="config.yaml",
45
- # weight_name="model.ckpt",
46
- # )
47
- # model.renderer.set_chunk_size(124218)
48
- # model.to(device)
49
 
50
- # rembg_session = rembg.new_session()
51
- # ACCESS = os.getenv("ACCESS")
52
- # SECRET = os.getenv("SECRET")
53
- # bedrock = boto3.client(service_name='bedrock', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
54
- # bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
55
 
56
- # def upload_file_to_s3(file_path, bucket_name, object_name=None):
57
- # s3_client = boto3.client('s3',aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
58
 
59
- # if object_name is None:
60
- # object_name = file_path
61
 
62
- # try:
63
- # s3_client.upload_file(file_path, bucket_name, object_name)
64
- # except FileNotFoundError:
65
- # print(f"The file {file_path} was not found.")
66
- # return False
67
- # except NoCredentialsError:
68
- # print("Credentials not available.")
69
- # return False
70
- # except PartialCredentialsError:
71
- # print("Incomplete credentials provided.")
72
- # return False
73
- # except Exception as e:
74
- # print(f"An error occurred: {e}")
75
- # return False
76
 
77
- # print(f"File {file_path} uploaded successfully to {bucket_name}/{object_name}.")
78
- # return True
79
 
80
 
81
- # def gen_pos_prompt(text):
82
- # instruction = f'''Your task is to create a positive prompt for image generation.
83
 
84
- # Objective: Generate images that prioritize structural integrity and accurate shapes. The focus should be on the correct form and basic contours of objects, with minimal concern for colors.
85
 
86
- # Guidelines:
87
 
88
- # Complex Objects (e.g., animals, vehicles, Machines, Fictional Characters, Fantasy and Mythical Creatures, Historical or Cultural Artifacts, Humanoid Figures,Buildings and Architecture): For these, the image should resemble a toy object, emphasizing the correct shape and structure while minimizing details and color complexity.
89
 
90
- # Example Input: A sports bike
91
- # Example Positive Prompt: Simple sports bike with accurate shape and structure, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, toy-like appearance, low contrast.
92
 
93
- # Example Input: A lion
94
- # Example Positive Prompt: Toy-like depiction of a lion with a focus on structural accuracy, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, simplified features, low contrast.
95
 
96
- # Input: The Spiderman with Wolverine Claws
97
- # Positive Prompt: Toy-like depiction of Spiderman with Wolverine claws, emphasizing structural accuracy with minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, simplified features, low contrast.
98
 
99
- # Simple Objects (e.g., a tennis ball): For these, the prompt should specify a realistic depiction, focusing on the accurate shape and structure.
100
 
101
- # Example Input: A tennis ball
102
- # Example Positive Prompt: photorealistic, uhd, high resolution, high quality, highly detailed; A tennis ball
103
 
104
- # Prompt Structure:
105
 
106
- # Subject: Clearly describe the object and its essential shape and structure.
107
- # Medium: Specify the art style (e.g., digital painting, concept art).
108
- # Style: Include relevant style terms (e.g., simplified, toy-like for complex objects; realistic for simple objects).
109
- # Resolution: Mention resolution if necessary (e.g., basic resolution).
110
- # Lighting: Indicate the type of lighting (e.g., soft lighting).
111
- # Color: Use neutral or muted colors with minimal emphasis on color details.
112
- # Additional Details: Keep additional details minimal.
113
-
114
-
115
- # ### MAXIMUM OUTPUT LENGTH SHOULD BE UNDER 30 WORDS ###
116
-
117
- # Input: {text}
118
- # Positive Prompt:
119
- # '''
120
-
121
- # body = json.dumps({'inputText': instruction,
122
- # 'textGenerationConfig': {'temperature': 0, 'topP': 0.01, 'maxTokenCount':1024}})
123
- # response = bedrock_runtime.invoke_model(body=body, modelId='amazon.titan-text-express-v1')
124
- # pos_prompt = json.loads(response.get('body').read())['results'][0]['outputText']
125
- # return pos_prompt
126
-
127
- # def generate_image_from_text(pos_prompt, seed):
128
- # new_prompt = gen_pos_prompt(pos_prompt)
129
- # # print(new_prompt)
130
- # # neg_prompt = '''Detailed, complex textures, intricate patterns, realistic lighting, high contrast, reflections, fuzzy surface, realistic proportions, photographic quality, vibrant colors, detailed background, shadows, disfigured, deformed, ugly, multiple, duplicate.'''
131
- # neg_prompt = '''Out of frame, blurry, ugly, cropped, reflections, detailed background, shadows, disfigured, deformed, ugly, multiple, duplicate.'''
132
- # # neg_prompt = '''Complex patterns, realistic lighting, high contrast, reflections, fuzzy, photographic, vibrant, detailed, shadows, disfigured, duplicate.'''
133
 
134
- # parameters = {
135
- # 'taskType': 'TEXT_IMAGE',
136
- # 'textToImageParams': {'text': new_prompt,
137
- # 'negativeText': neg_prompt},
138
- # 'imageGenerationConfig': {"cfgScale":8,
139
- # "seed":int(seed),
140
- # "width":1024,
141
- # "height":1024,
142
- # "numberOfImages":1
143
- # }
144
- # }
145
- # request_body = json.dumps(parameters)
146
- # response = bedrock_runtime.invoke_model(body=request_body, modelId='amazon.titan-image-generator-v2:0')
147
- # response_body = json.loads(response.get('body').read())
148
- # base64_image_data = base64.b64decode(response_body['images'][0])
149
-
150
- # return Image.open(io.BytesIO(base64_image_data))
151
-
152
-
153
- # def check_input_image(input_image):
154
- # if input_image is None:
155
- # raise gr.Error("No image uploaded!")
156
-
157
- # def preprocess(input_image, do_remove_background, foreground_ratio):
158
- # def fill_background(image):
159
- # torch.cuda.synchronize() # Ensure previous CUDA operations are complete
160
- # image = np.array(image).astype(np.float32) / 255.0
161
- # image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
162
- # image = Image.fromarray((image * 255.0).astype(np.uint8))
163
- # return image
164
-
165
- # if do_remove_background:
166
- # torch.cuda.synchronize()
167
- # image = input_image.convert("RGB")
168
- # image = remove_background(image, rembg_session)
169
- # image = resize_foreground(image, foreground_ratio)
170
- # image = fill_background(image)
171
 
172
- # torch.cuda.synchronize()
173
- # else:
174
- # image = input_image
175
- # if image.mode == "RGBA":
176
- # image = fill_background(image)
177
- # torch.cuda.synchronize() # Wait for all CUDA operations to complete
178
- # torch.cuda.empty_cache()
179
- # return image
180
-
181
-
182
-
183
- # # @spaces.GPU
184
- # def generate(image, mc_resolution, formats=["obj", "glb"]):
185
- # torch.cuda.synchronize()
186
- # scene_codes = model(image, device=device)
187
- # torch.cuda.synchronize()
188
- # mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
189
- # torch.cuda.synchronize()
190
- # mesh = to_gradio_3d_orientation(mesh)
191
- # torch.cuda.synchronize()
192
 
193
- # mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f".glb", delete=False)
194
- # torch.cuda.synchronize()
195
- # mesh.export(mesh_path_glb.name)
196
- # torch.cuda.synchronize()
197
 
198
- # mesh_path_obj = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False)
199
- # torch.cuda.synchronize()
200
- # mesh.apply_scale([-1, 1, 1])
201
- # mesh.export(mesh_path_obj.name)
202
- # torch.cuda.synchronize()
203
- # torch.cuda.empty_cache()
204
- # return mesh_path_obj.name, mesh_path_glb.name
 
 
 
 
 
205
 
 
206
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
- # def run_example(text_prompt,seed ,do_remove_background, foreground_ratio, mc_resolution):
209
- # image_pil = generate_image_from_text(text_prompt, seed)
 
 
 
 
 
 
 
 
 
210
 
211
- # preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
 
213
- # mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution, ["obj", "glb"])
 
 
 
 
 
214
 
215
- # return preprocessed, mesh_name_obj, mesh_name_glb
 
 
 
 
 
 
 
 
216
 
217
  # from gradio_client import Client
218
  # import requests
219
- # import json
220
 
221
- # client = Client("vibs08/flash-sd3-new",hf_token=os.getenv("token"))
 
222
 
 
223
  # url = 'https://vibs08-image-3d-fastapi.hf.space/process_image/'
224
 
225
-
226
  # def text2img(promptt):
227
- # result = client.predict(
228
- # prompt=promptt,
229
- # seed=0,
230
- # randomize_seed=False,
231
- # guidance_scale=1,
232
- # num_inference_steps=4,
233
- # negative_prompt="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, NSFW, bad text",
234
- # api_name="/infer"
235
- # )
236
- # return result
237
-
238
-
239
- # def three_d(prompt,seed,fr,mc,auth,text=None):
240
-
241
- # file_path = text2img(prompt)
242
- # payload = {
243
- # 'seed': seed,
244
- # 'enhance_image': False,
245
- # 'do_remove_background': True,
246
- # 'foreground_ratio': fr,
247
- # 'mc_resolution': mc,
248
- # 'auth': auth,
249
- # 'text_prompt': text
250
- # }
251
-
252
- # files = {
253
- # 'file': (file_path, open(file_path, 'rb'), 'image/png')
254
- # }
255
-
256
- # headers = {
257
- # 'accept': 'application/json'
258
- # }
259
-
260
- # response = requests.post(url, headers=headers, files=files, data=payload)
261
-
262
- # return response.json()
 
 
 
 
 
 
 
 
263
  # @app.post("/process_text/")
264
- # async def process_image(
265
  # text_prompt: str = Form(...),
266
  # seed: int = Form(...),
267
  # foreground_ratio: float = Form(...),
268
  # mc_resolution: int = Form(...),
269
  # auth: str = Form(...)
270
  # ):
271
-
272
  # if auth == os.getenv("AUTHORIZE"):
273
  # return three_d(text_prompt, seed, foreground_ratio, mc_resolution, auth)
274
-
275
- # # else:
276
- # # return {"ERROR": "Too Many Requests!"}
277
-
278
- # # preprocessed, mesh_name_obj, mesh_name_glb = run_example(text_prompt,seed ,do_remove_background, foreground_ratio, mc_resolution)
279
- # # # preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
280
- # # # mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution)
281
- # # timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
282
- # # object_name = f'object_{timestamp}_1.obj'
283
- # # object_name_2 = f'object_{timestamp}_2.glb'
284
- # # object_name_3 = f"object_{timestamp}.png"
285
- # # preprocessed_image_tempfile = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
286
- # # preprocessed.save(preprocessed_image_tempfile.name)
287
- # # upload_file_to_s3(preprocessed_image_tempfile.name, 'framebucket3d', object_name_3)
288
-
289
-
290
- # # if upload_file_to_s3(mesh_name_obj, 'framebucket3d',object_name) and upload_file_to_s3(mesh_name_glb, 'framebucket3d',object_name_2):
291
-
292
- # # return {
293
- # # "img_path": f"https://framebucket3d.s3.amazonaws.com/{object_name_3}",
294
- # # "obj_path": f"https://framebucket3d.s3.amazonaws.com/{object_name}",
295
- # # "glb_path": f"https://framebucket3d.s3.amazonaws.com/{object_name_2}"
296
-
297
- # # }
298
-
299
- # # else:
300
- # # return {"Internal Server Error": False}
301
  # else:
302
- # return {"Authentication":"Failed"}
303
 
304
  # if __name__ == "__main__":
305
  # import uvicorn
306
  # uvicorn.run(app, host="0.0.0.0", port=7860)
307
 
308
 
309
- from gradio_client import Client
 
 
310
  import requests
311
  import os
312
 
313
- # Initialize Gradio client with Hugging Face token
314
- client = Client("vibs08/flash-sd3-new", hf_token=os.getenv("token"))
315
 
316
  # URL for processing image via FastAPI
317
  url = 'https://vibs08-image-3d-fastapi.hf.space/process_image/'
318
 
319
- def text2img(promptt):
320
  # Use the Gradio client to generate an image from text
321
  result = client.predict(
322
- prompt=promptt, # Adjust the argument name based on the actual method signature
323
  seed=0,
324
  randomize_seed=False,
325
  guidance_scale=1,
@@ -331,8 +406,8 @@ def text2img(promptt):
331
  # Assuming result is a file path or image data
332
  return result
333
 
334
- def three_d(promptt, seed, fr, mc, auth, text=None):
335
- file_path = text2img(promptt) # Get the file path of the generated image
336
 
337
  payload = {
338
  'seed': seed,
@@ -357,23 +432,27 @@ def three_d(promptt, seed, fr, mc, auth, text=None):
357
 
358
  return response.json()
359
 
360
- from fastapi import FastAPI, Form
361
-
362
- app = FastAPI()
363
-
364
- @app.post("/process_text/")
365
- async def process_text(
366
- text_prompt: str = Form(...),
367
- seed: int = Form(...),
368
- foreground_ratio: float = Form(...),
369
- mc_resolution: int = Form(...),
370
- auth: str = Form(...)
371
- ):
372
  if auth == os.getenv("AUTHORIZE"):
373
  return three_d(text_prompt, seed, foreground_ratio, mc_resolution, auth)
374
  else:
375
  return {"Authentication": "Failed"}
376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  if __name__ == "__main__":
378
- import uvicorn
379
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ # from fastapi import FastAPI, File, UploadFile, Form
2
+ # from fastapi.responses import StreamingResponse
3
+ # from pydantic import BaseModel
4
+ # from typing import Optional
5
+ # import logging
6
+ # import os
7
+ # # import boto3
8
+ # import json
9
+ # # import shlex
10
+ # # import subprocess
11
+ # # import tempfile
12
+ # # import time
13
+ # # import base64
14
+ # # import gradio as gr
15
+ # # import numpy as np
16
+ # # import rembg
17
+ # # import spaces
18
+ # # import torch
19
+ # # from PIL import Image
20
+ # # from functools import partial
21
+ # # import io
22
+ # # import datetime
23
 
24
+ # app = FastAPI()
25
 
26
 
27
+ # # subprocess.run(shlex.split('pip install wheel/torchmcubes-0.1.0-cp310-cp310-linux_x86_64.whl'))
28
 
29
+ # # from tsr.system import TSR
30
+ # # from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
31
 
32
 
33
 
34
+ # # if torch.cuda.is_available():
35
+ # # device = "cuda:0"
36
+ # # else:
37
+ # # device = "cpu"
38
 
39
+ # # # torch.cuda.synchronize()
40
 
41
 
42
+ # # model = TSR.from_pretrained(
43
+ # # "stabilityai/TripoSR",
44
+ # # config_name="config.yaml",
45
+ # # weight_name="model.ckpt",
46
+ # # )
47
+ # # model.renderer.set_chunk_size(124218)
48
+ # # model.to(device)
49
 
50
+ # # rembg_session = rembg.new_session()
51
+ # # ACCESS = os.getenv("ACCESS")
52
+ # # SECRET = os.getenv("SECRET")
53
+ # # bedrock = boto3.client(service_name='bedrock', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
54
+ # # bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
55
 
56
+ # # def upload_file_to_s3(file_path, bucket_name, object_name=None):
57
+ # # s3_client = boto3.client('s3',aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
58
 
59
+ # # if object_name is None:
60
+ # # object_name = file_path
61
 
62
+ # # try:
63
+ # # s3_client.upload_file(file_path, bucket_name, object_name)
64
+ # # except FileNotFoundError:
65
+ # # print(f"The file {file_path} was not found.")
66
+ # # return False
67
+ # # except NoCredentialsError:
68
+ # # print("Credentials not available.")
69
+ # # return False
70
+ # # except PartialCredentialsError:
71
+ # # print("Incomplete credentials provided.")
72
+ # # return False
73
+ # # except Exception as e:
74
+ # # print(f"An error occurred: {e}")
75
+ # # return False
76
 
77
+ # # print(f"File {file_path} uploaded successfully to {bucket_name}/{object_name}.")
78
+ # # return True
79
 
80
 
81
+ # # def gen_pos_prompt(text):
82
+ # # instruction = f'''Your task is to create a positive prompt for image generation.
83
 
84
+ # # Objective: Generate images that prioritize structural integrity and accurate shapes. The focus should be on the correct form and basic contours of objects, with minimal concern for colors.
85
 
86
+ # # Guidelines:
87
 
88
+ # # Complex Objects (e.g., animals, vehicles, Machines, Fictional Characters, Fantasy and Mythical Creatures, Historical or Cultural Artifacts, Humanoid Figures,Buildings and Architecture): For these, the image should resemble a toy object, emphasizing the correct shape and structure while minimizing details and color complexity.
89
 
90
+ # # Example Input: A sports bike
91
+ # # Example Positive Prompt: Simple sports bike with accurate shape and structure, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, toy-like appearance, low contrast.
92
 
93
+ # # Example Input: A lion
94
+ # # Example Positive Prompt: Toy-like depiction of a lion with a focus on structural accuracy, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, simplified features, low contrast.
95
 
96
+ # # Input: The Spiderman with Wolverine Claws
97
+ # # Positive Prompt: Toy-like depiction of Spiderman with Wolverine claws, emphasizing structural accuracy with minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, simplified features, low contrast.
98
 
99
+ # # Simple Objects (e.g., a tennis ball): For these, the prompt should specify a realistic depiction, focusing on the accurate shape and structure.
100
 
101
+ # # Example Input: A tennis ball
102
+ # # Example Positive Prompt: photorealistic, uhd, high resolution, high quality, highly detailed; A tennis ball
103
 
104
+ # # Prompt Structure:
105
 
106
+ # # Subject: Clearly describe the object and its essential shape and structure.
107
+ # # Medium: Specify the art style (e.g., digital painting, concept art).
108
+ # # Style: Include relevant style terms (e.g., simplified, toy-like for complex objects; realistic for simple objects).
109
+ # # Resolution: Mention resolution if necessary (e.g., basic resolution).
110
+ # # Lighting: Indicate the type of lighting (e.g., soft lighting).
111
+ # # Color: Use neutral or muted colors with minimal emphasis on color details.
112
+ # # Additional Details: Keep additional details minimal.
113
+
114
+
115
+ # # ### MAXIMUM OUTPUT LENGTH SHOULD BE UNDER 30 WORDS ###
116
+
117
+ # # Input: {text}
118
+ # # Positive Prompt:
119
+ # # '''
120
+
121
+ # # body = json.dumps({'inputText': instruction,
122
+ # # 'textGenerationConfig': {'temperature': 0, 'topP': 0.01, 'maxTokenCount':1024}})
123
+ # # response = bedrock_runtime.invoke_model(body=body, modelId='amazon.titan-text-express-v1')
124
+ # # pos_prompt = json.loads(response.get('body').read())['results'][0]['outputText']
125
+ # # return pos_prompt
126
+
127
+ # # def generate_image_from_text(pos_prompt, seed):
128
+ # # new_prompt = gen_pos_prompt(pos_prompt)
129
+ # # # print(new_prompt)
130
+ # # # neg_prompt = '''Detailed, complex textures, intricate patterns, realistic lighting, high contrast, reflections, fuzzy surface, realistic proportions, photographic quality, vibrant colors, detailed background, shadows, disfigured, deformed, ugly, multiple, duplicate.'''
131
+ # # neg_prompt = '''Out of frame, blurry, ugly, cropped, reflections, detailed background, shadows, disfigured, deformed, ugly, multiple, duplicate.'''
132
+ # # # neg_prompt = '''Complex patterns, realistic lighting, high contrast, reflections, fuzzy, photographic, vibrant, detailed, shadows, disfigured, duplicate.'''
133
 
134
+ # # parameters = {
135
+ # # 'taskType': 'TEXT_IMAGE',
136
+ # # 'textToImageParams': {'text': new_prompt,
137
+ # # 'negativeText': neg_prompt},
138
+ # # 'imageGenerationConfig': {"cfgScale":8,
139
+ # # "seed":int(seed),
140
+ # # "width":1024,
141
+ # # "height":1024,
142
+ # # "numberOfImages":1
143
+ # # }
144
+ # # }
145
+ # # request_body = json.dumps(parameters)
146
+ # # response = bedrock_runtime.invoke_model(body=request_body, modelId='amazon.titan-image-generator-v2:0')
147
+ # # response_body = json.loads(response.get('body').read())
148
+ # # base64_image_data = base64.b64decode(response_body['images'][0])
149
+
150
+ # # return Image.open(io.BytesIO(base64_image_data))
151
+
152
+
153
+ # # def check_input_image(input_image):
154
+ # # if input_image is None:
155
+ # # raise gr.Error("No image uploaded!")
156
+
157
+ # # def preprocess(input_image, do_remove_background, foreground_ratio):
158
+ # # def fill_background(image):
159
+ # # torch.cuda.synchronize() # Ensure previous CUDA operations are complete
160
+ # # image = np.array(image).astype(np.float32) / 255.0
161
+ # # image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
162
+ # # image = Image.fromarray((image * 255.0).astype(np.uint8))
163
+ # # return image
164
+
165
+ # # if do_remove_background:
166
+ # # torch.cuda.synchronize()
167
+ # # image = input_image.convert("RGB")
168
+ # # image = remove_background(image, rembg_session)
169
+ # # image = resize_foreground(image, foreground_ratio)
170
+ # # image = fill_background(image)
171
 
172
+ # # torch.cuda.synchronize()
173
+ # # else:
174
+ # # image = input_image
175
+ # # if image.mode == "RGBA":
176
+ # # image = fill_background(image)
177
+ # # torch.cuda.synchronize() # Wait for all CUDA operations to complete
178
+ # # torch.cuda.empty_cache()
179
+ # # return image
180
+
181
+
182
+
183
+ # # # @spaces.GPU
184
+ # # def generate(image, mc_resolution, formats=["obj", "glb"]):
185
+ # # torch.cuda.synchronize()
186
+ # # scene_codes = model(image, device=device)
187
+ # # torch.cuda.synchronize()
188
+ # # mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
189
+ # # torch.cuda.synchronize()
190
+ # # mesh = to_gradio_3d_orientation(mesh)
191
+ # # torch.cuda.synchronize()
192
 
193
+ # # mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f".glb", delete=False)
194
+ # # torch.cuda.synchronize()
195
+ # # mesh.export(mesh_path_glb.name)
196
+ # # torch.cuda.synchronize()
197
 
198
+ # # mesh_path_obj = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False)
199
+ # # torch.cuda.synchronize()
200
+ # # mesh.apply_scale([-1, 1, 1])
201
+ # # mesh.export(mesh_path_obj.name)
202
+ # # torch.cuda.synchronize()
203
+ # # torch.cuda.empty_cache()
204
+ # # return mesh_path_obj.name, mesh_path_glb.name
205
+
206
+
207
+
208
+ # # def run_example(text_prompt,seed ,do_remove_background, foreground_ratio, mc_resolution):
209
+ # # image_pil = generate_image_from_text(text_prompt, seed)
210
 
211
+ # # preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
212
 
213
+ # # mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution, ["obj", "glb"])
214
+
215
+ # # return preprocessed, mesh_name_obj, mesh_name_glb
216
+
217
+ # # from gradio_client import Client
218
+ # # import requests
219
+ # # import json
220
+
221
+ # # client = Client("vibs08/flash-sd3-new",hf_token=os.getenv("token"))
222
+
223
+ # # url = 'https://vibs08-image-3d-fastapi.hf.space/process_image/'
224
+
225
 
226
+ # # def text2img(promptt):
227
+ # # result = client.predict(
228
+ # # prompt=promptt,
229
+ # # seed=0,
230
+ # # randomize_seed=False,
231
+ # # guidance_scale=1,
232
+ # # num_inference_steps=4,
233
+ # # negative_prompt="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, NSFW, bad text",
234
+ # # api_name="/infer"
235
+ # # )
236
+ # # return result
237
 
238
+
239
+ # # def three_d(prompt,seed,fr,mc,auth,text=None):
240
+
241
+ # # file_path = text2img(prompt)
242
+ # # payload = {
243
+ # # 'seed': seed,
244
+ # # 'enhance_image': False,
245
+ # # 'do_remove_background': True,
246
+ # # 'foreground_ratio': fr,
247
+ # # 'mc_resolution': mc,
248
+ # # 'auth': auth,
249
+ # # 'text_prompt': text
250
+ # # }
251
+
252
+ # # files = {
253
+ # # 'file': (file_path, open(file_path, 'rb'), 'image/png')
254
+ # # }
255
+
256
+ # # headers = {
257
+ # # 'accept': 'application/json'
258
+ # # }
259
+
260
+ # # response = requests.post(url, headers=headers, files=files, data=payload)
261
+
262
+ # # return response.json()
263
+ # # @app.post("/process_text/")
264
+ # # async def process_image(
265
+ # # text_prompt: str = Form(...),
266
+ # # seed: int = Form(...),
267
+ # # foreground_ratio: float = Form(...),
268
+ # # mc_resolution: int = Form(...),
269
+ # # auth: str = Form(...)
270
+ # # ):
271
+
272
+ # # if auth == os.getenv("AUTHORIZE"):
273
+ # # return three_d(text_prompt, seed, foreground_ratio, mc_resolution, auth)
274
+
275
+ # # # else:
276
+ # # # return {"ERROR": "Too Many Requests!"}
277
+
278
+ # # # preprocessed, mesh_name_obj, mesh_name_glb = run_example(text_prompt,seed ,do_remove_background, foreground_ratio, mc_resolution)
279
+ # # # # preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
280
+ # # # # mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution)
281
+ # # # timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
282
+ # # # object_name = f'object_{timestamp}_1.obj'
283
+ # # # object_name_2 = f'object_{timestamp}_2.glb'
284
+ # # # object_name_3 = f"object_{timestamp}.png"
285
+ # # # preprocessed_image_tempfile = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
286
+ # # # preprocessed.save(preprocessed_image_tempfile.name)
287
+ # # # upload_file_to_s3(preprocessed_image_tempfile.name, 'framebucket3d', object_name_3)
288
+
289
+
290
+ # # # if upload_file_to_s3(mesh_name_obj, 'framebucket3d',object_name) and upload_file_to_s3(mesh_name_glb, 'framebucket3d',object_name_2):
291
 
292
+ # # # return {
293
+ # # # "img_path": f"https://framebucket3d.s3.amazonaws.com/{object_name_3}",
294
+ # # # "obj_path": f"https://framebucket3d.s3.amazonaws.com/{object_name}",
295
+ # # # "glb_path": f"https://framebucket3d.s3.amazonaws.com/{object_name_2}"
296
+
297
+ # # # }
298
 
299
+ # # # else:
300
+ # # # return {"Internal Server Error": False}
301
+ # # else:
302
+ # # return {"Authentication":"Failed"}
303
+
304
+ # # if __name__ == "__main__":
305
+ # # import uvicorn
306
+ # # uvicorn.run(app, host="0.0.0.0", port=7860)
307
+
308
 
309
  # from gradio_client import Client
310
  # import requests
311
+ # import os
312
 
313
+ # # Initialize Gradio client with Hugging Face token
314
+ # client = Client("vibs08/flash-sd3-new", hf_token=os.getenv("token"))
315
 
316
+ # # URL for processing image via FastAPI
317
  # url = 'https://vibs08-image-3d-fastapi.hf.space/process_image/'
318
 
 
319
  # def text2img(promptt):
320
+ # # Use the Gradio client to generate an image from text
321
+ # result = client.predict(
322
+ # prompt=promptt, # Adjust the argument name based on the actual method signature
323
+ # seed=0,
324
+ # randomize_seed=False,
325
+ # guidance_scale=1,
326
+ # num_inference_steps=4,
327
+ # negative_prompt="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, NSFW, bad text",
328
+ # api_name="/infer"
329
+ # )
330
+
331
+ # # Assuming result is a file path or image data
332
+ # return result
333
+
334
+ # def three_d(promptt, seed, fr, mc, auth, text=None):
335
+ # file_path = text2img(promptt) # Get the file path of the generated image
336
+
337
+ # payload = {
338
+ # 'seed': seed,
339
+ # 'enhance_image': False,
340
+ # 'do_remove_background': True,
341
+ # 'foreground_ratio': fr,
342
+ # 'mc_resolution': mc,
343
+ # 'auth': auth,
344
+ # 'text_prompt': text
345
+ # }
346
+
347
+ # with open(file_path, 'rb') as image_file:
348
+ # files = {
349
+ # 'file': (file_path, image_file, 'image/png')
350
+ # }
351
+
352
+ # headers = {
353
+ # 'accept': 'application/json'
354
+ # }
355
+
356
+ # response = requests.post(url, headers=headers, files=files, data=payload)
357
+
358
+ # return response.json()
359
+
360
+ # from fastapi import FastAPI, Form
361
+
362
+ # app = FastAPI()
363
+
364
  # @app.post("/process_text/")
365
+ # async def process_text(
366
  # text_prompt: str = Form(...),
367
  # seed: int = Form(...),
368
  # foreground_ratio: float = Form(...),
369
  # mc_resolution: int = Form(...),
370
  # auth: str = Form(...)
371
  # ):
 
372
  # if auth == os.getenv("AUTHORIZE"):
373
  # return three_d(text_prompt, seed, foreground_ratio, mc_resolution, auth)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  # else:
375
+ # return {"Authentication": "Failed"}
376
 
377
  # if __name__ == "__main__":
378
  # import uvicorn
379
  # uvicorn.run(app, host="0.0.0.0", port=7860)
380
 
381
 
382
+
383
+ import gradio as gr
384
+ # from gradio_client import Client
385
  import requests
386
  import os
387
 
388
+ # # Initialize Gradio client with Hugging Face token
389
+ # client = Client("vibs08/flash-sd3-new", hf_token=os.getenv("token"))
390
 
391
  # URL for processing image via FastAPI
392
  url = 'https://vibs08-image-3d-fastapi.hf.space/process_image/'
393
 
394
+ def text2img(prompt):
395
  # Use the Gradio client to generate an image from text
396
  result = client.predict(
397
+ prompt=prompt,
398
  seed=0,
399
  randomize_seed=False,
400
  guidance_scale=1,
 
406
  # Assuming result is a file path or image data
407
  return result
408
 
409
+ def three_d(prompt, seed, fr, mc, auth, text=None):
410
+ file_path = text2img(prompt) # Get the file path of the generated image
411
 
412
  payload = {
413
  'seed': seed,
 
432
 
433
  return response.json()
434
 
435
+ def process_input(text_prompt, seed, foreground_ratio, mc_resolution, auth):
 
 
 
 
 
 
 
 
 
 
 
436
  if auth == os.getenv("AUTHORIZE"):
437
  return three_d(text_prompt, seed, foreground_ratio, mc_resolution, auth)
438
  else:
439
  return {"Authentication": "Failed"}
440
 
441
+ # Create Gradio Interface
442
+ interface = gr.Interface(
443
+ fn=process_input,
444
+ inputs=[
445
+ gr.Textbox(label="Text Prompt"),
446
+ gr.Number(label="Seed"),
447
+ gr.Number(label="Foreground Ratio"),
448
+ gr.Number(label="MC Resolution"),
449
+ gr.Textbox(label="Authorization Token", type="password")
450
+ ],
451
+ outputs="json",
452
+ title="3D Image Generator",
453
+ description="Generate 3D images from text prompts"
454
+ )
455
+
456
+ # Launch the Gradio Interface
457
  if __name__ == "__main__":
458
+ interface.launch()