|
| 1 | +# Diffio Python SDK |
| 2 | + |
| 3 | +The Diffio Python SDK helps you call the Diffio API from Python. This version covers project creation, upload, generation, progress checks, and download URLs. |
| 4 | +Requires Python 3.8 or later. |
| 5 | + |
| 6 | +## Install |
| 7 | + |
| 8 | +```bash |
| 9 | +pip install diffio |
| 10 | +``` |
| 11 | + |
| 12 | +For local development: |
| 13 | + |
| 14 | +```bash |
| 15 | +cd diffio-python |
| 16 | +pip install -e . |
| 17 | +``` |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +Set the API key with `DIFFIO_API_KEY`. You can also override the base URL with `DIFFIO_API_BASE_URL`. |
| 22 | + |
| 23 | +```bash |
| 24 | +export DIFFIO_API_KEY="diffio_live_..." |
| 25 | +export DIFFIO_API_BASE_URL="https://us-central1-diffioai.cloudfunctions.net" |
| 26 | +``` |
| 27 | + |
| 28 | +For emulators, set the base URL to the Functions emulator host. |
| 29 | + |
| 30 | +```bash |
| 31 | +export DIFFIO_API_BASE_URL="http://127.0.0.1:5001/diffioai/us-central1" |
| 32 | +``` |
| 33 | + |
| 34 | +## Request options |
| 35 | + |
| 36 | +Use request options to override headers, timeouts, retries, or the API key per request. |
| 37 | +You can also pass `timeoutInSeconds` as an alias for `timeout`. |
| 38 | + |
| 39 | +```py |
| 40 | +from diffio import DiffioClient, RequestOptions |
| 41 | + |
| 42 | +client = DiffioClient(apiKey="diffio_live_...") |
| 43 | +projects = client.list_projects( |
| 44 | + requestOptions=RequestOptions( |
| 45 | + headers={"X-Debug": "1"}, |
| 46 | + timeout=30.0, |
| 47 | + maxRetries=2, |
| 48 | + retryBackoff=0.5, |
| 49 | + ) |
| 50 | +) |
| 51 | +``` |
| 52 | + |
| 53 | +## Create a project and generation |
| 54 | + |
| 55 | +`create_project` uploads the file and returns the project metadata. |
| 56 | + |
| 57 | +```py |
| 58 | +from diffio import DiffioClient |
| 59 | + |
| 60 | +client = DiffioClient(apiKey="diffio_live_...") |
| 61 | +file_path = "sample.wav" |
| 62 | +project = client.create_project( |
| 63 | + filePath=file_path, |
| 64 | +) |
| 65 | + |
| 66 | +generation = client.create_generation( |
| 67 | + apiProjectId=project.apiProjectId, |
| 68 | + model="diffio-2", |
| 69 | + sampling={"steps": 12, "guidance": 1.5}, |
| 70 | +) |
| 71 | + |
| 72 | +print(generation.generationId) |
| 73 | +``` |
| 74 | + |
| 75 | +## Audio isolation helper |
| 76 | + |
| 77 | +```py |
| 78 | +from diffio import DiffioClient |
| 79 | + |
| 80 | +client = DiffioClient(apiKey="diffio_live_...") |
| 81 | +result = client.audio_isolation.isolate( |
| 82 | + filePath="sample.wav", |
| 83 | + model="diffio-2", |
| 84 | + sampling={"steps": 12, "guidance": 1.5}, |
| 85 | +) |
| 86 | + |
| 87 | +print(result.generation.generationId) |
| 88 | +``` |
| 89 | + |
| 90 | +## Restore audio in one call |
| 91 | + |
| 92 | +This helper runs the full flow and returns the downloaded bytes plus a metadata dict. |
| 93 | + |
| 94 | +```py |
| 95 | +from diffio import DiffioClient |
| 96 | + |
| 97 | +client = DiffioClient(apiKey="diffio_live_...") |
| 98 | +audio_bytes, info = client.restore_audio( |
| 99 | + filePath="sample.wav", |
| 100 | + model="diffio-2", |
| 101 | + sampling={"steps": 12, "guidance": 1.5}, |
| 102 | + onProgress=lambda progress: print(progress.status), |
| 103 | +) |
| 104 | + |
| 105 | +if info["error"]: |
| 106 | + print(info["error"]) |
| 107 | +else: |
| 108 | + with open("restored.mp3", "wb") as handle: |
| 109 | + handle.write(audio_bytes) |
| 110 | + |
| 111 | +print(info["apiProjectId"], info["generationId"]) |
| 112 | +``` |
| 113 | + |
| 114 | +## Generation progress |
| 115 | + |
| 116 | +```py |
| 117 | +from diffio import DiffioClient |
| 118 | + |
| 119 | +client = DiffioClient(apiKey="diffio_live_...") |
| 120 | +progress = client.generations.get_progress( |
| 121 | + generationId="gen_123", |
| 122 | + apiProjectId="proj_123", |
| 123 | +) |
| 124 | + |
| 125 | +print(progress.status) |
| 126 | +``` |
| 127 | + |
| 128 | +## Generation download |
| 129 | + |
| 130 | +```py |
| 131 | +from diffio import DiffioClient |
| 132 | + |
| 133 | +client = DiffioClient(apiKey="diffio_live_...") |
| 134 | +download = client.generations.download( |
| 135 | + generationId="gen_123", |
| 136 | + apiProjectId="proj_123", |
| 137 | + downloadType="mp3", |
| 138 | + downloadFilePath="restored.mp3", |
| 139 | +) |
| 140 | + |
| 141 | +print(download.downloadUrl) |
| 142 | +``` |
| 143 | + |
| 144 | +If you only need the URL, use `client.generations.get_download`. |
| 145 | + |
| 146 | +## List projects |
| 147 | + |
| 148 | +```py |
| 149 | +from diffio import DiffioClient |
| 150 | + |
| 151 | +client = DiffioClient(apiKey="diffio_live_...") |
| 152 | +projects = client.projects.list() |
| 153 | + |
| 154 | +for project in projects.projects: |
| 155 | + print(project.apiProjectId, project.status) |
| 156 | +``` |
| 157 | + |
| 158 | +## List project generations |
| 159 | + |
| 160 | +```py |
| 161 | +from diffio import DiffioClient |
| 162 | + |
| 163 | +client = DiffioClient(apiKey="diffio_live_...") |
| 164 | +generations = client.projects.list_generations(apiProjectId="proj_123") |
| 165 | + |
| 166 | +for generation in generations.generations: |
| 167 | + print(generation.generationId, generation.status) |
| 168 | +``` |
| 169 | + |
| 170 | +## Webhooks portal access |
| 171 | + |
| 172 | +```py |
| 173 | +from diffio import DiffioClient |
| 174 | + |
| 175 | +client = DiffioClient(apiKey="diffio_live_...") |
| 176 | +portal = client.webhooks.get_portal_access(mode="test") |
| 177 | +print(portal.portalUrl) |
| 178 | +``` |
| 179 | + |
| 180 | +## Send a test webhook event |
| 181 | + |
| 182 | +```py |
| 183 | +from diffio import DiffioClient |
| 184 | + |
| 185 | +client = DiffioClient(apiKey="diffio_live_...") |
| 186 | +event = client.webhooks.send_test_event( |
| 187 | + eventType="generation.completed", |
| 188 | + mode="test", |
| 189 | + samplePayload={"apiProjectId": "proj_123"}, |
| 190 | +) |
| 191 | + |
| 192 | +print(event.svixMessageId) |
| 193 | +``` |
| 194 | + |
| 195 | +## Tutorials |
| 196 | + |
| 197 | +* Audio restoration CLI tutorial: `tutorials/audio-restoration-cli/README.md` |
| 198 | + |
| 199 | +## Runtime compatibility |
| 200 | + |
| 201 | +Use Python 3.8 or later. |
| 202 | + |
| 203 | +## Tests |
| 204 | + |
| 205 | +```bash |
| 206 | +cd diffio-python |
| 207 | +python -m pip install -r requirements-dev.txt |
| 208 | +python -m pytest |
| 209 | +``` |
0 commit comments