Skip to content

Commit

Permalink
Merge pull request #187 from erictik/face-swap
Browse files Browse the repository at this point in the history
add face swap
  • Loading branch information
zcpua authored Jul 14, 2023
2 parents 36a6e65 + 04ad081 commit 974ea58
Show file tree
Hide file tree
Showing 18 changed files with 1,939 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Package
name: verson 4.3.x
env:
APPVERSION: 4.2.${{ github.run_number }}
APPVERSION: 4.3.${{ github.run_number }}
on:
workflow_dispatch:
push:
Expand Down
103 changes: 60 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# midjourney-api

Node.js client for the unofficial MidJourney api.
Major update New [niji bot](https://github.com/erictik/midjourney-api/blob/main/example/imagine-niji.ts) & [custom zoom](https://github.com/erictik/midjourney-api/blob/main/example/customzoom.ts) & [remix mode](https://github.com/erictik/midjourney-api/blob/main/example/variation-ws.ts)
Node.js client for the unofficial MidJourney api.

<div align="center">
<p>
<a href="https://discord.gg/GavuGHQbV4"><img src="https://img.shields.io/discord/1082500871478329374?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
<a href="https://www.npmjs.com/package/midjourney"><img src="https://img.shields.io/npm/v/midjourney.svg?maxAge=3600" alt="npm version" /></a>
</p>
</div>

## What's new

- [face swap](https://github.com/erictik/midjourney-api/blob/main/example/faceswap.ts)
- Major update New [niji bot](https://github.com/erictik/midjourney-api/blob/main/example/imagine-niji.ts) & [custom zoom](https://github.com/erictik/midjourney-api/blob/main/example/customzoom.ts) & [remix mode](https://github.com/erictik/midjourney-api/blob/main/example/variation-ws.ts)

## Install

```bash
Expand All @@ -21,40 +26,42 @@ yarn add midjourney

```typescript
import { Midjourney } from "midjourney";
const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
Ws:true,
});
await client.Connect();
const Imagine = await client.Imagine("A little pink elephant", (uri: string, progress:string) => {
console.log("Imagine", uri, "progress", progress);
});
console.log({ Imagine });

const Variation = await client.Variation({
index: 2,
msgId: <string>Imagine.id,
hash: <string>Imagine.hash,
flags: Imagine.flags,
loading: (uri: string, progress: string) => {
console.log("Variation.loading", uri, "progress", progress);
},
});
console.log({ Variation });
const Upscale = await client.Upscale({
index: 2,
msgId: <string>Variation.id,
hash: <string>Variation.hash,
flags: Variation.flags,
loading: (uri: string, progress: string) => {
console.log("Upscale.loading", uri, "progress", progress);
},
});
console.log({ Upscale });

const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
Ws: true,
});
await client.Connect();
const Imagine = await client.Imagine(
"A little pink elephant",
(uri: string, progress: string) => {
console.log("Imagine", uri, "progress", progress);
}
);
console.log({ Imagine });

const Variation = await client.Variation({
index: 2,
msgId: <string>Imagine.id,
hash: <string>Imagine.hash,
flags: Imagine.flags,
loading: (uri: string, progress: string) => {
console.log("Variation.loading", uri, "progress", progress);
},
});
console.log({ Variation });
const Upscale = await client.Upscale({
index: 2,
msgId: <string>Variation.id,
hash: <string>Variation.hash,
flags: Variation.flags,
loading: (uri: string, progress: string) => {
console.log("Upscale.loading", uri, "progress", progress);
},
});
console.log({ Upscale });
```

## Example
Expand All @@ -77,12 +84,12 @@ npm install
```

