| --- |
| title: HuggingFace Calorie Counter |
| emoji: ๐ฑ |
| colorFrom: pink |
| colorTo: yellow |
| sdk: gradio |
| sdk_version: 5.9.1 |
| python_version: "3.11" |
| app_file: app.py |
| pinned: false |
| license: apache-2.0 |
| --- |
| |
| # 6์ฃผ์ฐจ: HuggingFace Space ์นผ๋ก๋ฆฌ ์นด์ดํฐ |
|
|
| ์์ ์ฌ์ง์ ์
๋ก๋ํ๋ฉด HuggingFace Inference API๋ก ์์์ ์ธ์ํ๊ณ , |
| LLM์ด 1์ธ๋ถ ๊ธฐ์ค ์นผ๋ก๋ฆฌ/ํ๋จ์ง๋ฅผ ์ถ์ ํ๋ Gradio ์ฑ์ด๋ค. |
| ์์ฑ๋ ์ฑ์ ๊ทธ๋๋ก **HuggingFace Space** ์ ์ฌ๋ ค ๊ณต๊ฐ URL์ ๋ฐ์ ์ ์๋ค. |
|
|
| > ์ด ์๋ฃ์ ๊ธฐ์ ์ ๋ณด๋ 2026-04 ๊ธฐ์ค์
๋๋ค. |
|
|
| ## ํ์ผ ๊ตฌ์กฐ |
|
|
| ``` |
| week06/ |
| โโโ app.py # Gradio ์ฑ ๋ณธ์ฒด (TODO 4๊ณณ์ ์ฑ์ ์์ฑ) |
| โโโ model_config.py # HF ๋ชจ๋ธ ์ด๋ฆ ์์ + InferenceClient ํฉํ ๋ฆฌ |
| โโโ requirements.txt # Space๊ฐ ์ค์นํ ์์กด์ฑ |
| โโโ .env.example # HF_TOKEN ํ
ํ๋ฆฟ |
| โโโ README.md # ์ด ํ์ผ (์์ YAML์ด Space ์ค์ ) |
| ``` |
|
|
| ## ์คํ ๋ฐฉ๋ฒ (๋ก์ปฌ) |
|
|
| ```bash |
| # 1) ํ ํฐ ๋ฐ๊ธ: https://huggingface.co/settings/tokens (Read ๊ถํ) |
| cp .env.example .env |
| # .env ํ์ผ์ ์ด์ด HF_TOKEN=hf_... ๋ก ์์ |
| |
| # 2) ์์กด์ฑ ์ค์น |
| uv pip install -r requirements.txt |
| |
| # 3) ์คํ |
| uv run python app.py |
| # ๋ธ๋ผ์ฐ์ : http://127.0.0.1:7860 |
| ``` |
|
|
| ## TODO (app.py ์) |
|
|
| 1. **`SYSTEM_PROMPT`** โ ์์์ฌ ์ญํ + JSON ์คํค๋ง ๊ฐ์ (ChatPromptTemplate ์์ ๋ค์ด๊ฐ๋ฏ๋ก ์ค๊ดํธ๋ `{{ }}`๋ก ์ด์ค์ผ์ดํ) |
| 2. **`_chain_lazy()` ์ LCEL ์ฒด์ธ ์กฐ๋ฆฝ** (4๋จ๊ณ) |
| - 2-1. `HuggingFaceEndpoint` ์์ฑ (`repo_id=LLM_MODEL`, `task="text-generation"`, ํ ํฐ) |
| - 2-2. `ChatHuggingFace(llm=endpoint)` ๋ก ๊ฐ์ธ๊ธฐ |
| - 2-3. `ChatPromptTemplate.from_messages([("system", SYSTEM_PROMPT), ("human", "...{labels_json}")])` |
| - 2-4. `_chain = prompt | llm | JsonOutputParser()` ํ์ดํ ์ฐ๊ฒฐ |
| 3. **`classify_food()`** โ `client.image_classification(tmp_path, model=VISION_MODEL)` ํธ์ถ |
| 4. **`estimate_calories()`** โ `chain.invoke({"labels_json": labels_json})` ํธ์ถ |
| |
| ๋ค ๊ณณ์ ์ฑ์ฐ๋ฉด ์ฑ์ด ๋์ํ๋ค. |
| |
| ## HuggingFace Space ๋ฐฐํฌ ๋ฐฉ๋ฒ |
| |
| 1. https://huggingface.co/new-space ์์ **Gradio** SDK๋ก Space ์์ฑ |
| 2. Space์ **Settings > Variables and secrets** ์ `HF_TOKEN` ๋ฑ๋ก |
| 3. ํ์ผ ์
๋ก๋ (์น ๋๋๊ทธ or `git push`) |
| ```bash |
| git clone https://huggingface.co/spaces/<๋ณธ์ธ>/<space-์ด๋ฆ> |
| cd <space-์ด๋ฆ> |
| cp ../week06/{app.py,model_config.py,requirements.txt,README.md} . |
| git add . |
| git commit -m "init" |
| git push |
| ``` |
| 4. ๋ช ๋ถ ํ `https://huggingface.co/spaces/<๋ณธ์ธ>/<space-์ด๋ฆ>` ์ ์ํ์ฌ ๋์ ํ์ธ |
| |
| ## ๊ณผ์ |
| |
| - [ ] Space URL ์ ์ถ |
| - [ ] title/emoji/ํ๋กฌํํธ ์ค ํ๋ ์ด์ ์ปค์คํฐ๋ง์ด์ฆ |
| - [ ] ๊ฒฐ๊ณผ ์คํฌ๋ฆฐ์ท 1์ฅ + 2~3์ค ์ค๋ช
|
| |
| ## ์ฌ์ฉ ๋ชจ๋ธ |
| |
| | ์ญํ | ๋ชจ๋ธ | |
| |------|------| |
| | ์ด๋ฏธ์ง ๋ถ๋ฅ | `nateraw/food` | |
| | ์นผ๋ก๋ฆฌ ์ถ์ LLM | `meta-llama/Meta-Llama-3-8B-Instruct` | |
| |