KarthickAdopleAI's picture
Create app.py
69fc467 verified
raw
history blame
1.9 kB
import requests
import gradio as gr
import time
import os
class Model3d_generater:
def __init__(self):
api_key = os.get_env("MESHY_API_KEY")
self.headers = {
"Authorization": f"Bearer {api_key}"
}
self.query=''
def _generate_3d_(self,query):
self.query = query
print(f"query is {self.query}")
payload = {
"mode": "preview",
"prompt":query,
"art_style": "realistic",
"seed":1565957778,
"negative_prompt":"messy hands and feet, blurry mess, three eyes",
"texture_richness":"high"
}
generate_3d= requests.post(
"https://api.meshy.ai/v2/text-to-3d",
headers=self.headers,
json=payload,
)
task_id=generate_3d.json()["result"]
return task_id
def _get_task_id_(self,query):
try:
task_id=self._generate_3d_(query)
time.sleep(50)
get_asset= requests.get(
f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
headers=self.headers,
)
download_3d=get_asset.json()["model_urls"]["glb"]
return download_3d
except:
print("error on _get_task_id_")
def _download_3d(self,query):
try:
download_3d=self._get_task_id_(query)
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
except:
print("error on _download_3d")
def interface(self):
with gr.Blocks(css=".gradio-container {background-color: red}") as demo:
with gr.Row():
prompt=gr.Textbox("Prompt")
with gr.Row():
output=gr.Model3D()
with gr.Row():
button=gr.Button()
button.click(self._download_3d,prompt,output)
demo.launch()