Spaces:
Runtime error
Runtime error
| import requests | |
| import gradio as gr | |
| import time | |
| import os | |
| class Model3d_generater: | |
| def __init__(self): | |
| api_key = os.getenv("MESHY_API_KEY") | |
| self.headers = { | |
| "Authorization": f"Bearer {api_key}" | |
| } | |
| def _get_access_(self,query,style_prompt,resolution): | |
| payload = { | |
| "object_prompt":query, | |
| "style_prompt":style_prompt, | |
| "enable_pbr": True, | |
| "resolution":resolution, | |
| "art_style": "realistic", | |
| # "negative_prompt": "low quality, low resolution, low poly, ugly" | |
| } | |
| generate_3d = requests.post( | |
| "https://api.meshy.ai/v1/text-to-3d", | |
| headers=self.headers, | |
| json=payload, | |
| ) | |
| task_id=generate_3d.json()["result"] | |
| return task_id | |
| def check_status(self,task_id): | |
| response =requests.get( f"https://api.meshy.ai/v2/text-to-3d/{task_id}", | |
| headers=self.headers,) | |
| if response.status_code == 200: | |
| content=response.content | |
| status=response.json() | |
| return content,status | |
| else: | |
| return "Failed to get status" | |
| def _generate_3d_(self,query,style_prompt,resolution): | |
| task_id=self._get_access_(query,style_prompt,resolution) | |
| while True: | |
| content, status= self.check_status(task_id) | |
| if status['status'] == 'SUCCEEDED': | |
| print("Process complete. Model URLs:") | |
| download_3d=status["model_urls"]["glb"] | |
| response = requests.get(download_3d, allow_redirects=False) | |
| short_filename = '3d_asset.glb' # Change 'my_file.ext' to whatever you prefer, with the correct extension | |
| with open(short_filename, 'wb') as file: | |
| file.write(response.content) | |
| return short_filename | |
| elif status and status['status'] == 'FAILED': | |
| print("An error occurred during processing.") | |
| break | |
| else: | |
| print("Processing... Please wait.") | |
| time.sleep(30) | |
| def interface(self): | |
| css=""".gradio-container {background: rgb(157,228,255); | |
| background: radial-gradient(circle, rgba(157,228,255,1) 0%, rgba(18,115,106,1) 100%);} | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| gr.HTML(""" | |
| <center><h1 style="color:#fff">3d Model Generater</h1></center>""") | |
| with gr.Row(): | |
| prompt=gr.Textbox(label="Prompt") | |
| with gr.Column(scale=0.15): | |
| resolution=gr.Dropdown(choices=["1024","2048","4096"],label="Resolution") | |
| with gr.Row(): | |
| style=gr.Textbox(placeholder="describe the style like HDR,high quality,extrac",label="Style Prompt") | |
| with gr.Row(): | |
| output=gr.Model3D() | |
| with gr.Row(): | |
| button=gr.Button() | |
| button.click(self._generate_3d_,[prompt,style,resolution],outputs=output) | |
| demo.launch() | |
| if __name__=="__main__": | |
| result=Model3d_generater() | |
| result.interface() |