【山东大学软件学院创新项目实训】时空漫游 文本转3D模型

调用Meshy的接口,使用文本生成3D模型

接口

接口分3步调用:

生成粗模
细化
生成并获取链接

每一步都返回一个任务id,用于下一步任务使用

生成粗模

import requests

YOUR_API_KEY = '*********************'
payload = {
"mode": "preview",
"prompt": "a fox mask",
"art_style": "realistic",
"negative_prompt": "low quality, low resolution, low poly, ugly"
}
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}

response = requests.post(
"https://api.meshy.ai/v2/text-to-3d",
headers=headers,
json=payload,
)
response.raise_for_status()
print(response.json())

细化

import requests

YOUR_API_KEY = '**************'
payload = {
"mode": "refine",
"preview_task_id": "018ea235-c062-788e-bbec-a54f6291818d"
}
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}

response = requests.post(
"https://api.meshy.ai/v2/text-to-3d",
headers=headers,
json=payload,
)
response.raise_for_status()
print(response.json())

生成并获取链接

import requests

task_id = "018e9e6a-51c9-7cd7-9d17-b1494707ac45"
headers = {
"Authorization": f"Bearer {YOUR_API_KEY}"
}

response = requests.get(
f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
headers=headers,
)
response.raise_for_status()
print(response.json())

实现

问题

需要注意的是,第二步和第三步需要等待Meshy服务器执行完毕,如果远端服务器未完成任务,就区调接口的话:

  • 对refine过程来说,会报bad request错误
  • 对第三步来说,会产生一个空的链接

解决方案

采用轮询的思想,等待一段时间后重复请求;

但是要注意:

  • 后端和前端有请求超时的时间限制,而这个接口需要等待的时间较长(对于免费用户来说大概在5min左右),需要设置超时的时间
  • 设置最大请求次数,避免死循环

工作流程

由于三次请求过于复杂,不方便Unity(客户端)使用,采用后端进行整合代理的方式,简化了接口的调用

Client Server Meshy prompt preview preview task id refine refine task id generate 3D model link model url Client Server Meshy

代码

使用了Flask框架

service.py内容如下:

import requests
import time

YOUR_API_KEY = '**************'

def preview(prompt):
  payload = {
  "mode": "preview",
  "prompt": prompt,
  "art_style": "realistic",
  "negative_prompt": "low quality, low resolution, low poly, ugly"
  }
  headers = {
  "Authorization": f"Bearer {YOUR_API_KEY}"
  }
  response = requests.post(
  "https://api.meshy.ai/v2/text-to-3d",
  headers=headers,
  json=payload,
  )
  response.raise_for_status()
  preview_task_id = response.json()['result']
  return preview_task_id

def refine(preview_task_id):
  time.sleep(5)
  payload = {
  "mode": "refine",
  "preview_task_id": preview_task_id
  }
  headers = {
  "Authorization": f"Bearer {YOUR_API_KEY}"
  }
  response = requests.post(
  "https://api.meshy.ai/v2/text-to-3d",
  headers=headers,
  json=payload,
  )
  try:
    response.raise_for_status()
    return response.json()['result']
  except Exception:
    print('retry refine...')
    return refine(preview_task_id)

def getUrl(task_id):
  headers = {
  "Authorization": f"Bearer {YOUR_API_KEY}"
  }

  response = requests.get(
  f"https://api.meshy.ai/v2/text-to-3d/{task_id}",
  headers=headers,
  )
  response.raise_for_status()
  res = response.json()
  try:
    model_urls = res['model_urls']
    fbx = model_urls['fbx']
    return fbx
  except Exception:
    return 0

def tryGetUrl(task_id):
  time.sleep(30)
  fbx = getUrl(task_id)
  print('retry generate...')
  if fbx==0 or fbx=='':
    fbx = tryGetUrl(task_id)
  return fbx

server.py内容如下:

from flask import Flask, request
import service

app = Flask(__name__)
app.config["timeout"] = 1800

@app.route('/getModelUrl', methods=['POST'])
def sign():
  prompt = request.form.get('prompt')
  preview_task_id = service.preview(prompt)
  print(preview_task_id)
  task_id = service.refine(preview_task_id)
  print(task_id)
  url = service.tryGetUrl(task_id)
  return url
  
if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8087)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值