vidhi0405 commited on
Commit
4864dad
·
1 Parent(s): e81f17b

only for Image to Text

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -175,7 +175,7 @@ def _ensure_db_ready():
175
  raise AppError(db_init_error, 503)
176
 
177
 
178
- def _finalize_caption(raw_text: str) -> str:
179
  text = " ".join(raw_text.split()).strip()
180
  if not text:
181
  return ""
@@ -184,7 +184,7 @@ def _finalize_caption(raw_text: str) -> str:
184
  sentences = [s.strip() for s in sentences if s.strip()]
185
 
186
  if len(sentences) >= CAPTION_MIN_SENTENCES:
187
- return " ".join(sentences[:CAPTION_MAX_SENTENCES]).strip()
188
 
189
  if text and text[-1] not in ".!?":
190
  text = re.sub(r"[,:;\-]\s*[^,:;\-]*$", "", text).strip()
@@ -261,8 +261,8 @@ def summarize_captions(captions: list[str]) -> str:
261
  with torch.no_grad():
262
  output_ids = model.generate(
263
  **inputs,
264
- max_length=150,
265
- min_length=20,
266
  length_penalty=2.0,
267
  num_beams=4,
268
  early_stopping=True,
@@ -271,7 +271,7 @@ def summarize_captions(captions: list[str]) -> str:
271
  except Exception as exc:
272
  raise AppError("Failed to summarize captions.", 500) from exc
273
 
274
- return _finalize_caption(summary)
275
 
276
 
277
  def generate_caption_text(image: Image.Image) -> str:
 
175
  raise AppError(db_init_error, 503)
176
 
177
 
178
+ def _finalize_caption(raw_text: str, max_sentences: int = CAPTION_MAX_SENTENCES) -> str:
179
  text = " ".join(raw_text.split()).strip()
180
  if not text:
181
  return ""
 
184
  sentences = [s.strip() for s in sentences if s.strip()]
185
 
186
  if len(sentences) >= CAPTION_MIN_SENTENCES:
187
+ return " ".join(sentences[:max_sentences]).strip()
188
 
189
  if text and text[-1] not in ".!?":
190
  text = re.sub(r"[,:;\-]\s*[^,:;\-]*$", "", text).strip()
 
261
  with torch.no_grad():
262
  output_ids = model.generate(
263
  **inputs,
264
+ max_length=300,
265
+ min_length=50,
266
  length_penalty=2.0,
267
  num_beams=4,
268
  early_stopping=True,
 
271
  except Exception as exc:
272
  raise AppError("Failed to summarize captions.", 500) from exc
273
 
274
+ return _finalize_caption(summary, max_sentences=10)
275
 
276
 
277
  def generate_caption_text(image: Image.Image) -> str: