-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feat] 오디오 텍스트로 변환 및 요약 로직 구현
- Loading branch information
Showing
6 changed files
with
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export class CreateSummaryDto { | ||
ticleId: number; | ||
audioUrl: string; | ||
} |
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,4 +1,25 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'; | ||
|
||
import { CreateSummaryDto } from './dto/createSummaryDto'; | ||
import { StreamService } from './stream.service'; | ||
|
||
@Controller('stream') | ||
export class StreamController {} | ||
export class StreamController { | ||
constructor(private readonly streamService: StreamService) {} | ||
|
||
@Post('audio') | ||
async getAudioFileUrl(@Body() createSummaryDto: CreateSummaryDto) { | ||
const summary = await this.streamService.createSummary(createSummaryDto); | ||
|
||
const fulltext = await this.streamService.transcribeAudio(createSummaryDto.audioUrl); | ||
const summaryResult = await this.streamService.summaryAudio(fulltext); | ||
|
||
await this.streamService.updateSummaryText(summary, summaryResult); | ||
} | ||
|
||
@Get('summary/:ticleId') | ||
async getSummaryByTicleId(@Param('ticleId') ticleId: number) { | ||
const text = await this.streamService.getSummaryText(ticleId); | ||
return { summary: text }; | ||
} | ||
} |
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,8 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { Summary } from '@/entity/summary.entity'; | ||
import { Ticle } from '@/entity/ticle.entity'; | ||
|
||
import { StreamController } from './stream.controller'; | ||
import { StreamService } from './stream.service'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Ticle, Summary])], | ||
controllers: [StreamController], | ||
providers: [StreamService], | ||
}) | ||
export class StreamModule {} |
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,117 @@ | ||
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ErrorMessage } from '@repo/types'; | ||
|
||
import { Summary } from '@/entity/summary.entity'; | ||
import { Ticle } from '@/entity/ticle.entity'; | ||
|
||
import { CreateSummaryDto } from './dto/createSummaryDto'; | ||
|
||
@Injectable() | ||
export class StreamService { | ||
constructor( | ||
@InjectRepository(Summary) | ||
private summaryRepository: Repository<Summary>, | ||
@InjectRepository(Ticle) | ||
private ticleRepository: Repository<Ticle>, | ||
private configService: ConfigService | ||
) {} | ||
|
||
async createSummary(createSummaryDto: CreateSummaryDto) { | ||
const { ticleId, audioUrl } = createSummaryDto; | ||
const ticle = await this.ticleRepository.findOne({ | ||
where: { id: ticleId }, | ||
}); | ||
|
||
if (!ticle) { | ||
throw new NotFoundException(ErrorMessage.TICLE_NOT_FOUND); | ||
} | ||
|
||
const summary = this.summaryRepository.create({ | ||
audioUrl: audioUrl, | ||
ticle: ticle, | ||
}); | ||
return await this.summaryRepository.save(summary); | ||
} | ||
|
||
async transcribeAudio(remoteFileName: string) { | ||
const speechEndpoint = this.configService.get<string>('CLOVASPEECH_ENDPOINT'); | ||
|
||
const body = { | ||
dataKey: remoteFileName, | ||
params: { enable: true }, | ||
language: 'ko-KR', | ||
completion: 'sync', | ||
fulltext: true, | ||
}; | ||
|
||
try { | ||
const response = await fetch(speechEndpoint, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-CLOVASPEECH-API-KEY': this.configService.get<string>('CLOVASPEECH_API_KEY'), | ||
}, | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
const data = await response.json(); | ||
return data.text; | ||
} catch (error) { | ||
console.error('Failed to transcribe audio:', error); | ||
throw new InternalServerErrorException(ErrorMessage.FAILED_TO_TRANSCRIBE_AUDIO); | ||
} | ||
} | ||
|
||
async summaryAudio(text: string) { | ||
const studioEndpoint = this.configService.get<string>('CLOVASTUDIO_ENDPOINT'); | ||
|
||
const body = { | ||
texts: [text], | ||
segMinSize: 300, | ||
includeAiFilters: true, | ||
autoSentenceSplitter: true, | ||
segCount: -1, | ||
segMaxSize: 1000, | ||
}; | ||
|
||
try { | ||
const response = await fetch(studioEndpoint, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-NCP-CLOVASTUDIO-API-KEY': this.configService.get<string>('CLOVASTUDIO_API_KEY'), | ||
'X-NCP-APIGW-API-KEY': this.configService.get<string>('CLOVASTUDIO_APIGW_API_KEY'), | ||
}, | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
if (!response.ok) { | ||
const errorText = await response.text(); | ||
console.error('Error response:', errorText); | ||
throw new Error(`API error: ${response.status} - ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
return data.result.text; | ||
} catch (error) { | ||
console.error('Failed to summarize audio:', error); | ||
throw new InternalServerErrorException(ErrorMessage.FAILED_TO_SUMMARY_AUDIO); | ||
} | ||
} | ||
|
||
async getSummaryText(ticleId: number) { | ||
const summary = await this.summaryRepository.findOne({ | ||
where: { ticle: { id: ticleId } }, | ||
}); | ||
return summary.summaryText; | ||
} | ||
|
||
async updateSummaryText(summary: Summary, summaryText: string[]) { | ||
summary.summaryText = summaryText; | ||
await this.summaryRepository.save(summary); | ||
} | ||
} |
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