cabanemaths commited on
Commit
6129b65
·
verified ·
1 Parent(s): 3a1b387

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -68
app.py CHANGED
@@ -1,106 +1,51 @@
1
  from fastapi import FastAPI, UploadFile, File, Form
2
  from faster_whisper import WhisperModel
3
- import uvicorn
4
  import tempfile
5
  import shutil
6
- import torch
7
  import os
8
- import time
9
  from fastapi.middleware.cors import CORSMiddleware
10
- from pydub import AudioSegment
11
- from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
12
- from reportlab.lib.styles import getSampleStyleSheet
13
 
14
- # === Config GPU/CPU ===
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
16
- compute_type = "float16" if device == "cuda" else "int8"
17
-
18
- origins = [
19
- "https://cabane-data.fr" , "https://www.cabane-data.fr" # autoriser ton WordPress
20
- ]
21
 
22
  app = FastAPI()
23
 
24
  app.add_middleware(
25
  CORSMiddleware,
26
- allow_origins=origins,
27
  allow_credentials=True,
28
  allow_methods=["*"],
29
  allow_headers=["*"],
30
  )
31
 
32
- # === Dictionnaire des modèles dispo ===
33
  AVAILABLE_MODELS = ["tiny", "base", "small", "medium", "large-v2"]
34
 
35
  def load_model(model_name: str):
36
- """Charger un modèle Whisper"""
37
  return WhisperModel(model_name, device=device, compute_type=compute_type)
38
 
39
- def split_audio(file_path, chunk_length_ms=300_000):
40
- """Découpe l'audio en segments de 5 min max"""
41
- audio = AudioSegment.from_file(file_path)
42
- chunks = []
43
- for i in range(0, len(audio), chunk_length_ms):
44
- chunk = audio[i:i + chunk_length_ms]
45
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_chunk:
46
- chunk.export(tmp_chunk.name, format="mp3")
47
- chunks.append(tmp_chunk.name)
48
- return chunks
49
-
50
- def generate_pdf(text: str, output_path: str):
51
- """Créer un PDF avec la transcription"""
52
- doc = SimpleDocTemplate(output_path)
53
- styles = getSampleStyleSheet()
54
- story = [Paragraph("Transcription Audio", styles["Title"]), Spacer(1, 12)]
55
- story.append(Paragraph(text, styles["Normal"]))
56
- doc.build(story)
57
-
58
- # === Endpoint API REST ===
59
  @app.post("/transcribe")
60
- async def transcribe(
61
- file: UploadFile = File(...),
62
- model_name: str = Form("base") # par défaut "base"
63
- ):
64
  if model_name not in AVAILABLE_MODELS:
65
  return {"error": f"Modèle non reconnu. Choisissez parmi {AVAILABLE_MODELS}"}
66
 
67
- # Chronomètre début
68
- start_time = time.time()
69
-
70
- # Charger modèle
71
  model = load_model(model_name)
72
 
73
- # Sauvegarder fichier temporaire
74
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
75
  shutil.copyfileobj(file.file, tmp)
76
  tmp_path = tmp.name
77
 
78
- # Découpage en segments
79
- chunks = split_audio(tmp_path)
80
- full_text = ""
81
-
82
- # Transcrire chaque segment
83
- for chunk in chunks:
84
- segments, info = model.transcribe(chunk, beam_size=5)
85
- text_result = " ".join([segment.text for segment in segments])
86
- full_text += text_result + "\n"
87
- os.remove(chunk)
88
-
89
- # Nettoyage
90
  os.remove(tmp_path)
91
 
92
- # Génération PDF
93
- pdf_path = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
94
- generate_pdf(full_text, pdf_path)
95
-
96
- # Chronomètre fin
97
- total_time = round(time.time() - start_time, 2)
98
 
99
  return {
100
- "model_used": model_name,
101
- "language": info.language,
102
- "probability": info.language_probability,
103
- "transcription": full_text,
104
- "pdf_file": pdf_path,
105
- "processing_time_sec": total_time
106
  }
 
1
  from fastapi import FastAPI, UploadFile, File, Form
2
  from faster_whisper import WhisperModel
 
3
  import tempfile
4
  import shutil
 
5
  import os
6
+ import torch
7
  from fastapi.middleware.cors import CORSMiddleware
8
+ import time
 
 
9
 
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ compute_type = "float16" if device=="cuda" else "int8"
 
 
 
 
12
 
13
  app = FastAPI()
14
 
15
  app.add_middleware(
16
  CORSMiddleware,
17
+ allow_origins=["*"], # à restreindre à ton domaine
18
  allow_credentials=True,
19
  allow_methods=["*"],
20
  allow_headers=["*"],
21
  )
22
 
 
23
  AVAILABLE_MODELS = ["tiny", "base", "small", "medium", "large-v2"]
24
 
25
  def load_model(model_name: str):
 
26
  return WhisperModel(model_name, device=device, compute_type=compute_type)
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @app.post("/transcribe")
29
+ async def transcribe(file: UploadFile = File(...), model_name: str = Form("base")):
 
 
 
30
  if model_name not in AVAILABLE_MODELS:
31
  return {"error": f"Modèle non reconnu. Choisissez parmi {AVAILABLE_MODELS}"}
32
 
33
+ start = time.time()
 
 
 
34
  model = load_model(model_name)
35
 
 
36
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
37
  shutil.copyfileobj(file.file, tmp)
38
  tmp_path = tmp.name
39
 
40
+ segments, info = model.transcribe(tmp_path, beam_size=5)
41
+ text_result = " ".join([s.text for s in segments])
 
 
 
 
 
 
 
 
 
 
42
  os.remove(tmp_path)
43
 
44
+ end = time.time()
45
+ duration = round(end - start, 2)
 
 
 
 
46
 
47
  return {
48
+ "text": text_result,
49
+ "duration": duration,
50
+ "model_used": model_name
 
 
 
51
  }