Flutter Midjourney客户端插件midjourney_client的使用

发布于 1周前 作者 sinazl 最后一次编辑是 5天前 来自 Flutter

Flutter Midjourney客户端插件midjourney_client的使用

这是一个非官方的Midjourney客户端,它通过Discord账户令牌与真正的Midjourney机器人进行交互。截至目前,其稳定性尚未经过全面测试。因此,建议不要在生产环境中使用此客户端。

This is an unofficial client for Midjourney that interfaces with the authentic Midjourney Bot via a Discord account token. As of now, it’s stability has not been thoroughly tested. Consequently, it is advised against utilizing this client in production environments.

Install

For a flutter project, consider running this command:

flutter pub add midjourney_client

For a dart project, consider running this command:

dart pub add midjourney_client

This installs the midjourney_client library and its dependencies.

Usage

import 'dart:async';

import 'package:midjourney_client/midjourney_client.dart' as midjourney_client;

Future<void> main(List<Object> arguments) async {
  final client = midjourney_client.Midjourney();

  await client.initialize(
    channelId: Env.channelId,
    serverId: Env.serverId,
    token: Env.token,
  );

  final imagine = client.imagine('Elephant on a tree')..listen(print);

  final result = await imagine.last;

  final upscaled = client.upscale(result, 1)..listen(print);
  final uResult = await upscaled.last;

  print(uResult);
}

Set up

Pre-requisites:

How to get server id & channel id

  1. Open Discord app
  2. Open your server
  3. Right click on the message inside the channel you want to use
  4. Copy link to message, this should look like https://discord.com/channels/${SERVER_ID}/${CHANNEL_ID}/${MESSAGE_ID}
  5. Extract SERVER_ID and CHANNEL_ID from the link

How to get token

This one is a bit tricky, but here’s how you can get it:

  1. Login to discord web app
  2. Open developer tools, head for Network tab
  3. Send a message to the channel you want to use or reload the page
  4. Click on a random request and go to request headers
  5. Find Authorization header and extract the value, it is your token

Examples

These examples will instantiate a websocket connection to Discord Server and act as a Discord client sending messages to the channel specified by the CHANNEL_ID environment variable. The SERVER_ID environment variable is used to identify the server to which the channel belongs. The TOKEN environment variable is used to authenticate the client.

Imagine

This example will trigger /imagine command on the Midjourney Bot and print the result.

dart run --define=SERVER_ID="" --define=CHANNEL_ID="" --define=TOKEN="" example/imagine.dart
import 'dart:async';
import 'dart:io';

import 'package:midjourney_client/midjourney_client.dart' as midjourney_client;

import 'env.dart';

Future<void> main(List<Object> arguments) async {
  final client = midjourney_client.Midjourney();

  await client.initialize(
    channelId: Env.channelId,
    serverId: Env.serverId,
    token: Env.token,
  );

  final imagine = client.imagine('Cat in a hat')..listen(print);

  final result = await imagine.last;

  print('Result: $result');
  exit(0);
}

Variation

This example will trigger /imagine command on the Midjourney Bot, wait and trigger first variation.

dart run --define=SERVER_ID="" --define=CHANNEL_ID="" --define=TOKEN="" example/variations.dart
import 'dart:async';
import 'dart:io';

import 'package:midjourney_client/midjourney_client.dart' as midjourney_client;

import 'env.dart';

Future<void> main(List<Object> arguments) async {
  final client = midjourney_client.Midjourney();

  await client.initialize(
    channelId: Env.channelId,
    serverId: Env.serverId,
    token: Env.token,
  );

  final imagine = client.imagine('Cat with sword')..listen(print);

  final result = await imagine.last;

  final variation = client.variation(result, 1)..listen(print);

  final vResult = await variation.last;

  print(vResult);
  exit(0);
}

Upscale

This example will trigger /imagine command on the Midjourney Bot, wait and trigger first upscale.

dart run --define=SERVER_ID="" --define=CHANNEL_ID="" --define=TOKEN="" example/upscale.dart
import 'dart:async';
import 'dart:io';

import 'package:midjourney_client/midjourney_client.dart' as midjourney_client;

import 'env.dart';

Future<void> main(List<Object> arguments) async {
  final client = midjourney_client.Midjourney();

  await client.initialize(
    channelId: Env.channelId,
    serverId: Env.serverId,
    token: Env.token,
  );

  final imagine = client.imagine('Cat with a sword')..listen(print);

  final result = await imagine.last;

  final upscaled = client.upscale(result, 1)..listen(print);

  final uResult = await upscaled.last;

  print('Result: $uResult');
  exit(0);
}

更多关于Flutter Midjourney客户端插件midjourney_client的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter Midjourney客户端插件midjourney_client的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用midjourney_client插件的示例代码。这个插件假设是用来与Midjourney API进行交互的(请注意,由于我无法直接访问外部库或实时API文档,以下代码是基于假设和通用实践编写的,实际使用时可能需要根据具体插件的API文档进行调整)。

首先,确保你已经在pubspec.yaml文件中添加了midjourney_client依赖:

dependencies:
  flutter:
    sdk: flutter
  midjourney_client: ^x.y.z  # 替换为实际的版本号

然后运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter项目中这样使用midjourney_client插件:

import 'package:flutter/material.dart';
import 'package:midjourney_client/midjourney_client.dart';  // 假设插件提供了这个导入路径

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Midjourney Client Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MidjourneyDemoPage(),
    );
  }
}

class MidjourneyDemoPage extends StatefulWidget {
  @override
  _MidjourneyDemoPageState createState() => _MidjourneyDemoPageState();
}

class _MidjourneyDemoPageState extends State<MidjourneyDemoPage> {
  String? responseText;
  late MidjourneyClient midjourneyClient;

  @override
  void initState() {
    super.initState();
    // 初始化midjourneyClient,这里假设需要API密钥
    String apiKey = 'your_api_key_here';  // 替换为你的实际API密钥
    midjourneyClient = MidjourneyClient(apiKey: apiKey);
  }

  Future<void> fetchData() async {
    try {
      // 假设这是调用Midjourney API的方法
      var response = await midjourneyClient.createJourney(
        prompt: 'A beautiful landscape with mountains and a river',
        negativePrompt: 'Any people or buildings',
        width: 512,
        height: 512,
      );

      // 处理响应,这里假设response是一个包含图像URL的JSON对象
      setState(() {
        responseText = response['image_url'];  // 假设响应中包含一个image_url字段
      });
    } catch (e) {
      setState(() {
        responseText = 'Error: ${e.message}';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Midjourney Client Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: fetchData,
              child: Text('Generate Image'),
            ),
            if (responseText != null)
              Image.network(responseText!),
            if (responseText != null && responseText!.startsWith('Error'))
              Text(responseText!),
          ],
        ),
      ),
    );
  }
}

在这个示例中:

  1. 我们导入了midjourney_client包。
  2. _MidjourneyDemoPageState中,我们初始化了MidjourneyClient实例,并传入了一个API密钥(这通常是必须的,用于身份验证)。
  3. 我们定义了一个fetchData方法,该方法调用midjourneyClientcreateJourney方法来生成图像,并处理响应。
  4. 在UI中,我们有一个按钮来触发fetchData方法,并在成功获取图像URL后显示该图像。如果发生错误,则显示错误信息。

请注意,以上代码是基于假设的API和方法调用。实际使用时,你需要查阅midjourney_client插件的官方文档来了解正确的API调用方式和参数。特别是,API密钥的管理、错误处理、响应格式等都可能有所不同。

回到顶部