Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,27 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import google.generativeai as genai
|
3 |
from PIL import Image
|
4 |
-
import os
|
5 |
-
import io
|
6 |
|
7 |
# Configure the Gemini API
|
8 |
-
genai.configure(api_key=os.environ
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
generation_config = {
|
12 |
-
"temperature":
|
13 |
"top_p": 0.95,
|
14 |
"top_k": 64,
|
15 |
-
"max_output_tokens":
|
16 |
"response_mime_type": "text/plain",
|
17 |
}
|
18 |
|
@@ -21,29 +30,42 @@ model = genai.GenerativeModel(
|
|
21 |
generation_config=generation_config,
|
22 |
)
|
23 |
|
24 |
-
def image_to_byte_array(image: Image) -> bytes:
|
25 |
-
imgByteArr = io.BytesIO()
|
26 |
-
image.save(imgByteArr, format=image.format)
|
27 |
-
imgByteArr = imgByteArr.getvalue()
|
28 |
-
return imgByteArr
|
29 |
-
|
30 |
def chat_with_gemini(history, user_message, image):
|
31 |
history = history or []
|
32 |
|
33 |
try:
|
34 |
-
if image
|
35 |
-
|
36 |
-
|
37 |
-
#
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
else:
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
response_text = response.text
|
46 |
-
|
47 |
history.append((user_message, response_text))
|
48 |
except Exception as e:
|
49 |
error_message = f"An error occurred: {str(e)}"
|
|
|
1 |
+
import os
|
2 |
+
import io
|
3 |
import gradio as gr
|
4 |
import google.generativeai as genai
|
5 |
from PIL import Image
|
|
|
|
|
6 |
|
7 |
# Configure the Gemini API
|
8 |
+
genai.configure(api_key=os.environ["AIzaSyCFdxcKVO6VSxEBaNE2W3LIvRLPEPpyMGw"])
|
9 |
|
10 |
+
def upload_to_gemini(image):
|
11 |
+
"""Uploads the given image to Gemini."""
|
12 |
+
if image is None:
|
13 |
+
return None
|
14 |
+
image_byte_array = io.BytesIO()
|
15 |
+
image.save(image_byte_array, format='JPEG')
|
16 |
+
image_byte_array = image_byte_array.getvalue()
|
17 |
+
return genai.upload_file(image_byte_array, mime_type="image/jpeg")
|
18 |
+
|
19 |
+
# Create the model
|
20 |
generation_config = {
|
21 |
+
"temperature": 0.9,
|
22 |
"top_p": 0.95,
|
23 |
"top_k": 64,
|
24 |
+
"max_output_tokens": 1024,
|
25 |
"response_mime_type": "text/plain",
|
26 |
}
|
27 |
|
|
|
30 |
generation_config=generation_config,
|
31 |
)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def chat_with_gemini(history, user_message, image):
|
34 |
history = history or []
|
35 |
|
36 |
try:
|
37 |
+
uploaded_image = upload_to_gemini(image) if image else None
|
38 |
+
|
39 |
+
if not history:
|
40 |
+
# Start a new chat session
|
41 |
+
chat_session = model.start_chat(
|
42 |
+
history=[
|
43 |
+
{
|
44 |
+
"role": "user",
|
45 |
+
"parts": [
|
46 |
+
uploaded_image,
|
47 |
+
user_message,
|
48 |
+
] if uploaded_image else [user_message],
|
49 |
+
},
|
50 |
+
]
|
51 |
+
)
|
52 |
else:
|
53 |
+
# Continue existing chat session
|
54 |
+
chat_session = model.start_chat(history=[
|
55 |
+
{"role": "user" if i % 2 == 0 else "model", "parts": [msg]}
|
56 |
+
for i, (msg, _) in enumerate(history)
|
57 |
+
])
|
58 |
+
|
59 |
+
# Send the new message
|
60 |
+
if uploaded_image:
|
61 |
+
chat_session.send_message([uploaded_image, user_message])
|
62 |
+
else:
|
63 |
+
chat_session.send_message(user_message)
|
64 |
+
|
65 |
+
# Get the response
|
66 |
+
response = chat_session.last
|
67 |
response_text = response.text
|
68 |
+
|
69 |
history.append((user_message, response_text))
|
70 |
except Exception as e:
|
71 |
error_message = f"An error occurred: {str(e)}"
|