Skip to content

Commit

Permalink
Merge pull request #11 from snuhcs-course/ml-initialization
Browse files Browse the repository at this point in the history
Ml initialization
  • Loading branch information
mechanicjo authored Nov 7, 2023
2 parents 682c4e9 + d0cb999 commit e083b1d
Show file tree
Hide file tree
Showing 25 changed files with 1,055 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish-backend-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
username: eldpswp99
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build image
Expand All @@ -51,4 +51,4 @@ jobs:
username: eldpswp99
password: ${{ secrets.DOCKER_TOKEN }}
- name: Docker compose up
run: SHA=${GITHUB_SHA} docker compose -f ~/docker-compose.yml up -d fooriend-backend
run: SHA=${GITHUB_SHA} docker compose -f ~/docker-compose.yml up -d
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ name: 테스트

on:
push:
branches: [ "main" ]
branches:
- "main"
pull_request:
branches: [ "main" ]
branches:
- "main"

jobs:
test:
Expand Down
41 changes: 36 additions & 5 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/bcryptjs": "^2.4.5",
"@types/jsonwebtoken": "^9.0.3",
"@types/multer": "^1.4.8",
"axios": "^1.6.0",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
3 changes: 3 additions & 0 deletions backend/src/review/dtos/in-dtos/restaurant.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export class RestaurantDto {
@IsString()
googleMapPlaceId: string;

@IsString()
name: string;

@IsNumber()
latitude: number;

Expand Down
10 changes: 9 additions & 1 deletion backend/src/review/dtos/out-dtos/restaurant.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ export class RestaurantDto {
private googleMapPlaceId: string;
private longitude: number;
private latitude: number;
private name: string;

constructor({ id, googleMapPlaceId, longitude, latitude }: RestaurantEntity) {
constructor({
id,
googleMapPlaceId,
longitude,
latitude,
name,
}: RestaurantEntity) {
this.id = id;
this.googleMapPlaceId = googleMapPlaceId;
this.longitude = longitude;
this.latitude = latitude;
this.name = name;
}
}
19 changes: 19 additions & 0 deletions backend/src/review/ml-remote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from 'axios';

export async function getReceiptOcr(imageUrl: string) {
return (
await axios.post(`${process.env.ML_URL}ocr`, {
image_url: imageUrl,
})
).data;
}

export async function getReviewIsPositive(review: string) {
return (
(
await axios.post(`${process.env.ML_URL}review`, {
review,
})
).data['result'] === '긍정'
);
}
3 changes: 3 additions & 0 deletions backend/src/review/models/restaurant.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class RestaurantEntity extends IssuedAtMetaEntity {
@Column({ type: 'float8' })
longitude: number;

@Column({ type: 'varchar', default: '' })
name: string;

@OneToMany(() => ReviewEntity, (review) => review.restaurant)
reviews: ReviewEntity[];
}
6 changes: 6 additions & 0 deletions backend/src/review/models/review.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export class ReviewEntity extends IssuedAtMetaEntity {
@Column({ type: 'varchar' })
content: string;

@Column({ type: 'boolean', default: false })
isPositive: boolean;

@Column('varchar', { array: true, default: [] })
menu: string[];

get receiptImage() {
return this.images.find((image) => image.isReceipt);
}
Expand Down
3 changes: 2 additions & 1 deletion backend/src/review/repositories/restaurant.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RestaurantDto } from '../dtos/in-dtos/restaurant.dto';
@CustomRepository(RestaurantEntity)
export class RestaurantRepository extends Repository<RestaurantEntity> {
async findOrCreate(data: RestaurantDto) {
const { googleMapPlaceId, longitude, latitude } = data;
const { googleMapPlaceId, longitude, latitude, name } = data;
const restaurant = await this.findOne({
where: { googleMapPlaceId },
});
Expand All @@ -17,6 +17,7 @@ export class RestaurantRepository extends Repository<RestaurantEntity> {
googleMapPlaceId,
longitude,
latitude,
name,
}).save();
}
}
19 changes: 18 additions & 1 deletion backend/src/review/review.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { UserEntity } from '../user/models/user.entity';
import { CreateReviewDto } from './dtos/in-dtos/createReview.dto';
import { RestaurantRepository } from './repositories/restaurant.repository';
Expand All @@ -7,6 +7,7 @@ import { In } from 'typeorm';
import { ReviewEntity } from './models/review.entity';
import { ReviewAdjacentQueryDto } from './dtos/in-dtos/review-adjacent-query.dto';
import { getDistance } from 'geolib';
import { getReceiptOcr, getReviewIsPositive } from './ml-remote';
import { RestaurantEntity } from './models/restaurant.entity';

@Injectable()
Expand All @@ -29,15 +30,31 @@ export class ReviewService {
id: In(imageIds.concat([receiptImageId ?? -1])),
});
const receiptImage = images.find((image) => image.id === receiptImageId);
let menu = [];
if (receiptImage) {
await receiptImage.markAsReceipt();
try {
const receiptData = await getReceiptOcr(receiptImage.url);
menu = receiptData['menu'];
} catch (e) {
throw new BadRequestException('잘못된 영수증입니다.');
}
}

let isPositive = false;
try {
isPositive = await getReviewIsPositive(content);
} catch (e) {
throw new BadRequestException('리뷰 분석 중 오류가 발생했습니다.');
}

return await ReviewEntity.create({
content,
user,
restaurant,
images,
isPositive,
menu,
}).save();
}

Expand Down
Loading

0 comments on commit e083b1d

Please sign in to comment.