Skip to content

Commit 7335d77

Browse files
committed
adding new code
1 parent 3bbfc8e commit 7335d77

File tree

2 files changed

+377
-242
lines changed

2 files changed

+377
-242
lines changed

docs/whisper_transcription/whisper_api_server.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from queue import Queue
77
from threading import Lock
88
from fastapi import FastAPI, UploadFile, File, Form
9-
from fastapi.responses import StreamingResponse, JSONResponse
10-
from faster_code_week1_v28 import transcribe_and_summarize, load_whisper_model_faster
9+
from fastapi.responses import StreamingResponse, JSONResponse, FileResponse
10+
from whisper_code import transcribe_and_summarize, load_whisper_model_faster
11+
import logging
1112

1213
app = FastAPI()
1314

@@ -28,6 +29,21 @@ async def transcribe_audio_api(
2829
max_speakers: int = Form(None),
2930
streaming: bool = Form(False)
3031
):
32+
33+
# ✅ Setup dynamic logging per request
34+
from datetime import datetime
35+
basename = os.path.splitext(os.path.basename(audio_file.filename))[0]
36+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
37+
log_file = f"transcription_log_{basename}_{timestamp}.log"
38+
39+
logging.basicConfig(
40+
level=logging.INFO,
41+
format="%(asctime)s - %(levelname)s - %(message)s",
42+
filename=log_file,
43+
filemode="w",
44+
force=True # ensures logging resets each request
45+
)
46+
3147
temp_audio_path = tempfile.mktemp(suffix=f"_{audio_file.filename}")
3248
with open(temp_audio_path, "wb") as f:
3349
f.write(await audio_file.read())
@@ -45,6 +61,14 @@ def generator():
4561
q = Queue()
4662

4763
def api_callback(result):
64+
# Log like CLI mode
65+
if result.get("chunk_id", "").startswith("chunk_"):
66+
logging.info(f"\n====== Streaming {result['chunk_id']} ======\n{result.get('text', '').strip()}\n")
67+
elif result.get("chunk_id") == "summary":
68+
logging.info("\n====== Final Summary ======\n")
69+
logging.info(result.get("summary", "").strip())
70+
result["logfile"] = log_file
71+
4872
q.put(json.dumps(result) + "\n")
4973

5074
def run_pipeline():
@@ -126,4 +150,11 @@ def run_pipeline():
126150
return JSONResponse(
127151
content={"error": f"Failed to read output JSON: {str(e)}"},
128152
status_code=500
129-
)
153+
)
154+
155+
@app.get("/log/{filename}")
156+
def get_log_file(filename: str):
157+
log_path = os.path.join(".", filename)
158+
if not os.path.exists(log_path):
159+
return JSONResponse(content={"error": "Log file not found."}, status_code=404)
160+
return FileResponse(log_path, media_type="text/plain")

0 commit comments

Comments
 (0)