MichaelChou0806 commited on
Commit
0d4c4bd
·
verified ·
1 Parent(s): c76e92c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -69
app.py CHANGED
@@ -253,107 +253,140 @@ async def api_transcribe_sync(request: Request):
253
  )
254
 
255
  # ====== Gradio 介面 ======
256
- with gr.Blocks(theme=gr.themes.Soft(), title="LINE Audio Transcription") as demo:
257
- gr.Markdown("# 🎧 LINE Audio Transcription & Summary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
 
259
  with gr.Tab("🌐 Web Upload"):
260
- gr.Markdown("### Upload audio file directly from browser")
261
- with gr.Row():
262
- with gr.Column(scale=1):
263
- pw_ui = gr.Textbox(label="Password", type="password")
264
- file_ui = gr.File(label="Upload Audio File", file_types=["audio"])
265
- btn_ui = gr.Button("Start Transcription 🚀", variant="primary", size="lg")
266
- with gr.Column(scale=2):
267
- status_ui = gr.Textbox(label="Status", interactive=False)
268
- transcript_ui = gr.Textbox(label="Transcription Result", lines=10)
269
- summary_ui = gr.Textbox(label="AI Summary", lines=6)
270
 
271
- btn_ui.click(transcribe_ui, [pw_ui, file_ui], [status_ui, transcript_ui, summary_ui])
272
-
273
- with gr.Tab("📱 API Documentation"):
274
- gr.Markdown("""
275
- ### 🚀 Synchronous API (Recommended for iPhone Shortcuts)
276
-
277
- **Endpoint**: `/api/transcribe` (POST)
 
 
 
 
 
 
 
 
278
 
279
- **完全同步** - 直接返回結果,無需輪詢
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
- ✅ **穩定可靠** - 不受音檔長度影響,自動等待完成
 
 
 
 
 
 
 
 
282
 
283
- ---
 
 
 
284
 
285
- #### Request Format (JSON):
286
  ```json
287
  {
288
  "password": "your_password",
289
- "file_data": "data:audio/m4a;base64,UklGR...",
290
  "file_name": "recording.m4a"
291
  }
292
  ```
293
 
294
- #### Response Format:
295
  ```json
296
  {
297
  "status": "success",
298
- "transcription": "轉錄內容...",
299
- "summary": "摘要內容..."
300
  }
301
  ```
302
 
303
  ---
304
 
305
- ### 📱 iPhone Shortcuts 設定
306
-
307
- **動作流程:**
308
 
309
- 1. **取得檔案** 語音檔
310
- 2. **Base64 編碼**
311
- 3. **文字** (組合 data URL):
312
- ```
313
- data:audio/m4a;base64,Base64編碼結果
314
- ```
315
- 4. **字典** (請求本文):
316
- - 鍵: `password`, 值: `chou`
317
- - 鍵: `file_data`, 值: 上一步的文字
318
- - 鍵: `file_name`, 值: `recording.m4a`
319
- 5. **取得 URL 內容**:
320
- - URL: `https://你的網址/api/transcribe`
321
- - 方法: `POST`
322
- - 標頭: `Content-Type` = `application/json`
323
- - 請求本文: 上一步的字典
324
- - 請求本文類型: `JSON`
325
- 6. **從字典取得值**:
326
- - 鍵: `transcription` → 轉錄結果
327
- - 鍵: `summary` → 摘要
328
 
329
- ---
330
 
331
- ### 💡 重要提醒
332
 
333
- - ✅ 這個端點**完全同步**,會等待轉錄完成後才返回
334
- - ✅ 無論音檔多長,都會自動處理完成
335
- - ✅ 不需要設定等待時間或輪詢機制
336
- - ✅ 直接取得最終結果,不會有 `event_id`
337
 
338
- ### 🧪 測試 API
339
 
340
- 使用 curl 測試:
341
- ```bash
342
- curl -X POST https://你的網址/api/transcribe \\
343
- -H "Content-Type: application/json" \\
344
- -d '{
345
- "password": "chou",
346
- "file_data": "data:audio/m4a;base64,AAAA...",
347
- "file_name": "test.m4a"
348
- }'
349
- ```
 
 
 
350
  """)
351
 
352
  gr.Markdown("""
353
  ---
354
- 💡 **Supported Formats**: MP4, M4A, MP3, WAV, OGG, WEBM
355
- 📦 **Max File Size**: 25MB per chunk (auto-split)
356
- 🔒 **Security**: Password-protected
357
  """)
358
 
359
  # ====== 掛載 Gradio 到 FastAPI ======
 
253
  )
