Ravishankarsharma commited on
Commit
08bbe1b
·
verified ·
1 Parent(s): e6ebc18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -11
app.py CHANGED
@@ -49,7 +49,7 @@ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
49
  app = FastAPI(
50
  title="Voice2Text API",
51
  description="Audio Transcription + Summarization + Default Audio Processing",
52
- version="2.2.0"
53
  )
54
 
55
  app.add_middleware(
@@ -65,19 +65,19 @@ app.add_middleware(
65
  @app.post("/transcribe/")
66
  async def transcribe_audio(
67
  file: Optional[UploadFile] = File(None),
68
- summary: Optional[bool] = Form(True) # default summary True
69
  ):
70
  """Transcribe uploaded audio or default audio if no file provided"""
71
  tmp_path = None
72
  try:
73
  if file is not None:
74
- # Save uploaded file temporarily
75
  with tempfile.NamedTemporaryFile(delete=False, dir=UPLOAD_DIR, suffix=".wav") as tmp:
76
  content = await file.read()
77
  tmp.write(content)
78
  tmp_path = tmp.name
79
  else:
80
- # Use default audio if no file uploaded
81
  default_audio_path = os.path.join(UPLOAD_DIR, "default_audio.wav")
82
  if not os.path.exists(default_audio_path):
83
  raise HTTPException(
@@ -86,17 +86,14 @@ async def transcribe_audio(
86
  )
87
  tmp_path = default_audio_path
88
 
89
- # Transcribe audio
90
  result = asr_model.transcribe(tmp_path)
91
  transcription = result.get("text", "")
92
 
93
- # Summarization
94
  if summary and transcription.strip():
95
  sentences = sent_tokenize(transcription)
96
- chunks = [
97
- " ".join(sentences[i:i + 3])
98
- for i in range(0, len(sentences), 3)
99
- ]
100
  summarized_text = " ".join(
101
  summarizer(chunk, max_length=60, min_length=25, do_sample=False)[0]["summary_text"]
102
  for chunk in chunks
@@ -111,7 +108,36 @@ async def transcribe_audio(
111
  if file is not None and tmp_path and os.path.exists(tmp_path):
112
  os.remove(tmp_path)
113
 
114
- # ---------------- Direct Text Summarization ----------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  @app.get("/summarize_direct/")
117
  async def summarize_direct():
 
49
  app = FastAPI(
50
  title="Voice2Text API",
51
  description="Audio Transcription + Summarization + Default Audio Processing",
52
+ version="2.4.0"
53
  )
54
 
55
  app.add_middleware(
 
65
  @app.post("/transcribe/")
66
  async def transcribe_audio(
67
  file: Optional[UploadFile] = File(None),
68
+ summary: Optional[bool] = Form(True)
69
  ):
70
  """Transcribe uploaded audio or default audio if no file provided"""
71
  tmp_path = None
72
  try:
73
  if file is not None:
74
+ # Uploaded audio
75
  with tempfile.NamedTemporaryFile(delete=False, dir=UPLOAD_DIR, suffix=".wav") as tmp:
76
  content = await file.read()
77
  tmp.write(content)
78
  tmp_path = tmp.name
79
  else:
80
+ # Default audio
81
  default_audio_path = os.path.join(UPLOAD_DIR, "default_audio.wav")
82
  if not os.path.exists(default_audio_path):
83
  raise HTTPException(
 
86
  )
87
  tmp_path = default_audio_path
88
 
89
+ # Transcribe
90
  result = asr_model.transcribe(tmp_path)
91
  transcription = result.get("text", "")
92
 
93
+ # Summarize if needed
94
  if summary and transcription.strip():
95
  sentences = sent_tokenize(transcription)
96
+ chunks = [" ".join(sentences[i:i + 3]) for i in range(0, len(sentences), 3)]
 
 
 
97
  summarized_text = " ".join(
98
  summarizer(chunk, max_length=60, min_length=25, do_sample=False)[0]["summary_text"]
99
  for chunk in chunks
 
108
  if file is not None and tmp_path and os.path.exists(tmp_path):
109
  os.remove(tmp_path)
110
 
111
+
112
+ @app.get("/auto_summarize/")
113
+ async def auto_summarize():
114
+ """Automatically summarize default audio if exists, else summarize sample text"""
115
+ default_audio_path = os.path.join(UPLOAD_DIR, "default_audio.wav")
116
+
117
+ if os.path.exists(default_audio_path):
118
+ # Default audio exists → transcribe + summarize
119
+ result = asr_model.transcribe(default_audio_path)
120
+ transcription = result.get("text", "")
121
+ if transcription.strip():
122
+ sentences = sent_tokenize(transcription)
123
+ chunks = [" ".join(sentences[i:i + 3]) for i in range(0, len(sentences), 3)]
124
+ summarized_text = " ".join(
125
+ summarizer(chunk, max_length=60, min_length=25, do_sample=False)[0]["summary_text"]
126
+ for chunk in chunks
127
+ )
128
+ return {"transcription": transcription, "summary": summarized_text.strip()}
129
+ else:
130
+ return {"transcription": "", "summary": ""}
131
+ else:
132
+ # Default audio missing → summarize sample text
133
+ sample_text = """
134
+ Artificial Intelligence and Machine Learning are transforming industries
135
+ by enabling automation, advanced analytics, and data-driven decision making.
136
+ These technologies are particularly useful in healthcare, finance, and education.
137
+ """
138
+ summary = summarizer(sample_text, max_length=100, min_length=25, do_sample=False)
139
+ return {"transcription": None, "summary": summary[0]["summary_text"]}
140
+
141
 
142
  @app.get("/summarize_direct/")
143
  async def summarize_direct():