Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ import os
|
|
| 8 |
import io
|
| 9 |
from PIL import Image
|
| 10 |
import google.generativeai as genai
|
| 11 |
-
from google.generativeai import types
|
| 12 |
|
| 13 |
|
| 14 |
def initialize_gemini():
|
|
@@ -39,11 +38,6 @@ def generate_image_description(image):
|
|
| 39 |
if image.mode != 'RGB':
|
| 40 |
image = image.convert('RGB')
|
| 41 |
|
| 42 |
-
# Convert PIL image to bytes
|
| 43 |
-
buffered = io.BytesIO()
|
| 44 |
-
image.save(buffered, format="JPEG")
|
| 45 |
-
image_bytes = buffered.getvalue()
|
| 46 |
-
|
| 47 |
# Create prompt for detailed image description
|
| 48 |
prompt = """You are an expert at describing images in detail. Analyze this image and provide a comprehensive description that includes:
|
| 49 |
|
|
@@ -58,13 +52,7 @@ Provide a clear, detailed description that would help someone who cannot see the
|
|
| 58 |
|
| 59 |
# Generate description using Gemma 3n via Gemini API
|
| 60 |
model = genai.GenerativeModel('gemma-3n-e4b-it')
|
| 61 |
-
response = model.generate_content([
|
| 62 |
-
types.Part.from_bytes(
|
| 63 |
-
data=image_bytes,
|
| 64 |
-
mime_type='image/jpeg',
|
| 65 |
-
),
|
| 66 |
-
prompt
|
| 67 |
-
])
|
| 68 |
|
| 69 |
if hasattr(response, 'text') and response.text:
|
| 70 |
return response.text.strip()
|
|
@@ -90,11 +78,6 @@ def create_alt_text(image):
|
|
| 90 |
if image.mode != 'RGB':
|
| 91 |
image = image.convert('RGB')
|
| 92 |
|
| 93 |
-
# Convert PIL image to bytes
|
| 94 |
-
buffered = io.BytesIO()
|
| 95 |
-
image.save(buffered, format="JPEG")
|
| 96 |
-
image_bytes = buffered.getvalue()
|
| 97 |
-
|
| 98 |
# Create prompt for concise alt text
|
| 99 |
prompt = """You are an accessibility expert creating alt text for images. Analyze this image and provide a clear, concise description suitable for screen readers.
|
| 100 |
|
|
@@ -107,13 +90,7 @@ Provide alt text in 1-2 sentences that is informative but concise. Start directl
|
|
| 107 |
|
| 108 |
# Generate alt text using Gemma 3n via Gemini API
|
| 109 |
model = genai.GenerativeModel('gemma-3n-e4b-it')
|
| 110 |
-
response = model.generate_content([
|
| 111 |
-
types.Part.from_bytes(
|
| 112 |
-
data=image_bytes,
|
| 113 |
-
mime_type='image/jpeg',
|
| 114 |
-
),
|
| 115 |
-
prompt
|
| 116 |
-
])
|
| 117 |
|
| 118 |
if hasattr(response, 'text') and response.text:
|
| 119 |
alt_text = response.text.strip()
|
|
|
|
| 8 |
import io
|
| 9 |
from PIL import Image
|
| 10 |
import google.generativeai as genai
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def initialize_gemini():
|
|
|
|
| 38 |
if image.mode != 'RGB':
|
| 39 |
image = image.convert('RGB')
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# Create prompt for detailed image description
|
| 42 |
prompt = """You are an expert at describing images in detail. Analyze this image and provide a comprehensive description that includes:
|
| 43 |
|
|
|
|
| 52 |
|
| 53 |
# Generate description using Gemma 3n via Gemini API
|
| 54 |
model = genai.GenerativeModel('gemma-3n-e4b-it')
|
| 55 |
+
response = model.generate_content([prompt, image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if hasattr(response, 'text') and response.text:
|
| 58 |
return response.text.strip()
|
|
|
|
| 78 |
if image.mode != 'RGB':
|
| 79 |
image = image.convert('RGB')
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Create prompt for concise alt text
|
| 82 |
prompt = """You are an accessibility expert creating alt text for images. Analyze this image and provide a clear, concise description suitable for screen readers.
|
| 83 |
|
|
|
|
| 90 |
|
| 91 |
# Generate alt text using Gemma 3n via Gemini API
|
| 92 |
model = genai.GenerativeModel('gemma-3n-e4b-it')
|
| 93 |
+
response = model.generate_content([prompt, image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if hasattr(response, 'text') and response.text:
|
| 96 |
alt_text = response.text.strip()
|