Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,17 +17,20 @@ def analyze_book_cover(image):
|
|
| 17 |
if image is None:
|
| 18 |
return "Please upload an image.", "", "", "", ""
|
| 19 |
|
|
|
|
| 20 |
try:
|
| 21 |
# Save PIL Image to temporary file (analyzer expects file path)
|
| 22 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp:
|
| 23 |
-
image.save(tmp.name)
|
| 24 |
tmp_path = tmp.name
|
| 25 |
|
| 26 |
# Analyze
|
| 27 |
result = analyzer.analyze(tmp_path)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Extract interpretation
|
| 33 |
interp = result.get('interpretation', {})
|
|
@@ -39,22 +42,28 @@ def analyze_book_cover(image):
|
|
| 39 |
|
| 40 |
# Format detected elements
|
| 41 |
image_regions = result.get('image_regions', [])
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Format detected text
|
| 47 |
text_regions = result.get('text_regions', [])
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
return title, authors, publisher, elements_text, text_output
|
| 60 |
|
|
@@ -62,6 +71,14 @@ def analyze_book_cover(image):
|
|
| 62 |
error_msg = f"Error analyzing image: {str(e)}"
|
| 63 |
return error_msg, "", "", "", ""
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# Create Gradio interface
|
| 66 |
demo = gr.Interface(
|
| 67 |
fn=analyze_book_cover,
|
|
|
|
| 17 |
if image is None:
|
| 18 |
return "Please upload an image.", "", "", "", ""
|
| 19 |
|
| 20 |
+
tmp_path = None
|
| 21 |
try:
|
| 22 |
# Save PIL Image to temporary file (analyzer expects file path)
|
| 23 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp:
|
| 24 |
+
image.save(tmp.name, format='JPEG')
|
| 25 |
tmp_path = tmp.name
|
| 26 |
|
| 27 |
# Analyze
|
| 28 |
result = analyzer.analyze(tmp_path)
|
| 29 |
|
| 30 |
+
# Check if analysis succeeded
|
| 31 |
+
if not result.get('success', False):
|
| 32 |
+
error = result.get('error', 'Unknown error')
|
| 33 |
+
return f"Analysis failed: {error}", "", "", "", ""
|
| 34 |
|
| 35 |
# Extract interpretation
|
| 36 |
interp = result.get('interpretation', {})
|
|
|
|
| 42 |
|
| 43 |
# Format detected elements
|
| 44 |
image_regions = result.get('image_regions', [])
|
| 45 |
+
if image_regions:
|
| 46 |
+
elements_text = f"**Detected {len(image_regions)} visual elements:**\n"
|
| 47 |
+
for elem in image_regions[:5]: # Top 5
|
| 48 |
+
elements_text += f"- {elem['location']}: {elem['pytorch_class']} ({elem['confidence']:.1%})\n"
|
| 49 |
+
else:
|
| 50 |
+
elements_text = "No visual elements detected (possibly plain text cover)"
|
| 51 |
|
| 52 |
# Format detected text
|
| 53 |
text_regions = result.get('text_regions', [])
|
| 54 |
+
if text_regions:
|
| 55 |
+
text_output = f"**Detected {len(text_regions)} text regions:**\n"
|
| 56 |
+
for region in text_regions[:10]: # Top 10
|
| 57 |
+
text_output += f"- {region['text']}\n"
|
| 58 |
|
| 59 |
+
# Format other text (reviews/quotes)
|
| 60 |
+
other = interp.get('other_text', [])
|
| 61 |
+
if other:
|
| 62 |
+
text_output += f"\n**Reviews/Quotes:**\n"
|
| 63 |
+
for text in other[:3]:
|
| 64 |
+
text_output += f"- {text}\n"
|
| 65 |
+
else:
|
| 66 |
+
text_output = "No text detected on cover"
|
| 67 |
|
| 68 |
return title, authors, publisher, elements_text, text_output
|
| 69 |
|
|
|
|
| 71 |
error_msg = f"Error analyzing image: {str(e)}"
|
| 72 |
return error_msg, "", "", "", ""
|
| 73 |
|
| 74 |
+
finally:
|
| 75 |
+
# Always clean up temp file, even if there's an error
|
| 76 |
+
if tmp_path and os.path.exists(tmp_path):
|
| 77 |
+
try:
|
| 78 |
+
os.unlink(tmp_path)
|
| 79 |
+
except:
|
| 80 |
+
pass # Ignore cleanup errors
|
| 81 |
+
|
| 82 |
# Create Gradio interface
|
| 83 |
demo = gr.Interface(
|
| 84 |
fn=analyze_book_cover,
|