Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,25 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
import base64
|
5 |
|
6 |
-
# Set
|
7 |
-
|
8 |
|
9 |
def generate_caption(image):
|
10 |
-
"""Generate a caption for the uploaded image using
|
11 |
-
|
12 |
-
# Convert image to Base64
|
13 |
-
with open(image, "rb") as img_file:
|
14 |
-
img_base64 = base64.b64encode(img_file.read()).decode("utf-8")
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
{"role": "system", "content": "You are an AI that describes images accurately."},
|
21 |
-
{
|
22 |
-
"role": "user",
|
23 |
-
"content": [
|
24 |
-
{"type": "text", "text": "Describe this image in detail."},
|
25 |
-
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}},
|
26 |
-
],
|
27 |
-
},
|
28 |
-
],
|
29 |
-
max_tokens=100
|
30 |
-
)
|
31 |
|
32 |
-
#
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
return caption
|
35 |
|
36 |
# Gradio UI
|
@@ -38,8 +27,8 @@ iface = gr.Interface(
|
|
38 |
fn=generate_caption,
|
39 |
inputs=gr.Image(type="filepath"),
|
40 |
outputs="text",
|
41 |
-
title="Image Captioning App",
|
42 |
-
description="Upload an image, and the AI will generate a descriptive caption."
|
43 |
)
|
44 |
|
45 |
# Run the app
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
import gradio as gr
|
3 |
+
from PIL import Image
|
|
|
4 |
|
5 |
+
# Set up Gemini API key
|
6 |
+
genai.configure(api_key="AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI") # Replace with your API key
|
7 |
|
8 |
def generate_caption(image):
|
9 |
+
"""Generate a caption for the uploaded image using Google Gemini Pro Vision API."""
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
model = genai.GenerativeModel("gemini-pro-vision")
|
12 |
+
|
13 |
+
# Open image using PIL
|
14 |
+
img = Image.open(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Generate caption
|
17 |
+
response = model.generate_content(
|
18 |
+
[img, "Describe the contents of this image in detail."]
|
19 |
+
)
|
20 |
+
|
21 |
+
# Extract caption
|
22 |
+
caption = response.text
|
23 |
return caption
|
24 |
|
25 |
# Gradio UI
|
|
|
27 |
fn=generate_caption,
|
28 |
inputs=gr.Image(type="filepath"),
|
29 |
outputs="text",
|
30 |
+
title="Free Image Captioning App",
|
31 |
+
description="Upload an image, and the AI (Google Gemini) will generate a descriptive caption."
|
32 |
)
|
33 |
|
34 |
# Run the app
|