Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.get_env("MESHY_API_KEY")
|
9 |
+
self.headers = {
|
10 |
+
"Authorization": f"Bearer {api_key}"
|
11 |
+
}
|
12 |
+
self.query=''
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
def _generate_3d_(self,query):
|
17 |
+
self.query = query
|
18 |
+
print(f"query is {self.query}")
|
19 |
+
payload = {
|
20 |
+
"mode": "preview",
|
21 |
+
"prompt":query,
|
22 |
+
"art_style": "realistic",
|
23 |
+
"seed":1565957778,
|
24 |
+
"negative_prompt":"messy hands and feet, blurry mess, three eyes",
|
25 |
+
"texture_richness":"high"
|
26 |
+
}
|
27 |
+
|
28 |
+
generate_3d= requests.post(
|
29 |
+
"https://api.meshy.ai/v2/text-to-3d",
|
30 |
+
headers=self.headers,
|
31 |
+
json=payload,
|
32 |
+
)
|
33 |
+
task_id=generate_3d.json()["result"]
|
34 |
+
|
35 |
+
return task_id
|
36 |
+
def _get_task_id_(self,query):
|
37 |
+
|
38 |
+
try:
|
39 |
+
task_id=self._generate_3d_(query)
|
40 |
+
|
41 |
+
time.sleep(50)
|
42 |
+
|
43 |
+
get_asset= requests.get(
|
44 |
+
f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
|
45 |
+
headers=self.headers,
|
46 |
+
)
|
47 |
+
|
48 |
+
|
49 |
+
download_3d=get_asset.json()["model_urls"]["glb"]
|
50 |
+
|
51 |
+
return download_3d
|
52 |
+
except:
|
53 |
+
print("error on _get_task_id_")
|
54 |
+
|
55 |
+
def _download_3d(self,query):
|
56 |
+
try:
|
57 |
+
|
58 |
+
download_3d=self._get_task_id_(query)
|
59 |
+
response = requests.get(download_3d, allow_redirects=False)
|
60 |
+
short_filename = '3d_asset.glb' # Change 'my_file.ext' to whatever you prefer, with the correct extension
|
61 |
+
|
62 |
+
with open(short_filename, 'wb') as file:
|
63 |
+
file.write(response.content)
|
64 |
+
return short_filename
|
65 |
+
except:
|
66 |
+
print("error on _download_3d")
|
67 |
+
|
68 |
+
def interface(self):
|
69 |
+
with gr.Blocks(css=".gradio-container {background-color: red}") as demo:
|
70 |
+
with gr.Row():
|
71 |
+
prompt=gr.Textbox("Prompt")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
output=gr.Model3D()
|
75 |
+
with gr.Row():
|
76 |
+
button=gr.Button()
|
77 |
+
|
78 |
+
button.click(self._download_3d,prompt,output)
|
79 |
+
|
80 |
+
demo.launch()
|