danicor commited on
Commit
caed707
·
verified ·
1 Parent(s): f47b96a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -12
app.py CHANGED
@@ -6,10 +6,14 @@ import torch
6
  import tempfile
7
  import os
8
  import uvicorn
 
 
 
 
 
9
 
10
  app = FastAPI()
11
 
12
- # اضافه کردن CORS
13
  app.add_middleware(
14
  CORSMiddleware,
15
  allow_origins=["*"],
@@ -19,44 +23,83 @@ app.add_middleware(
19
  )
20
 
21
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
22
  model = whisper.load_model("large-v3", device=device)
 
23
 
24
  @app.get("/")
25
  async def root():
26
- return {"message": "Whisper API is running"}
27
 
28
  @app.post("/transcribe")
29
  async def transcribe_audio(file: UploadFile = File(...)):
30
- if not file:
31
- raise HTTPException(status_code=400, detail="No file provided")
32
-
33
  tmp_file_path = None
 
34
  try:
 
 
 
 
 
35
  contents = await file.read()
 
 
36
 
37
- if len(contents) > 50 * 1024 * 1024:
38
  raise HTTPException(status_code=413, detail="File too large")
39
 
40
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
 
 
 
 
 
 
 
 
41
  tmp_file.write(contents)
42
  tmp_file_path = tmp_file.name
43
 
 
 
44
  result = model.transcribe(
45
  tmp_file_path,
46
  fp16=False if device == "cpu" else True,
47
- language="auto",
48
  task="transcribe",
49
- verbose=False
 
50
  )
51
 
52
- return JSONResponse({"text": result["text"].strip()})
 
 
 
 
 
 
53
 
54
  except Exception as e:
55
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
56
 
57
  finally:
58
  if tmp_file_path and os.path.exists(tmp_file_path):
59
- os.unlink(tmp_file_path)
 
 
 
 
 
 
 
60
 
61
  if __name__ == "__main__":
62
  uvicorn.run(app, host="0.0.0.0", port=7860, timeout_keep_alive=300)
 
6
  import tempfile
7
  import os
8
  import uvicorn
9
+ import logging
10
+
11
+ # تنظیم لاگ
12
+ logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
 
15
  app = FastAPI()
16
 
 
17
  app.add_middleware(
18
  CORSMiddleware,
19
  allow_origins=["*"],
 
23
  )
24
 
25
  device = "cuda" if torch.cuda.is_available() else "cpu"
26
+ logger.info(f"Loading model on {device}")
27
  model = whisper.load_model("large-v3", device=device)
28
+ logger.info("Model loaded successfully")
29
 
30
  @app.get("/")
31
  async def root():
32
+ return {"message": "Whisper API is running", "device": device}
33
 
34
  @app.post("/transcribe")
35
  async def transcribe_audio(file: UploadFile = File(...)):
 
 
 
36
  tmp_file_path = None
37
+
38
  try:
39
+ logger.info(f"Received file: {file.filename}, size: {file.size}")
40
+
41
+ if not file or not file.filename:
42
+ raise HTTPException(status_code=400, detail="No valid file provided")
43
+
44
  contents = await file.read()
45
+ file_size = len(contents)
46
+ logger.info(f"File read successfully, size: {file_size} bytes")
47
 
48
+ if file_size > 50 * 1024 * 1024:
49
  raise HTTPException(status_code=413, detail="File too large")
50
 
51
+ if file_size == 0:
52
+ raise HTTPException(status_code=400, detail="Empty file")
53
+
54
+ # تشخیص فرمت فایل
55
+ file_ext = os.path.splitext(file.filename)[1].lower()
56
+ if not file_ext:
57
+ file_ext = ".wav"
58
+
59
+ with tempfile.NamedTemporaryFile(delete=False, suffix=file_ext) as tmp_file:
60
  tmp_file.write(contents)
61
  tmp_file_path = tmp_file.name
62
 
63
+ logger.info(f"Temp file created: {tmp_file_path}")
64
+
65
  result = model.transcribe(
66
  tmp_file_path,
67
  fp16=False if device == "cpu" else True,
68
+ language=None,
69
  task="transcribe",
70
+ verbose=False,
71
+ word_timestamps=False
72
  )
73
 
74
+ logger.info("Transcription completed")
75
+
76
+ text = result["text"].strip()
77
+ if not text:
78
+ return JSONResponse({"text": "متن شناسایی نشد", "warning": "No speech detected"})
79
+
80
+ return JSONResponse({"text": text})
81
 
82
  except Exception as e:
83
+ logger.error(f"Error in transcription: {str(e)}")
84
+ if "No module named" in str(e):
85
+ raise HTTPException(status_code=500, detail="Missing required modules")
86
+ elif "CUDA" in str(e):
87
+ raise HTTPException(status_code=500, detail="GPU error")
88
+ elif "FFmpeg" in str(e):
89
+ raise HTTPException(status_code=500, detail="Audio processing error")
90
+ else:
91
+ raise HTTPException(status_code=500, detail=f"Processing error: {str(e)}")
92
 
93
  finally:
94
  if tmp_file_path and os.path.exists(tmp_file_path):
95
+ try:
96
+ os.unlink(tmp_file_path)
97
+ logger.info(f"Temp file deleted: {tmp_file_path}")
98
+ except:
99
+ pass
100
+
101
+ if __name__ == "__main__":
102
+ uvicorn.run(app, host="0.0.0.0", port=7860, timeout_keep_alive=300)
103
 
104
  if __name__ == "__main__":
105
  uvicorn.run(app, host="0.0.0.0", port=7860, timeout_keep_alive=300)