File size: 6,585 Bytes
3c0ddc9
 
 
 
a38dc37
3c0ddc9
 
 
 
aacf792
 
 
 
7ac6163
aacf792
 
 
a676b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aacf792
 
 
 
 
 
 
 
 
 
 
 
 
7ac6163
 
aacf792
 
 
 
 
 
 
 
7ac6163
aacf792
7ac6163
aacf792
 
 
6a28768
aacf792
 
 
 
 
 
 
6a28768
aacf792
6a28768
 
 
aacf792
 
 
 
 
 
 
 
 
 
 
 
 
 
7ac6163
 
 
 
4ec07a1
 
88ed3d7
 
4ec07a1
88ed3d7
7ac6163
4ec07a1
aacf792
4ec07a1
 
 
 
 
aacf792
 
4ec07a1
aacf792
 
4ec07a1
 
aacf792
4ec07a1
 
 
 
 
 
 
aacf792
 
 
 
 
 
 
 
 
 
 
 
a676b27
 
 
 
dfd803e
aacf792
 
 
 
 
c4db5be
 
 
eb80da4
 
 
dfd803e
 
4ec07a1
aacf792
 
 
 
 
 
 
 
 
 
 
 
 
 
7ac6163
 
aacf792
 
 
 
 
7ac6163
aacf792
 
7ac6163
 
aacf792
7ac6163
aacf792
 
 
 
 
 
 
 
7ac6163
aacf792
7ac6163
aacf792
c4db5be
aacf792
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
title: CrownCode Backend
emoji: 👑
colorFrom: yellow
colorTo: red
sdk: docker
pinned: false
---

# CrownCode Backend

YouTube-first backend service for AI music detection workflows.

---

## Hugging Face Spaces Deployment

### Adım 0 — Model Dosyalarını HF Hub'a Yükle (tek seferlik)

Model dosyaları `.gitignore`'da — Space'e gönderilmez. Bunun yerine container
başlangıcında `startup.py` onları `Rtur2003/auris-models` hub reposundan indirir.

```bash
# Lokal makinede bir kere çalıştır:
huggingface-cli login          # HF token ister (write izni gerekli)
python upload_models_to_hub.py # ~415 MB yükler (RF 47MB + wav2vec2 360MB dahil)
```

Upload sonrası `https://huggingface.co/Rtur2003/auris-models` adresinde
tüm model dosyaları görünmeli.

### Adım 1 — Space Oluştur

