Ravishankarsharma commited on
Commit
e8d2326
·
verified ·
1 Parent(s): 68b5071

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -43
app.py CHANGED
@@ -1,23 +1,30 @@
1
  import os
2
- import csv
3
- from datetime import datetime
 
 
 
 
 
 
 
 
 
4
  from fastapi import FastAPI, UploadFile, File, Form, HTTPException
5
  from fastapi.middleware.cors import CORSMiddleware
6
- import whisper
7
  from transformers import pipeline
8
 
9
- # ---------------- Config ----------------
10
- UPLOAD_DIR = os.getenv("UPLOAD_DIR", "./uploads")
11
 
12
- if not os.path.exists(UPLOAD_DIR):
13
- try:
14
- os.makedirs(UPLOAD_DIR, exist_ok=True)
15
- except PermissionError:
16
- UPLOAD_DIR = "."
17
 
18
- # ---------------- App ----------------
19
  app = FastAPI()
20
 
 
21
  app.add_middleware(
22
  CORSMiddleware,
23
  allow_origins=["*"],
@@ -26,45 +33,23 @@ app.add_middleware(
26
  allow_headers=["*"],
27
  )
28
 
29
- # ---------------- Models ----------------
30
- asr_model = whisper.load_model("base")
31
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
32
-
33
- # ---------------- Utils ----------------
34
- def save_to_csv(audio_file: str, transcription: str, summary: str):
35
- csv_file = os.path.join(UPLOAD_DIR, "output.csv")
36
- file_exists = os.path.isfile(csv_file)
37
 
38
- with open(csv_file, mode="a", newline="", encoding="utf-8") as f:
39
- writer = csv.writer(f)
40
- if not file_exists:
41
- writer.writerow(["Timestamp", "Audio_File", "Transcription", "Summary"])
42
- writer.writerow([datetime.now().isoformat(), audio_file, transcription, summary])
43
 
44
- # ---------------- Routes ----------------
45
  @app.post("/transcribe/")
46
- async def transcribe_audio(file: UploadFile = File(...), summary_length: int = Form(100)):
47
  try:
48
- # Save uploaded file
49
  file_path = os.path.join(UPLOAD_DIR, file.filename)
50
- with open(file_path, "wb") as buffer:
51
- buffer.write(await file.read())
52
-
53
- # Transcribe
54
- result = asr_model.transcribe(file_path)
55
- transcription = result["text"]
56
 
57
- # Summarize
58
- summary = summarizer(transcription, max_length=summary_length, min_length=30, do_sample=False)[0]["summary_text"]
59
 
60
- # Save to CSV
61
- save_to_csv(file.filename, transcription, summary)
62
 
63
- return {
64
- "filename": file.filename,
65
- "transcription": transcription,
66
- "summary": summary,
67
- "csv_saved": True
68
- }
69
  except Exception as e:
70
  raise HTTPException(status_code=500, detail=str(e))
 
1
  import os
2
+ import time
3
+ import json
4
+ import tempfile
5
+ import threading
6
+ from typing import Optional
7
+ from datetime import datetime, timezone
8
+
9
+ import httpx
10
+ import whisper
11
+ import nltk
12
+ from nltk.tokenize import sent_tokenize
13
  from fastapi import FastAPI, UploadFile, File, Form, HTTPException
14
  from fastapi.middleware.cors import CORSMiddleware
 
15
  from transformers import pipeline
16
 
17
+ # ---------------- Fix Whisper cache issue ----------------
18
+ os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
19
 
20
+ # ---------------- Config ----------------
21
+ UPLOAD_DIR = "uploads"
22
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
 
 
23
 
24
+ # Initialize FastAPI
25
  app = FastAPI()
26
 
27
+ # Allow CORS for all origins
28
  app.add_middleware(
29
  CORSMiddleware,
30
  allow_origins=["*"],
 
33
  allow_headers=["*"],
34
  )
35
 
36
+ # Load Whisper model (small for speed)
37
+ model = whisper.load_model("small")
 
 
 
 
 
 
38
 
 
 
 
 
 
39
 
 
40
  @app.post("/transcribe/")
41
+ async def transcribe_audio(file: UploadFile = File(...)):
42
  try:
43
+ # Save uploaded file temporarily
44
  file_path = os.path.join(UPLOAD_DIR, file.filename)
45
+ with open(file_path, "wb") as f:
46
+ f.write(await file.read())
 
 
 
 
47
 
48
+ # Transcribe with Whisper
49
+ result = model.transcribe(file_path)
50
 
51
+ # Return text output
52
+ return {"filename": file.filename, "transcription": result["text"]}
53
 
 
 
 
 
 
 
54
  except Exception as e:
55
  raise HTTPException(status_code=500, detail=str(e))