-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from vicentefelipechile/master
Added support to Stability Model
- Loading branch information
Showing
7 changed files
with
96 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Third-party modules */ | ||
import FormData from 'form-data'; | ||
import axios from 'axios'; | ||
|
||
/* Local modules */ | ||
import { AIArguments, AIHandle, AIModel } from './BaseAiModel'; | ||
import { ENV } from '../baileys/env'; | ||
|
||
/* Stability Mode */ | ||
class StabilityModel extends AIModel<AIArguments, AIHandle> { | ||
public endPointGenerate: string = 'https://api.stability.ai/v2beta/stable-image/generate/'; | ||
public headers; | ||
|
||
constructor() { | ||
super(ENV.API_KEY_STABILITY, 'Stability'); | ||
|
||
this.endPointGenerate = this.endPointGenerate + ENV.STABILITY_MODEL; | ||
this.headers = { | ||
Authorization: `Bearer ${this.getApiKey()}`, | ||
Accept: 'image/*' | ||
}; | ||
} | ||
|
||
public async generateImage(prompt: string): Promise<Buffer> { | ||
const payload = { | ||
prompt: prompt, | ||
output_format: 'jpeg' | ||
}; | ||
|
||
const formData = new FormData(); | ||
|
||
const response = await axios.postForm(this.endPointGenerate, axios.toFormData(payload, formData), { | ||
validateStatus: undefined, | ||
responseType: 'arraybuffer', | ||
headers: this.headers | ||
}); | ||
|
||
if ( response.status === 200 ) { | ||
return Buffer.from(response.data); | ||
} else { | ||
throw new Error(response.data.errors[0]); | ||
} | ||
} | ||
|
||
async sendMessage({ prompt }: AIArguments, handle: AIHandle): Promise<any> { | ||
try { | ||
const imageData = await this.generateImage(prompt); | ||
|
||
await handle({ image: imageData }); | ||
} catch (err) { | ||
await handle('', err as string); | ||
} | ||
} | ||
} | ||
|
||
export { StabilityModel }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export type AIModels = 'ChatGPT' | 'Gemini' | 'FLUX'; | ||
export type AIModels = 'ChatGPT' | 'Gemini' | 'FLUX' | 'Stability'; | ||
export type AIModelsName = Exclude<AIModels, 'Custom'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters