Stability-AI / rest-api-support

Stability REST API examples, issues, and discussions | https://api.stability.ai
108 stars 21 forks source link

flutter stability integration #36

Open zer01e opened 1 year ago

zer01e commented 1 year ago

Future generateImage(String prompt) async { const String STABILITY_API_KEY = stabilityApiKey; var url = Uri.parse("https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image");

var requestBody = { "text_prompts": [prompt], "cfg_scale": 7, "clip_guidance_preset": "FAST_BLUE", "height": 512, "width": 512, "sampler": "K_DPM_2_ANCESTRAL", "samples": 1, "steps": 75, };

final response = await http.post( url, headers: { "Content-Type": "application/json", "Accept": "application/json", "Authorization": "Bearer $STABILITY_API_KEY", }, body: json.encode(requestBody), );

print('Request Headers: ${response.request?.headers}'); print('Request Body: ${json.encode(requestBody)}'); print('Response Headers: ${response.headers}'); print('Response Body: ${response.body}');

if (response.statusCode == 200) { Map<String, dynamic> newResponse = jsonDecode(response.body); print('API Response: $newResponse'); // Add this line for debugging return newResponse['artifacts'][0]['base64']; } else { print('API Error: ${response.body}'); // Add this line for debugging throw Exception('Failed to generate image. Status code: ${response.statusCode}'); } }

notes: header defined as per documentation, and i am to generate image using documentation code, but flutter integration is causing this problem

ERROR: I/flutter (15596): Request Headers: {Content-Type: application/json; charset=utf-8, Accept: application/json, Authorization: Bearer xxxxxxxxx} I/flutter (15596): Request Body: {"text_prompts":["lion"],"cfg_scale":7,"clip_guidance_preset":"FAST_BLUE","height":512,"width":512,"sampler":"K_DPM_2_ANCESTRAL","samples":1,"steps":75} I/flutter (15596): Response Headers: {connection: keep-alive, alt-svc: h3=":443"; ma=86400, set-cookie: __cf_bm=oz29NnronGgO8HHIKZOwJ3Iw.ekiLjtwLy14qsXfbqs-1689341665-0-Aee7yecOodZd/X9qrtGNWmskYxlp9DKHVrsL4LAQr4CiowE4v009jFkJnQxvS3PZvfMfqGwrHuoF0gCnNSTgsLs=; path=/; expires=Fri, 14-Jul-23 14:04:25 GMT; domain=.stability.ai; HttpOnly; Secure; SameSite=None, date: Fri, 14 Jul 2023 13:34:25 GMT, x-envoy-upstream-service-time: 1, vary: Origin, content-length: 114, cf-ray: 7e6a24a2ffe64740-DFW, cf-cache-status: DYNAMIC, content-type: application/json; charset=UTF-8, server: cloudflare} I/flutter (15596): Response Body: {"id":"b975a1e22c75a8d2b6edf48f4a43956c","message":"content-type: must be application/json","name":"bad_request"} I/flutter (15596): API Error: {"id":"b975a1e22c75a8d2b6edf48f4a43956c","message":"content-type: must be application/json","name":"bad_request"} I/flutter (15596): Failed to generate image: Exception: Failed to generate image. Status code: 400

advviniciusgyn commented 1 year ago

I think that to make it work you must to Encode Body as UTF-8 Bytes