File size: 2,882 Bytes
f315cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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()