Update app.py
Browse files
app.py
CHANGED
|
@@ -5,19 +5,23 @@ from PIL import Image
|
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
|
| 8 |
-
# Configure API (
|
| 9 |
-
API_KEY = os.getenv("DEEPSEEK_API_KEY") #
|
| 10 |
-
API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
|
| 11 |
|
| 12 |
def encode_image(image):
|
| 13 |
"""Convert image to a base64 string for AI processing."""
|
| 14 |
img_bytes = io.BytesIO()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
image.save(img_bytes, format="JPEG")
|
| 16 |
return base64.b64encode(img_bytes.getvalue()).decode()
|
| 17 |
|
| 18 |
def get_valuation(item_description, image_base64=None):
|
| 19 |
"""Generate an estimated value range based on past auction data."""
|
| 20 |
-
|
| 21 |
if not API_KEY:
|
| 22 |
st.error("❌ API key is missing! Please set it in your Hugging Face secrets.")
|
| 23 |
return None
|
|
@@ -48,16 +52,17 @@ def get_valuation(item_description, image_base64=None):
|
|
| 48 |
"max_tokens": 1000
|
| 49 |
}
|
| 50 |
|
| 51 |
-
# Ensure DeepSeek supports image inputs before using it
|
| 52 |
-
if image_base64:
|
| 53 |
-
data["image"] = image_base64
|
| 54 |
-
|
| 55 |
try:
|
| 56 |
response = requests.post(API_ENDPOINT, json=data, headers=headers)
|
| 57 |
response.raise_for_status()
|
| 58 |
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response from API.")
|
| 59 |
except requests.exceptions.HTTPError as http_err:
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
except requests.exceptions.RequestException as req_err:
|
| 62 |
st.error(f"Request Error: {req_err}")
|
| 63 |
return None
|
|
@@ -83,11 +88,7 @@ elif input_method == "Upload an image":
|
|
| 83 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 84 |
description = st.text_area("(Optional) Add additional details about the item")
|
| 85 |
|
| 86 |
-
# Convert image to RGB mode before
|
| 87 |
-
if image.mode == "RGBA":
|
| 88 |
-
image = image.convert("RGB")
|
| 89 |
-
|
| 90 |
-
# Encode image for AI processing
|
| 91 |
image_base64 = encode_image(image)
|
| 92 |
|
| 93 |
if st.button("Get Estimated Value"):
|
|
|
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
|
| 8 |
+
# Configure OpenAI API (keeping DeepSeek variable names for simplicity)
|
| 9 |
+
API_KEY = os.getenv("DEEPSEEK_API_KEY") # Ensure this is set in Hugging Face secrets
|
| 10 |
+
API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
|
| 11 |
|
| 12 |
def encode_image(image):
|
| 13 |
"""Convert image to a base64 string for AI processing."""
|
| 14 |
img_bytes = io.BytesIO()
|
| 15 |
+
|
| 16 |
+
# Convert to RGB to avoid RGBA-related errors
|
| 17 |
+
if image.mode != "RGB":
|
| 18 |
+
image = image.convert("RGB")
|
| 19 |
+
|
| 20 |
image.save(img_bytes, format="JPEG")
|
| 21 |
return base64.b64encode(img_bytes.getvalue()).decode()
|
| 22 |
|
| 23 |
def get_valuation(item_description, image_base64=None):
|
| 24 |
"""Generate an estimated value range based on past auction data."""
|
|
|
|
| 25 |
if not API_KEY:
|
| 26 |
st.error("❌ API key is missing! Please set it in your Hugging Face secrets.")
|
| 27 |
return None
|
|
|
|
| 52 |
"max_tokens": 1000
|
| 53 |
}
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
response = requests.post(API_ENDPOINT, json=data, headers=headers)
|
| 57 |
response.raise_for_status()
|
| 58 |
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response from API.")
|
| 59 |
except requests.exceptions.HTTPError as http_err:
|
| 60 |
+
if response.status_code == 401:
|
| 61 |
+
st.error("❌ Unauthorized: Check your API key in Hugging Face secrets.")
|
| 62 |
+
elif response.status_code == 404:
|
| 63 |
+
st.error("❌ API Endpoint Not Found: Verify the API URL.")
|
| 64 |
+
else:
|
| 65 |
+
st.error(f"HTTP Error: {http_err}")
|
| 66 |
except requests.exceptions.RequestException as req_err:
|
| 67 |
st.error(f"Request Error: {req_err}")
|
| 68 |
return None
|
|
|
|
| 88 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 89 |
description = st.text_area("(Optional) Add additional details about the item")
|
| 90 |
|
| 91 |
+
# Convert image to RGB mode before encoding
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
image_base64 = encode_image(image)
|
| 93 |
|
| 94 |
if st.button("Get Estimated Value"):
|