- [huggingface.co/new-space](https://huggingface.co/new-space) adresine git
- Space Name: `crowncode-backend`
- SDK: **Docker** (önemli!)
- Hardware: **CPU Basic (Free)**

### Adım 2 — Kodu Push Et

```bash
cd hf-crowncode-backend
git remote add space https://huggingface.co/spaces/Rtur2003/crowncode-backend
git push space main
```

Veya Space sayfasında "Files" > "Add file" ile `Dockerfile`, `requirements.txt`,
`startup.py`, `app/` klasörünü yükle.

### Adım 3 — HF_TOKEN Secret Ekle (isteğe bağlı)

Eğer `auris-models` reposu **private** ise:

- Space > Settings > Repository secrets
- `HF_TOKEN` = HuggingFace read token

### Adım 4 — Deploy

- "Commit changes" butonuna bas
- Build ~5-8 dk sürer (PyTorch + model download)
- `startup.py` tüm model dosyalarını `/app/models/` altına indirir
- API ayağa kalkar, "Önizleme Modu" biter

### URL Format
```
https://KULLANICI_ADI-crowncode-backend.hf.space
```

### Test Endpoints
```
GET  /api/health     -> {"status": "healthy"}
GET  /docs           -> Swagger UI
POST /api/youtube/analyze
```

---

## What This Service Does

- Accepts a YouTube URL
- Downloads audio via `yt-dlp`
- Optionally forwards the audio to external services:
  - Music-AIDetector (`/predict`)
  - Ses-Analizi (`/analyze`)
- Produces a deterministic preview decision if no model is available

---

## Structure

```
hf-crowncode-backend/
  Dockerfile          <- Hugging Face Spaces icin
  requirements.txt    <- CPU-compatible dependencies
  app/
    main.py
    schemas.py
    routes/
      health.py
      analyze.py
      data_processing.py
      commend/
        router.py
        youtube_service.py
    services/
      external_clients.py
      url_parser.py
      youtube_analysis.py
      youtube_downloader.py
      audio_processor.py
      validation.py
      logging_config.py
      preview_model.py
```

---

## API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/api/health` | Health check |
| POST | `/api/analyze` | Analyze YouTube URL or uploaded file |
| POST | `/api/process/audio` | Audio augmentation |
| GET | `/api/commend/health` | Crown Commend health check |
| POST | `/api/commend/generate` | Generate AI comment for YouTube |
| POST | `/api/commend/post` | Post comment to YouTube (feature-flagged) |
| GET | `/api/commend/styles` | Get available comment styles |

### POST /api/analyze

Request (multipart form):

- `sourceType`: `youtube` | `file` | `spotify` | `apple`
- `url`: YouTube URL (when sourceType=youtube)
- `file`: Audio file upload (when sourceType=file)

Response:

```json
{
  "result": {
    "isAIGenerated": true,
    "confidence": 0.85,
    "processingTime": 2.1,
    "modelVersion": "preview-v2-enhanced",
    "decisionSource": "preview",
    "analysisMode": "preview",
    "source": { "kind": "youtube", "videoId": "..." },
    "features": { ... },
    "audioInfo": { ... }
  },
  "warnings": [],
  "errors": []
}
```

---

## Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `AURIS_MODELS_REPO` | `Rtur2003/auris-models` | HuggingFace model repo to download artifacts from at startup |
| `MODELS_DIR` | `/app/models` | Local directory where model files are stored |
| `HF_TOKEN` | - | HuggingFace token (required if `auris-models` repo is private) |
| `SKIP_MODEL_DOWNLOAD` | `0` | Set to `1` to skip startup download (local dev with models already present) |
| `CROWNCODE_CORS_ORIGINS` | `http://localhost:3000` | Allowed CORS origins (comma-separated). **Do not use `*` in production.** |
| `MUSIC_AI_API_URL` | - | Music-AIDetector service URL |
| `SES_ANALIZI_API_URL` | - | Ses-Analizi service URL |
| `CROWNCODE_API_TIMEOUT_SEC` | `30` | External service timeout |
| `SES_ANALIZI_THRESHOLD` | `0.5` | Authenticity score threshold |
| `LOG_LEVEL` | `INFO` | Logging level |
| `YOUTUBE_COOKIES_FROM_BROWSER` | - | Optional `yt-dlp` browser cookie source, e.g. `edge` or `chrome:Default` |
| `YOUTUBE_COOKIES_FILE` | - | Optional Netscape `cookies.txt` path for YouTube-authenticated downloads |
| `YOUTUBE_COOKIES_BASE64` | - | Optional base64-encoded `cookies.txt` content for secret managers |
| `COMMEND_GEMINI_API_KEY` | - | Gemini API key for Crown Commend (also used as YouTube API Key fallback) |
| `COMMEND_YOUTUBE_API_KEY` | - | YouTube Data API key for read operations (optional if Gemini key is set) |
| `COMMEND_TOKEN_JSON` | - | YouTube OAuth token JSON for posting comments (optional) |
| `COMMEND_API_KEY` | - | API key for commend endpoint auth (**required in production**) |
| `COMMEND_REQUIRE_AUTH` | `true` | Fail-closed auth gate. Set to `false` only for local development. |
| `COMMEND_ENABLE_POSTING` | `false` | Enable YouTube comment posting (`true`/`false`) |

---

## Frontend Configuration

Backend deploy edildikten sonra frontend `.env` dosyasini guncelle:

```env
NEXT_PUBLIC_API_URL=https://kullaniciadi-crowncode-backend.hf.space
```

---

## Local Development

```bash
# Install dependencies
pip install -r requirements.txt

# Install PyTorch CPU
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

# Run server
uvicorn app.main:app --reload --port 8000
```

---

## Docker Local Build

```bash
docker build -t crowncode-backend .
docker run -p 7860:7860 crowncode-backend
```

---

## Notes

- `yt-dlp` requires network access and works best with `ffmpeg` installed
- If YouTube returns a bot-check/sign-in challenge, configure `YOUTUBE_COOKIES_FROM_BROWSER=edge` or pass a `cookies.txt` file via `YOUTUBE_COOKIES_FILE`
- When external services are not configured, returns preview decision
- Hugging Face free tier has 16GB RAM and 2 vCPU
- Build may take 5-10 minutes due to PyTorch installation