Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @youyeon11, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
์ด PR์ ๊ฐ๋ฐ ๋ธ๋์น์์ ๋ฉ์ธ ๋ธ๋์น๋ก์ ๋ณํฉ์ ํตํด AI ๊ธฐ๋ฐ ์์ฝ ์๋ณ ์๋น์ค์ ํต์ฌ ๊ธฐ๋ฅ์ ๋์ ํฉ๋๋ค. ์ด ์๋น์ค๋ FastAPI๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ถ๋์์ผ๋ฉฐ, Redis ์คํธ๋ฆผ์ ํตํด ๋น๋๊ธฐ ์์ ์ฒ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๊ณ , S3์์ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ํ๋ฉฐ, PyTorch ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์์ฝ์ ์๋ณํฉ๋๋ค. ์ด๋ ํ๋ก์ ํธ์ ์ํคํ ์ฒ๋ฅผ ํฌ๊ฒ ์ ๋ฐ์ดํธํ๊ณ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ์ค์ํ ๋ณ๊ฒฝ ์ฌํญ์ ๋๋ค.
Highlights
- AI ๊ธฐ๋ฐ ์์ฝ ์๋ณ ์๋น์ค ๋์ : FastAPI๋ฅผ ํ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ํตํ ์์ฝ ์๋ณ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
- ๋น๋๊ธฐ ์์ ์ฒ๋ฆฌ ์์คํ ๊ตฌ์ถ: Redis ์คํธ๋ฆผ์ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์์ ์ ๋น๋๊ธฐ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ , ์์ ํ ๋ฐ ๊ฒฐ๊ณผ ์คํธ๋ฆผ์ ํตํด ํจ์จ์ ์ธ ์ฒ๋ฆฌ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค.
- S3 ์ฐ๋์ ํตํ ์ด๋ฏธ์ง ์ฒ๋ฆฌ: Presigned URL์ ํตํด S3์์ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ํ๊ณ ์ฒ๋ฆฌํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ์ต๋๋ค.
- PyTorch ๋ชจ๋ธ ํตํฉ: LightCNN ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง์์ ์์ฝ์ ์์ธกํ๊ณ ์๋ณํ๋ ๋ก์ง์ ํฌํจํฉ๋๋ค.
- ํ๋ก์ ํธ ๊ตฌ์กฐ ๋ฐ ์ค์ ์ ๋ฐ์ดํธ: .github ์ด์ ํ ํ๋ฆฟ, .gitignore ํ์ผ, README.md๋ฅผ ์ ๋ฐ์ดํธํ๊ณ , .idea IDE ์ค์ ํ์ผ์ ์ ๊ฑฐํ๋ฉฐ, ์๋ก์ด app ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ๋ก ํ์ผ์ ์ฌ๊ตฌ์ฑํ์ต๋๋ค.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with ๐ and ๐ on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. โฉ
There was a problem hiding this comment.
Code Review
์ด PR์ ๊ธฐ์กด ํ๋ก์ ํธ ๊ตฌ์กฐ๋ฅผ FastAPI ๊ธฐ๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ์ผ๋ก ์ฌ๊ตฌ์ฑํ๊ณ , Redis Stream์ ์ด์ฉํ ๋น๋๊ธฐ ์์ ํ ์์คํ ์ ๋์ ํ๋ ๋ฑ ๋ง์ ๋ณ๊ฒฝ ์ฌํญ์ ํฌํจํ๊ณ ์์ต๋๋ค. ์ ๋ฐ์ ์ผ๋ก ์ข์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ถ๊ณ ์์ง๋ง, ๋ช ๊ฐ์ง ์ค์ํ ์์ ์ด ํ์ํด ๋ณด์ ๋๋ค. ํนํ ๋ชจ๋ธ ๋ก๋ฉ ์์ ๋ณด์ ์ทจ์ฝ์ , ์์ปค ๋ก์ง์ ๋ฒ๊ทธ, ์ค์ ๊ด๋ฆฌ ๋ฐฉ์ ๋ฑ์์ ๊ฐ์ ์ด ํ์ํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ ๊ฐ ํ์ผ์ ๋จ๊ธด ์ฝ๋ฉํธ๋ฅผ ์ฐธ๊ณ ํด ์ฃผ์ธ์.
| def _load_model(self, model_path: Path) -> LightCNN: | ||
| import __main__ | ||
| __main__.LightCNN = LightCNN | ||
| model = torch.load(model_path, map_location=self.device, weights_only=False) |
There was a problem hiding this comment.
torch.load ํจ์์ weights_only=False๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ์ฌ๊ฐํ ๋ณด์ ์ทจ์ฝ์ ์ ์ ๋ฐํ ์ ์์ต๋๋ค. ์ ๋ขฐํ ์ ์๋ ์์ค์ ๋ชจ๋ธ ํ์ผ์ ๋ก๋ํ ๊ฒฝ์ฐ ์์์ ์ฝ๋๊ฐ ์คํ๋ ์ ์์ต๋๋ค. ๋ชจ๋ธ์ ์ ์ฅํ ๋ torch.save(model.state_dict(), PATH)์ ๊ฐ์ด state_dict๋ง ์ ์ฅํ๊ณ , ๋ก๋ํ ๋๋ model = TheModelClass(*args, **kwargs)๋ก ๋ชจ๋ธ ์ธ์คํด์ค๋ฅผ ๋จผ์ ์์ฑํ ๋ค model.load_state_dict(torch.load(PATH))๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์์ ํ๊ณ ๊ถ์ฅ๋ฉ๋๋ค. ํ์ฌ์ __main__ ์์กด์ฑ ๋ฌธ์ ๋ ํจ๊ป ํด๊ฒฐํ ์ ์์ต๋๋ค.
|
|
||
| import asyncio | ||
| import redis.asyncio as redis | ||
| from app.core.config import Settings |
There was a problem hiding this comment.
| await process_image_scan(job, redis_client) | ||
| # ์ฒ๋ฆฌ ์ฑ๊ณต ์์๋ง ack | ||
| await redis_client.xack(settings.STREAM_JOB, settings.GROUP_NAME, msg_id) |
There was a problem hiding this comment.
run ๋ฉ์๋ ๋ด์์ redis_client ๋ณ์๊ฐ ์ ์๋์ง ์์์ต๋๋ค. ํด๋์ค ์ธ์คํด์ค์ redis ํด๋ผ์ด์ธํธ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ฏ๋ก self.redis_client๋ก ์์ ํด์ผ ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด NameError๊ฐ ๋ฐ์ํฉ๋๋ค.
| await process_image_scan(job, redis_client) | |
| # ์ฒ๋ฆฌ ์ฑ๊ณต ์์๋ง ack | |
| await redis_client.xack(settings.STREAM_JOB, settings.GROUP_NAME, msg_id) | |
| await process_image_scan(job, self.redis_client) | |
| # ์ฒ๋ฆฌ ์ฑ๊ณต ์์๋ง ack | |
| await self.redis_client.xack(settings.STREAM_JOB, settings.GROUP_NAME, msg_id) |
| REDIS_URL: str = os.environ.get("REDIS_SERVER_URL", "redis://localhost:6379") | ||
| S3_REGION: str = os.environ.get("S3_REGION", "ap-northeast-2") | ||
| S3_ENDPOINT_URL: str | None = os.environ.get("S3_ENDPOINT_URL") | ||
| BUCKET_NAME: str = os.environ.get("BUCKET_NAME") |
There was a problem hiding this comment.
BUCKET_NAME ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์ ๊ฒฝ์ฐ os.environ.get("BUCKET_NAME")์ None์ ๋ฐํํฉ๋๋ค. ํ์ง๋ง ํ์
์ str๋ก ์ง์ ๋์ด ์์ด None์ด ํ ๋น๋ ๊ฒฝ์ฐ Pydantic ์ ํจ์ฑ ๊ฒ์ฌ๋ ๋ฐํ์์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. BUCKET_NAME์ด ํ์ ์ค์ ์ด๋ผ๋ฉด os.environ.get์ ์ฌ์ฉํ์ง ์๊ณ BUCKET_NAME: str๋ก ์ ์ธํ์ฌ, ํ๊ฒฝ ๋ณ์๊ฐ ์์ ๋ Pydantic์ด ์์ ์์ ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋๋ก ํ๋ ๊ฒ์ด ๋ ์์ ํฉ๋๋ค.
| BUCKET_NAME: str = os.environ.get("BUCKET_NAME") | |
| BUCKET_NAME: str |
| import redis | ||
| from dotenv import load_dotenv | ||
| import os | ||
| from pydantic import BaseModel, Field | ||
| from typing import Any, Dict | ||
| from app.core.config import settings | ||
|
|
||
| load_dotenv() | ||
|
|
||
| redis_host = os.environ.get("REDIS_SERVER_HOST") | ||
|
|
||
| redis_client = redis.Redis( | ||
| host=redis_host, port=6379, encoding="UTF-8", decode_responses=True | ||
| ) | ||
|
|
||
| class PublishRequest(BaseModel): | ||
| stream: str = Field(default=settings.JOB_STREAM, description="Redis Stream Job name") | ||
| payload: Dict[str, Any] No newline at end of file |
There was a problem hiding this comment.
์ด ํ์ผ์ ์ฌ๋ฌ ๋ฌธ์ ๋ฅผ ๊ฐ์ง๊ณ ์์ด ๋ณด์ ๋๋ค.
- ์ ํ๋ฆฌ์ผ์ด์
์ ๋ค๋ฅธ ๋ถ๋ถ์์๋ ๋น๋๊ธฐ
redis.asyncio๋ฅผ ์ฌ์ฉํ๋๋ฐ, ์ฌ๊ธฐ์๋ ๋๊ธฐredis.Redisํด๋ผ์ด์ธํธ๋ฅผ ์์ฑํ๊ณ ์์ต๋๋ค. settings.JOB_STREAM์ ์ฌ์ฉํ๋ ค ํ์ง๋ง,app/core/config.py์๋STREAM_JOB์ผ๋ก ์ ์๋์ด ์์ดAttributeError๊ฐ ๋ฐ์ํฉ๋๋ค.- Redis ํธ์คํธ๋ฅผ
REDIS_SERVER_HOSTํ๊ฒฝ ๋ณ์์์ ๊ฐ์ ธ์ค๋๋ฐ,config.py์์๋REDIS_URL์ ์ฌ์ฉํ๋ฏ๋ก ์ค์ ์ด ์ผ๊ด๋์ง ์์ต๋๋ค.
์ด ํ์ผ์ด ํ์ฌ ํ๋ก์ ํธ์์ ์ฌ์ฉ๋์ง ์๋๋ค๋ฉด ํผ๋์ ํผํ๊ธฐ ์ํด ์ ๊ฑฐํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ๋ง์ฝ ์ฌ์ฉ๋๋ ํ์ผ์ด๋ผ๋ฉด ์ ๋ฌธ์ ๋ค์ ์์ ํด์ผ ํฉ๋๋ค.
| correlationId=correlationId, | ||
| presignedUrl=job_request.presigned_url, | ||
| replyQueue=settings.STREAM_RESULT, | ||
| callbackUrl=None, | ||
| contentType="image/jpeg", | ||
| createdAt=datetime.utcnow().isoformat(), | ||
| ttlSec=3600, | ||
| ) |
There was a problem hiding this comment.
| class ImageJob(BaseModel): | ||
| correlationId: str | ||
| presignedUrl: str | ||
| replyQueue: str | ||
| callbackUrl: str | None = None | ||
| contentType: str | ||
| createdAt: str | ||
| ttlSec: int |
There was a problem hiding this comment.
ImageJob ๋ชจ๋ธ์ ํ๋๋ช
(correlationId, presignedUrl ๋ฑ)์ด camelCase๋ก ์์ฑ๋์ด ์์ต๋๋ค. Python์์๋ PEP 8 ์คํ์ผ ๊ฐ์ด๋์ ๋ฐ๋ผ snake_case (correlation_id, presigned_url) ์ฌ์ฉ์ ๊ถ์ฅํฉ๋๋ค. ์ฝ๋ ์ ์ฒด์ ์ผ๊ด์ฑ์ ์ํด snake_case๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ ๊ณ ๋ คํด ๋ณด์ธ์. ์ด ๋ณ๊ฒฝ ์ฌํญ์ ์ ์ฉํ๋ฉด ์ด ๋ชจ๋ธ์ ์ฌ์ฉํ๋ ๋ค๋ฅธ ์ฝ๋๋ ํจ๊ป ์์ ํด์ผ ํฉ๋๋ค.
| class ImageJob(BaseModel): | |
| correlationId: str | |
| presignedUrl: str | |
| replyQueue: str | |
| callbackUrl: str | None = None | |
| contentType: str | |
| createdAt: str | |
| ttlSec: int | |
| class ImageJob(BaseModel): | |
| correlation_id: str | |
| presigned_url: str | |
| reply_queue: str | |
| callback_url: str | None = None | |
| content_type: str | |
| created_at: str | |
| ttl_sec: int |
|
|
||
|
|
||
| HERE = Path(__file__).resolve().parent.parent | ||
| MODEL_PATH = HERE / "models" / "models" / "best_model_0823.pt" |
There was a problem hiding this comment.
๋ชจ๋ธ ๊ฒฝ๋ก์ models๊ฐ ์ค๋ณต์ผ๋ก ํฌํจ๋์ด ์์ต๋๋ค (HERE / "models" / "models" / ...). ์ด๋ ์คํ์ผ ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ฉฐ, ์ค์ ํ๋ก์ ํธ ๊ตฌ์กฐ์ ์ผ์นํ๋์ง ํ์ธ์ด ํ์ํฉ๋๋ค. ์๋๋ ๊ฒ์ด ์๋๋ผ๋ฉด ๊ฒฝ๋ก๋ฅผ ์์ ํด์ผ ํฉ๋๋ค.
| MODEL_PATH = HERE / "models" / "models" / "best_model_0823.pt" | |
| MODEL_PATH = HERE / "models" / "best_model_0823.pt" |
| print(f"[task] Image scan finished for job_id={correlation_id}") | ||
|
|
||
| except Exception as e: | ||
| print(f"[task] Failed to process job_id={correlation_id}: {e}") |
There was a problem hiding this comment.
print ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ณ ์์ต๋๋ค. Python์ logging ๋ชจ๋์ ์ฌ์ฉํ๋ฉด ๋ก๊ทธ ๋ ๋ฒจ(DEBUG, INFO, ERROR ๋ฑ)์ ๋ฐ๋ฅธ ์ ์ด, ๋ก๊ทธ ํฌ๋งทํ
, ํ์ผ ๋๋ ์ธ๋ถ ์๋น์ค๋ก์ ๋ก๊ทธ ์ ์ก ๋ฑ ๋ ์ ์ฐํ๊ณ ๊ฐ๋ ฅํ ๋ก๊น
์ด ๊ฐ๋ฅํฉ๋๋ค. logging ๋ชจ๋์ ์ฌ์ฉํ๋๋ก ๋ฆฌํฉํ ๋งํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
| self.redis_client = redis_client | ||
|
|
||
| async def run(self): | ||
| print(f"[worker] start consumer={settings.CONSUMER_NAME} group={settings.GROUP_NAME} stream={settings.STREAM_JOB}") |
There was a problem hiding this comment.
print ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๊ทธ๋ฅผ ๋จ๊ธฐ๊ณ ์์ต๋๋ค. Python์ logging ๋ชจ๋์ ์ฌ์ฉํ๋ฉด ๋ก๊ทธ ๋ ๋ฒจ(DEBUG, INFO, ERROR ๋ฑ)์ ๋ฐ๋ฅธ ์ ์ด, ๋ก๊ทธ ํฌ๋งทํ
, ํ์ผ ๋๋ ์ธ๋ถ ์๋น์ค๋ก์ ๋ก๊ทธ ์ ์ก ๋ฑ ๋ ์ ์ฐํ๊ณ ๊ฐ๋ ฅํ ๋ก๊น
์ด ๊ฐ๋ฅํฉ๋๋ค. logging ๋ชจ๋์ ์ฌ์ฉํ๋๋ก ๋ฆฌํฉํ ๋งํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
๐ ์์ ๋ชฉ์