File size: 2,901 Bytes
fe8a5d3 61b4ff5 fe8a5d3 61b4ff5 fe8a5d3 61b4ff5 fe8a5d3 61b4ff5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | ---
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` |
|