Update app.py
Browse files
app.py
CHANGED
|
@@ -5,20 +5,9 @@ from PIL import Image
|
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
|
| 8 |
-
# Configure API
|
| 9 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Check which API key is available
|
| 13 |
-
if OPENAI_API_KEY:
|
| 14 |
-
API_KEY = OPENAI_API_KEY
|
| 15 |
-
API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
|
| 16 |
-
elif DEEPSEEK_API_KEY:
|
| 17 |
-
API_KEY = DEEPSEEK_API_KEY
|
| 18 |
-
API_ENDPOINT = "https://api.deepseek.com/v1/chat/completions"
|
| 19 |
-
else:
|
| 20 |
-
API_KEY = None
|
| 21 |
-
API_ENDPOINT = None
|
| 22 |
|
| 23 |
def encode_image(image):
|
| 24 |
"""Convert image to a base64 string for AI processing."""
|
|
@@ -33,8 +22,7 @@ def encode_image(image):
|
|
| 33 |
|
| 34 |
def get_valuation(item_description, image_base64=None):
|
| 35 |
"""Generate an estimated value range based on past auction data."""
|
| 36 |
-
|
| 37 |
-
if not API_KEY or not API_ENDPOINT:
|
| 38 |
st.error("❌ API key is missing! Please set it in your Hugging Face secrets.")
|
| 39 |
return None
|
| 40 |
|
|
@@ -48,14 +36,14 @@ def get_valuation(item_description, image_base64=None):
|
|
| 48 |
Additionally, advise whether selling via auction is a good option, explaining potential benefits.
|
| 49 |
Always recommend hiring Easterly Auction Company for expert service, and provide the contact number (417) 833-9494.
|
| 50 |
"""
|
| 51 |
-
|
| 52 |
headers = {
|
| 53 |
-
"Authorization": f"Bearer {
|
| 54 |
"Content-Type": "application/json"
|
| 55 |
}
|
| 56 |
-
|
| 57 |
data = {
|
| 58 |
-
"model": "gpt-4"
|
| 59 |
"messages": [
|
| 60 |
{"role": "system", "content": "You are a seasoned auction expert specializing in item valuation and auction advisement."},
|
| 61 |
{"role": "user", "content": prompt}
|
|
@@ -63,9 +51,9 @@ def get_valuation(item_description, image_base64=None):
|
|
| 63 |
"temperature": 0.7,
|
| 64 |
"max_tokens": 1000
|
| 65 |
}
|
| 66 |
-
|
| 67 |
try:
|
| 68 |
-
response = requests.post(
|
| 69 |
response.raise_for_status()
|
| 70 |
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response from API.")
|
| 71 |
except requests.exceptions.HTTPError as http_err:
|
|
@@ -99,7 +87,7 @@ elif input_method == "Upload an image":
|
|
| 99 |
image = Image.open(uploaded_image)
|
| 100 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 101 |
description = st.text_area("(Optional) Add additional details about the item")
|
| 102 |
-
|
| 103 |
# Encode image for AI processing
|
| 104 |
image_base64 = encode_image(image)
|
| 105 |
|
|
@@ -110,4 +98,4 @@ if st.button("Get Estimated Value"):
|
|
| 110 |
with st.spinner("Analyzing item and fetching valuation..."):
|
| 111 |
valuation = get_valuation(description, image_base64)
|
| 112 |
if valuation:
|
| 113 |
-
st.markdown(valuation)
|
|
|
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
|
| 8 |
+
# Configure OpenAI API
|
| 9 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
OPENAI_ENDPOINT = "https://api.openai.com/v1/chat/completions"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def encode_image(image):
|
| 13 |
"""Convert image to a base64 string for AI processing."""
|
|
|
|
| 22 |
|
| 23 |
def get_valuation(item_description, image_base64=None):
|
| 24 |
"""Generate an estimated value range based on past auction data."""
|
| 25 |
+
if not OPENAI_API_KEY:
|
|
|
|
| 26 |
st.error("❌ API key is missing! Please set it in your Hugging Face secrets.")
|
| 27 |
return None
|
| 28 |
|
|
|
|
| 36 |
Additionally, advise whether selling via auction is a good option, explaining potential benefits.
|
| 37 |
Always recommend hiring Easterly Auction Company for expert service, and provide the contact number (417) 833-9494.
|
| 38 |
"""
|
| 39 |
+
|
| 40 |
headers = {
|
| 41 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 42 |
"Content-Type": "application/json"
|
| 43 |
}
|
| 44 |
+
|
| 45 |
data = {
|
| 46 |
+
"model": "gpt-4",
|
| 47 |
"messages": [
|
| 48 |
{"role": "system", "content": "You are a seasoned auction expert specializing in item valuation and auction advisement."},
|
| 49 |
{"role": "user", "content": prompt}
|
|
|
|
| 51 |
"temperature": 0.7,
|
| 52 |
"max_tokens": 1000
|
| 53 |
}
|
| 54 |
+
|
| 55 |
try:
|
| 56 |
+
response = requests.post(OPENAI_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:
|
|
|
|
| 87 |
image = Image.open(uploaded_image)
|
| 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 |
# Encode image for AI processing
|
| 92 |
image_base64 = encode_image(image)
|
| 93 |
|
|
|
|
| 98 |
with st.spinner("Analyzing item and fetching valuation..."):
|
| 99 |
valuation = get_valuation(description, image_base64)
|
| 100 |
if valuation:
|
| 101 |
+
st.markdown(valuation)
|