254
 
255
  # ====== Gradio 介面 ======
256
+ with gr.Blocks(
257
+ theme=gr.themes.Soft(),
258
+ title="LINE Audio Transcription",
259
+ css="""
260
+ /* 手機優化樣式 */
261
+ @media (max-width: 768px) {
262
+ .gradio-container {
263
+ padding: 10px !important;
264
+ }
265
+ /* 強制單欄布局 */
266
+ .contain {
267
+ flex-direction: column !important;
268
+ }
269
+ /* 調整按鈕大小 */
270
+ button {
271
+ font-size: 16px !important;
272
+ padding: 12px !important;
273
+ }
274
+ /* 調整輸入框 */
275
+ input, textarea {
276
+ font-size: 16px !important;
277
+ }
278
+ /* Tab 標籤更大更好點擊 */
279
+ .tabs button {
280
+ padding: 12px 16px !important;
281
+ font-size: 15px !important;
282
+ }
283
+ }
284
+ """
285
+ ) as demo:
286
+ gr.Markdown("# 🎧 LINE Audio Transcription")
287
 
288
  with gr.Tab("🌐 Web Upload"):
289
+ gr.Markdown("### Upload audio file from browser")
 
 
 
 
 
 
 
 
 
290
 
291
+ # 手機版:改用單欄布局
292
+ pw_ui = gr.Textbox(
293
+ label="Password",
294
+ type="password",
295
+ placeholder="Enter password"
296
+ )
297
+ file_ui = gr.File(
298
+ label="Upload Audio File",
299
+ file_types=["audio"]
300
+ )
301
+ btn_ui = gr.Button(
302
+ "Start Transcription 🚀",
303
+ variant="primary",
304
+ size="lg"
305
+ )
306
 
307
+ status_ui = gr.Textbox(
308
+ label="Status",
309
+ interactive=False,
310
+ show_label=True
311
+ )
312
+ transcript_ui = gr.Textbox(
313
+ label="Transcription Result",
314
+ lines=8,
315
+ placeholder="Transcription will appear here...",
316
+ show_copy_button=True
317
+ )
318
+ summary_ui = gr.Textbox(
319
+ label="AI Summary",
320
+ lines=5,
321
+ placeholder="Summary will appear here...",
322
+ show_copy_button=True
323
+ )
324
 
325
+ btn_ui.click(
326
+ transcribe_ui,
327
+ inputs=[pw_ui, file_ui],
328
+ outputs=[status_ui, transcript_ui, summary_ui]
329
+ )
330
+
331
+ with gr.Tab("📱 API Info"):
332
+ gr.Markdown("""
333
+ ### iPhone Shortcuts Integration
334
 
335
+ **Endpoint:**
336
+ ```
337
+ POST /api/transcribe
338
+ ```
339
 
340
+ **Request (JSON):**
341
  ```json
342
  {
343
  "password": "your_password",
344
+ "file_data": "data:audio/m4a;base64,...",
345
  "file_name": "recording.m4a"
346
  }
347
  ```
348
 
349
+ **Response:**
350
  ```json
351
  {
352
  "status": "success",
353
+ "transcription": "...",
354
+ "summary": "..."
355
  }
356
  ```
357
 
358
  ---
359
 
360
+ ### Key Points
 
 
361
 
362
+ **Fully synchronous** - Returns result directly
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
 
364
+ ✅ **No polling needed** - Waits until completion
365
 
366
+ **Works with any audio length** - Auto-handles long files
367
 
368
+ ---
 
 
 
369
 
370
+ ### Shortcuts Setup
371
 
372
+ 1. Get file → Your audio
373
+ 2. Base64 encode
374
+ 3. Text: `data:audio/m4a;base64,[encoded]`
375
+ 4. Dictionary:
376
+ - `password`: `chou`
377
+ - `file_data`: Step 3 text
378
+ - `file_name`: `recording.m4a`
379
+ 5. Get URL contents:
380
+ - URL: `/api/transcribe`
381
+ - Method: POST
382
+ - Header: `Content-Type: application/json`
383
+ - Body: Step 4 dictionary (JSON)
384
+ 6. Get `transcription` and `summary` from response
385
  """)
386
 
387
  gr.Markdown("""
388
  ---
389
+ 💡 **Formats**: MP4, M4A, MP3, WAV, OGG, WEBM | **Max**: 25MB/chunk | 🔒 **Password-protected**
 
 
390
  """)
391
 
392
  # ====== 掛載 Gradio 到 FastAPI ======