Spaces:
Sleeping
Sleeping
only for Image to Text
Browse files
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[:
|
| 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=
|
| 265 |
-
min_length=
|
| 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:
|