3. set the environment variables
- [How to get your Discord TOKEN:](https://www.androidauthority.com/get-discord-token-3149920/)
- [Create a server](https://discord.com/blog/starting-your-first-discord-server) and [Invite Midjourney Bot to Your Server](https://docs.midjourney.com/docs/invite-the-bot)
OR [Join my discord server](https://discord.com/invite/GavuGHQbV4)
- How to get server and channel ids:
when you click on a channel in your server in the browser expect to have the follow URL pattern `https://discord.com/channels/$SERVER_ID/$CHANNEL_ID`

- [How to get your Discord TOKEN:](https://www.androidauthority.com/get-discord-token-3149920/)
- [Create a server](https://discord.com/blog/starting-your-first-discord-server) and [Invite Midjourney Bot to Your Server](https://docs.midjourney.com/docs/invite-the-bot)
OR [Join my discord server](https://discord.com/invite/GavuGHQbV4)
- How to get server and channel ids:
when you click on a channel in your server in the browser expect to have the follow URL pattern `https://discord.com/channels/$SERVER_ID/$CHANNEL_ID`

```bash
#example variables, please set up yours
Expand All @@ -99,26 +106,36 @@ npx tsx example/imagine-ws.ts
```

## route-map

- [x] `/imagine` `variation` `upscale` `reroll` `blend` `zoomout` `vary`
- [x] `/info`
- [x] `/fast ` and `/relax `
- [x] [`/prefer remix`](https://github.com/erictik/midjourney-api/blob/main/example/prefer-remix.ts)
- [x] [`/prefer remix`](https://github.com/erictik/midjourney-api/blob/main/example/prefer-remix.ts)
- [x] [`variation (remix mode)`](https://github.com/erictik/midjourney-api/blob/main/example/variation-ws.ts)
- [x] `/describe`
- [x] `/describe`
- [x] [`/shorten`](https://github.com/erictik/midjourney-api/blob/main/example/shorten.ts)
- [x] `/settings` `reset`
- [x] verify human
- [x] [proxy](https://github.com/erictik/midjourney-discord/blob/main/examples/proxy.ts)
- [x] [niji bot](https://github.com/erictik/midjourney-api/blob/main/example/imagine-niji.ts)
- [x] [custom zoom](https://github.com/erictik/midjourney-api/blob/main/example/customzoom.ts)
- [x] autoload command payload

---

## Projects

- [midjourney-ui](https://github.com/erictik/midjourney-ui/)
- [midjourney-discord](https://github.com/erictik/midjourney-discord)-bot
- [phrame](https://github.com/jakowenko/phrame)
- [guapitu](https://www.guapitu.com/zh/draw?code=RRXQNF)

---

## Support

<a href='https://ko-fi.com/erictik' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee' /></a>

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=erictik/midjourney-api&type=Date)](https://star-history.com/#erictik/midjourney-api&Date)
33 changes: 33 additions & 0 deletions example/faceswap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import "dotenv/config";
import { Midjourney, detectBannedWords } from "../src";
/**
*
* a simple example of how to use faceSwap
* ```
* npx tsx example/faceswap.ts
* ```
*/
async function main() {
const source = `https://cdn.discordapp.com/attachments/1107965981839605792/1129362418775113789/3829c5d7-3e7e-473c-9c7b-b858e3ec97bc.jpeg`;
// const source = `https://cdn.discordapp.com/attachments/1108587422389899304/1129321826804306031/guapitu006_Cute_warrior_girl_in_the_style_of_Baten_Kaitos__111f39bc-329e-4fab-9af7-ee219fedf260.png`;
const target = `https://cdn.discordapp.com/attachments/1108587422389899304/1129321837042602016/guapitu006_a_girls_face_with_david_bowies_thunderbolt_71ee5899-bd45-4fc4-8c9d-92f19ddb0a03.png`;
const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
HuggingFaceToken: <string>process.env.HUGGINGFACE_TOKEN,
});

const info = await client.FaceSwap(target, source);
console.log(info);
}
main()
.then(() => {
console.log("finished");
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@huggingface/inference": "^2.5.0",
"async": "^3.2.4",
"isomorphic-ws": "^5.0.0",
"semiver": "^1.1.0",
"snowyflake": "^2.0.0",
"tslib": "^2.5.0",
"ws": "^8.13.0"
Expand Down
16 changes: 10 additions & 6 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MJOptions,
OnModal,
MJShorten,
MJDescribe,
} from "./interfaces";
import { MidjourneyApi } from "./midjourne.api";
import {
Expand Down Expand Up @@ -201,10 +202,16 @@ export class WsMessage {
this.emit("settings", message);
return;
case "describe":
this.emitMJ(id, {
// console.log("describe", "meseesage", message);
const describe: MJDescribe = {
id: id,
flags: message.flags,
descriptions: embeds?.[0]?.description.split("\n\n"),
uri: embeds?.[0]?.image?.url,
proxy_url: embeds?.[0]?.image?.proxy_url,
options: formatOptions(components),
});
};
this.emitMJ(id, describe);
break;
case "prefer remix":
if (content != "") {
Expand Down Expand Up @@ -606,10 +613,7 @@ export class WsMessage {
});
}
async waitDescribe(nonce: string) {
return new Promise<{
options: MJOptions[];
descriptions: string[];
} | null>((resolve) => {
return new Promise<MJDescribe | null>((resolve) => {
this.onceMJ(nonce, (message) => {
resolve(message);
});
Expand Down
24 changes: 24 additions & 0 deletions src/face.swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { client } from "./gradio/index";

export class faceSwap {
public hf_token?: string;
constructor(hf_token?: string) {
this.hf_token = hf_token;
}
async changeFace(Target: Blob, Source: Blob) {
const app = await client("https://felixrosberg-face-swap.hf.space/", {
hf_token: this.hf_token as any,
});
// console.log("app", app);
const result: any = await app.predict(1, [
Target, // blob in 'Target' Image component
Source, // blob in 'Source' Image component
0, // number (numeric value between 0 and 100) in 'Anonymization ratio (%)' Slider component
0, // number (numeric value between 0 and 100) in 'Adversarial defense ratio (%)' Slider component
"Compare", // string[] (array of strings) in 'Mode' Checkboxgroup component
]);
// result.data;
return result.data;
// console.log(result.data[0]);
}
}
Loading

0 comments on commit 974ea58

Please sign in to comment.