41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import dotenv from 'dotenv';
 | 
						|
import util from 'node:util';
 | 
						|
 | 
						|
dotenv.config({ path: '.env' });
 | 
						|
 | 
						|
import { QwenText2Image } from '../src/dashscope/dashscope.ts';
 | 
						|
 | 
						|
const main = async () => {
 | 
						|
  // 1. create image
 | 
						|
  const qwen = new QwenText2Image(process.env.BAILIAN_API_KEY || '');
 | 
						|
  const res = await qwen.createImage({
 | 
						|
    model: 'wan2.2-t2i-flash',
 | 
						|
    input: {
 | 
						|
      prompt: '一个可爱的小的熊猫,卡通风格,抱着竹子。',
 | 
						|
    },
 | 
						|
  });
 | 
						|
  console.log('create image response:', res);
 | 
						|
  // 2. for-of loop get status
 | 
						|
};
 | 
						|
 | 
						|
const id = '857ac37e-d470-4008-a98b-afe8ad054f81';
 | 
						|
const loopGetStatus = async (taskId: string) => {
 | 
						|
  const qwen = new QwenText2Image(process.env.BAILIAN_API_KEY || '');
 | 
						|
  let status: string;
 | 
						|
  do {
 | 
						|
    await qwen.getTaskStatus(taskId);
 | 
						|
    const taskResponse = qwen.taskResponse;
 | 
						|
    console.log('task status:', taskResponse);
 | 
						|
    status = taskResponse?.output?.task_status;
 | 
						|
    if (status === 'SUCCEEDED') {
 | 
						|
      break;
 | 
						|
    }
 | 
						|
    await new Promise((resolve) => setTimeout(resolve, 1000));
 | 
						|
  } while (status !== 'SUCCEEDED' && status !== 'FAILED');
 | 
						|
 | 
						|
  console.log(util.inspect(qwen.taskResponse, { depth: null, colors: true }));
 | 
						|
};
 | 
						|
 | 
						|
// main()
 | 
						|
loopGetStatus(id);
 |