-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/implement generate button (#23)
* generate button backend
- Loading branch information
1 parent
e0668c6
commit 74cae68
Showing
10 changed files
with
270 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
make before_commit |
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,27 @@ | ||
import { Button, createFrames } from 'frames.js/next'; | ||
import { StampService } from '../../../service/stampService'; | ||
|
||
const frames = createFrames({ | ||
basePath: '/api/frames/createStamp', | ||
}); | ||
|
||
const handleRequest = frames(async (ctx) => { | ||
const template = | ||
'スタイルはキティちゃんのようにシンプルで愛らしいキャラクターのLINEスタンプを生成してください。ポーズはセリフに合わせて適切に変化させてください。セリフ:'; | ||
const service = new StampService(template); | ||
const image_url = await service.generateImageUrl(ctx.message?.inputText); | ||
|
||
return { | ||
image: image_url, | ||
imageOptions: { | ||
aspectRatio: '1:1', | ||
}, | ||
buttons: [ | ||
<Button action="link" target="https://google.com" key=""> | ||
{ctx.message?.inputText} | ||
</Button>, | ||
], | ||
}; | ||
}); | ||
|
||
export const POST = handleRequest; |
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,22 @@ | ||
import OpenAI from 'openai'; | ||
|
||
export class StampRepository { | ||
constructor(private openai = new OpenAI()) {} | ||
|
||
async generateImage(prompt: string): Promise<string> { | ||
try { | ||
const imageResponse = await this.openai.images.generate({ | ||
model: 'dall-e-3', | ||
prompt: prompt, | ||
n: 1, | ||
size: '1024x1024', | ||
}); | ||
const imageUrl = imageResponse.data[0]?.url || ''; | ||
|
||
return imageUrl; | ||
} catch (error) { | ||
console.error('Error generating image:', error); | ||
throw new Error('Image generation failed'); | ||
} | ||
} | ||
} |
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,19 @@ | ||
// stampService.js | ||
import { StampRepository } from '../repository/stampRepository'; | ||
|
||
export class StampService { | ||
private template: string; | ||
|
||
constructor( | ||
template: string, | ||
private stampRepository: StampRepository = new StampRepository() | ||
) { | ||
this.template = template; | ||
} | ||
|
||
async generateImageUrl(input: string): Promise<string> { | ||
const prompt = this.template + input; | ||
const imageUrl = await this.stampRepository.generateImage(prompt); | ||
return imageUrl; | ||
} | ||
} |
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,54 @@ | ||
// import OpenAI from 'openai'; | ||
// import { StampRepository } from '../app/repository/stampRepository'; | ||
|
||
// jest.mock('openai', () => { | ||
// return { | ||
// OpenAI: jest.fn().mockImplementation(() => { | ||
// return { | ||
// images: { | ||
// generate: jest.fn().mockResolvedValue({ | ||
// data: [ | ||
// { | ||
// url: 'mockImageUrl', | ||
// }, | ||
// ], | ||
// }), | ||
// }, | ||
// }; | ||
// }), | ||
// }; | ||
// }); | ||
|
||
// describe('StampRepository', () => { | ||
// let stampRepository: StampRepository; | ||
|
||
// beforeEach(() => { | ||
// stampRepository = new StampRepository(); | ||
// }); | ||
|
||
// it('should return an image URL when generateImage is called', async () => { | ||
// const prompt = 'test prompt'; | ||
// const imageUrl = await stampRepository.generateImage(prompt); | ||
|
||
// // 正しい画像URLが返されることを確認 | ||
// expect(imageUrl).toBe('mockImageUrl'); | ||
// // OpenAI.images.generateが適切な引数で呼び出されたことを確認 | ||
// expect(OpenAI.prototype.images.generate).toHaveBeenCalledWith({ | ||
// model: 'dall-e-3', | ||
// prompt: prompt, | ||
// n: 1, | ||
// size: '1024x1024', | ||
// }); | ||
// }); | ||
|
||
// it('should throw an error if the OpenAI API call fails', async () => { | ||
// // OpenAI.images.generate メソッドを失敗させるようにモックを設定 | ||
// OpenAI.prototype.images.generate.mockRejectedValue( | ||
// new Error('API call failed') | ||
// ); | ||
|
||
// await expect(stampRepository.generateImage('test prompt')).rejects.toThrow( | ||
// 'Image generation failed' | ||
// ); | ||
// }); | ||
// }); |
Empty file.
Oops, something went wrong.