Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ import json
|
|
| 12 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
| 13 |
|
| 14 |
# Set the OpenAI API Key securely (from Hugging Face secrets)
|
| 15 |
-
OPENAI_API_KEY = st.secrets["
|
| 16 |
|
| 17 |
# API Call Limits (To Protect Against Abuse)
|
| 18 |
MAX_REQUESTS_PER_SESSION = 3 # Maximum API calls per user session
|
|
@@ -133,6 +133,14 @@ initial_messages = [{
|
|
| 133 |
|
| 134 |
# Function to encode images to base64 for API transmission
|
| 135 |
def encode_image_to_base64(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
buffered = BytesIO()
|
| 137 |
image.save(buffered, format="JPEG")
|
| 138 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
|
|
|
| 12 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
| 13 |
|
| 14 |
# Set the OpenAI API Key securely (from Hugging Face secrets)
|
| 15 |
+
OPENAI_API_KEY = st.secrets["YOUR_OPENAI_API_KEY"]
|
| 16 |
|
| 17 |
# API Call Limits (To Protect Against Abuse)
|
| 18 |
MAX_REQUESTS_PER_SESSION = 3 # Maximum API calls per user session
|
|
|
|
| 133 |
|
| 134 |
# Function to encode images to base64 for API transmission
|
| 135 |
def encode_image_to_base64(image):
|
| 136 |
+
# Convert RGBA images to RGB for JPEG compatibility
|
| 137 |
+
if image.mode == 'RGBA':
|
| 138 |
+
# Create a white background image
|
| 139 |
+
background = Image.new('RGB', image.size, (255, 255, 255))
|
| 140 |
+
# Paste the image on the background using the alpha channel
|
| 141 |
+
background.paste(image, (0, 0), image)
|
| 142 |
+
image = background
|
| 143 |
+
|
| 144 |
buffered = BytesIO()
|
| 145 |
image.save(buffered, format="JPEG")
|
| 146 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|