Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
class Model3d_generater:
|
| 6 |
+
|
| 7 |
+
def __init__(self):
|
| 8 |
+
api_key = os.getenv("MESHY_API_KEY")
|
| 9 |
+
self.headers = {
|
| 10 |
+
"Authorization": f"Bearer {api_key}"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def _get_access_(self,query,style_prompt,resolution):
|
| 14 |
+
|
| 15 |
+
payload = {
|
| 16 |
+
"object_prompt":query,
|
| 17 |
+
"style_prompt":style_prompt,
|
| 18 |
+
"enable_pbr": True,
|
| 19 |
+
"resolution":resolution,
|
| 20 |
+
"art_style": "realistic",
|
| 21 |
+
# "negative_prompt": "low quality, low resolution, low poly, ugly"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
generate_3d = requests.post(
|
| 25 |
+
"https://api.meshy.ai/v1/text-to-3d",
|
| 26 |
+
headers=self.headers,
|
| 27 |
+
json=payload,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
task_id=generate_3d.json()["result"]
|
| 31 |
+
return task_id
|
| 32 |
+
|
| 33 |
+
def check_status(self,task_id):
|
| 34 |
+
|
| 35 |
+
response =requests.get( f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
|
| 36 |
+
headers=self.headers,)
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
content=response.content
|
| 39 |
+
status=response.json()
|
| 40 |
+
return content,status
|
| 41 |
+
else:
|
| 42 |
+
return "Failed to get status"
|
| 43 |
+
|
| 44 |
+
def _generate_3d_(self,query,style_prompt,resolution):
|
| 45 |
+
task_id=self._get_access_(query,style_prompt,resolution)
|
| 46 |
+
while True:
|
| 47 |
+
content, status= self.check_status(task_id)
|
| 48 |
+
if status['status'] == 'SUCCEEDED':
|
| 49 |
+
print("Process complete. Model URLs:")
|
| 50 |
+
download_3d=status["model_urls"]["glb"]
|
| 51 |
+
response = requests.get(download_3d, allow_redirects=False)
|
| 52 |
+
short_filename = '3d_asset.glb' # Change 'my_file.ext' to whatever you prefer, with the correct extension
|
| 53 |
+
|
| 54 |
+
with open(short_filename, 'wb') as file:
|
| 55 |
+
file.write(response.content)
|
| 56 |
+
return short_filename
|
| 57 |
+
|
| 58 |
+
elif status and status['status'] == 'FAILED':
|
| 59 |
+
print("An error occurred during processing.")
|
| 60 |
+
break
|
| 61 |
+
else:
|
| 62 |
+
print("Processing... Please wait.")
|
| 63 |
+
|
| 64 |
+
time.sleep(30)
|
| 65 |
+
|
| 66 |
+
def interface(self):
|
| 67 |
+
css=""".gradio-container {background: rgb(157,228,255);
|
| 68 |
+
background: radial-gradient(circle, rgba(157,228,255,1) 0%, rgba(18,115,106,1) 100%);}
|
| 69 |
+
|
| 70 |
+
}
|
| 71 |
+
"""
|
| 72 |
+
with gr.Blocks(css=css) as demo:
|
| 73 |
+
gr.HTML("""
|
| 74 |
+
<center><h1 style="color:#fff">3d Model Generater</h1></center>""")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
prompt=gr.Textbox(label="Prompt")
|
| 78 |
+
with gr.Column(scale=0.15):
|
| 79 |
+
resolution=gr.Dropdown(choices=["1024","2048","4096"],label="Resolution")
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
style=gr.Textbox(placeholder="describe the style like HDR,high quality,extrac",label="Style Prompt")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
output=gr.Model3D()
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
button=gr.Button()
|
| 89 |
+
|
| 90 |
+
button.click(self._generate_3d_,[prompt,style,resolution],outputs=output)
|
| 91 |
+
|
| 92 |
+
demo.launch()
|
| 93 |
+
|
| 94 |
+
if __name__=="__main__":
|
| 95 |
+
result=Model3d_generater()
|
| 96 |
+
result.interface()
|