-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.ts
More file actions
137 lines (121 loc) Β· 4.35 KB
/
demo.ts
File metadata and controls
137 lines (121 loc) Β· 4.35 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// demo.ts - ModelsLab SDK TypeScript Demo
import "dotenv/config";
import {
Client,
Community,
Audio,
Video,
ImageEditing,
DeepFake,
ThreeD,
Realtime,
InteriorAPI,
Text2Image,
Text2Speech,
} from "./src/index";
async function runTypeScriptDemo(): Promise<void> {
console.log("π ModelsLab SDK TypeScript Demo Starting...\n");
// Initialize client
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error("β API_KEY not found in environment variables");
console.log("Please set your API key in the .env file");
return;
}
console.log("β
API Key loaded successfully");
console.log(
`π API Key: ${apiKey.substring(0, 8)}...${apiKey.substring(
apiKey.length - 8
)}\n`
);
// Initialize API clients with TypeScript types
const community = new Community(apiKey);
const audio = new Audio(apiKey);
const video = new Video(apiKey);
const imageEditing = new ImageEditing(apiKey);
const deepfake = new DeepFake(apiKey);
const threeD = new ThreeD(apiKey);
const realtime = new Realtime(apiKey);
const interior = new InteriorAPI(apiKey);
console.log("π― TypeScript SDK Features:");
console.log("β¨ Full type safety with TypeScript interfaces");
console.log("π IntelliSense support for all API parameters");
console.log("π‘οΈ Runtime validation with Zod schemas");
console.log("π Auto-completion for all API methods\n");
// Example 1: Text to Image with TypeScript types
console.log("π¨ Example 1: Text to Image with TypeScript");
try {
const textToImageRequest: Text2Image = {
key: apiKey,
model_id: "flux",
prompt:
"A majestic dragon flying over a medieval castle, fantasy art style",
width: 512,
height: 512,
samples: 1,
num_inference_steps: 25,
guidance_scale: 7.5,
safety_checker: "yes",
enhance_prompt: "yes",
seed: Math.floor(Math.random() * 1000000),
};
console.log("π€ Sending typed text-to-image request...");
const result = await community.textToImage(textToImageRequest);
console.log("π₯ Response received:", JSON.stringify(result, null, 2));
if (result.id) {
console.log("π Fetching result with retry logic...");
const fetchResult = await community.fetch(result.id);
console.log("β
Final result:", JSON.stringify(fetchResult, null, 2));
}
} catch (error) {
console.error(
"β Text to Image Error:",
error instanceof Error ? error.message : error
);
}
console.log("\n" + "=".repeat(50) + "\n");
// Example 2: Text to Speech with TypeScript types
console.log("π£οΈ Example 2: Text to Speech with TypeScript");
try {
const textToSpeechRequest: Text2Speech = {
key: apiKey,
prompt:
"Welcome to ModelsLab! This TypeScript SDK provides type-safe access to all our AI APIs.",
voice_id: "madison",
language: "american english",
};
console.log("π€ Sending typed text-to-speech request...");
const result = await audio.textToSpeech(textToSpeechRequest);
console.log("π₯ Response received:", JSON.stringify(result, null, 2));
if (result.id) {
console.log("π Fetching audio result...");
const fetchResult = await audio.fetch(result.id);
console.log("β
Final result:", JSON.stringify(fetchResult, null, 2));
}
} catch (error) {
console.error(
"β Text to Speech Error:",
error instanceof Error ? error.message : error
);
}
console.log("\n" + "=".repeat(50) + "\n");
// Example 3: Show TypeScript benefits
console.log("π‘ TypeScript Benefits Demonstration:");
console.log("1. β
Type checking prevents runtime errors");
console.log("2. π IDE autocomplete for all parameters");
console.log("3. π Built-in documentation through types");
console.log("4. π‘οΈ Schema validation with Zod");
console.log("5. π§ Refactoring safety");
console.log("\nπ TypeScript Demo completed!");
console.log("\nπ Next Steps:");
console.log("- Explore all available API methods with full type support");
console.log("- Use the schema types for request validation");
console.log(
"- Check out the generated .d.ts files for complete type definitions"
);
console.log(
"- Visit https://docs.modelslab.com for comprehensive API documentation"
);
}
// Run the TypeScript demo
runTypeScriptDemo().catch(console.error);