Measterly commited on
Commit
1e2881b
·
verified ·
1 Parent(s): 1a3f548

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -3,17 +3,24 @@ import requests
3
  import os
4
  from PIL import Image
5
  import io
 
6
 
7
  # Configure OpenAI API (keeping DeepSeek variable names for simplicity)
8
  DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
9
  DEEPSEEK_ENDPOINT = "https://api.openai.com/v1/chat/completions"
10
 
11
- def get_valuation(item_description):
 
 
 
 
 
 
12
  """Generate an estimated value range based on past auction data."""
13
  prompt = f"""
14
  As a professional auctioneer and appraiser, estimate the value range for the following item based on past auction data:
15
 
16
- {item_description}
17
 
18
  Consider valuation factors such as condition, rarity, and market demand.
19
 
@@ -36,6 +43,10 @@ def get_valuation(item_description):
36
  "max_tokens": 1000
37
  }
38
 
 
 
 
 
39
  try:
40
  response = requests.post(DEEPSEEK_ENDPOINT, json=data, headers=headers)
41
  response.raise_for_status()
@@ -53,6 +64,8 @@ st.markdown("### Upload an image or describe an item to estimate its value")
53
  input_method = st.radio("Choose input method:", ["Describe the item", "Upload an image"])
54
 
55
  description = ""
 
 
56
  if input_method == "Describe the item":
57
  description = st.text_area("Describe the item in detail*")
58
 
@@ -67,17 +80,14 @@ elif input_method == "Upload an image":
67
  if image.mode == "RGBA":
68
  image = image.convert("RGB")
69
 
70
- img_bytes = io.BytesIO()
71
- image.save(img_bytes, format="JPEG")
72
- img_bytes = img_bytes.getvalue()
73
- # Image processing could be added here in the future
74
 
75
  if st.button("Get Estimated Value"):
76
- if not description:
77
  st.error("Please provide a description or upload an image.")
78
  else:
79
  with st.spinner("Analyzing item and fetching valuation..."):
80
- valuation = get_valuation(description)
81
  if valuation:
82
  st.markdown(valuation)
83
-
 
3
  import os
4
  from PIL import Image
5
  import io
6
+ import base64
7
 
8
  # Configure OpenAI API (keeping DeepSeek variable names for simplicity)
9
  DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
10
  DEEPSEEK_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
  prompt = f"""
21
  As a professional auctioneer and appraiser, estimate the value range for the following item based on past auction data:
22
 
23
+ {item_description if item_description else 'An image of the item has been provided.'}
24
 
25
  Consider valuation factors such as condition, rarity, and market demand.
26
 
 
43
  "max_tokens": 1000
44
  }
45
 
46
+ # If an image is provided, include it in the request (future AI models could process it)
47
+ if image_base64:
48
+ data["image"] = image_base64
49
+
50
  try:
51
  response = requests.post(DEEPSEEK_ENDPOINT, json=data, headers=headers)
52
  response.raise_for_status()
 
64
  input_method = st.radio("Choose input method:", ["Describe the item", "Upload an image"])
65
 
66
  description = ""
67
+ image_base64 = None
68
+
69
  if input_method == "Describe the item":
70
  description = st.text_area("Describe the item in detail*")
71
 
 
80
  if image.mode == "RGBA":
81
  image = image.convert("RGB")
82
 
83
+ # Encode image for AI processing
84
+ image_base64 = encode_image(image)
 
 
85
 
86
  if st.button("Get Estimated Value"):
87
+ if not description and not image_base64:
88
  st.error("Please provide a description or upload an image.")
89
  else:
90
  with st.spinner("Analyzing item and fetching valuation..."):
91
+ valuation = get_valuation(description, image_base64)
92
  if valuation:
93
  st.markdown(valuation)