Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,30 +2,38 @@ import google.generativeai as genai
|
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import PIL.Image
|
|
|
5 |
|
6 |
genai.configure(api_key="AIzaSyAj-b3sO_wUguMdpXWScxKzMHxb8C5cels")
|
7 |
|
8 |
def ImageChat(image, prompt):
|
9 |
-
|
10 |
-
|
11 |
-
model = genai.GenerativeModel("gemini-pro-vision")
|
12 |
-
|
13 |
-
# check image file and convert to a Numpy array
|
14 |
-
if isinstance(image, np.ndarray):
|
15 |
-
|
16 |
img = PIL.Image.fromarray(image)
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
app.launch()
|
|
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import PIL.Image
|
5 |
+
import io
|
6 |
|
7 |
genai.configure(api_key="AIzaSyAj-b3sO_wUguMdpXWScxKzMHxb8C5cels")
|
8 |
|
9 |
def ImageChat(image, prompt):
|
10 |
+
# Check image file and convert to a PIL Image object
|
11 |
+
if isinstance(image, np.ndarray):
|
|
|
|
|
|
|
|
|
|
|
12 |
img = PIL.Image.fromarray(image)
|
13 |
+
else:
|
14 |
+
try:
|
15 |
+
img = PIL.Image.open(image)
|
16 |
+
except (AttributeError, IOError) as e:
|
17 |
+
return f"Invalid image provided. Please provide a valid image file. Error: {e}"
|
18 |
+
|
19 |
+
# Load model
|
20 |
+
model = genai.GenerativeModel("gemini-pro-vision")
|
21 |
+
|
22 |
+
# Generate response
|
23 |
+
try:
|
24 |
+
response = model.generate_content([prompt, img])
|
25 |
+
if not response or not response.text:
|
26 |
+
return "No valid response received. The response might have been blocked."
|
27 |
+
return response.text
|
28 |
+
except ValueError as e:
|
29 |
+
return f"Error in generating response: {e}"
|
30 |
+
|
31 |
+
app = gr.Interface(
|
32 |
+
fn=ImageChat,
|
33 |
+
inputs=[gr.Image(label="Image"), gr.Text(label="Prompt")],
|
34 |
+
outputs=gr.Text(label="Response"),
|
35 |
+
title="Image Chat",
|
36 |
+
theme=gr.themes.Soft()
|
37 |
+
)
|
38 |
|
39 |
app.launch()
|