Measterly commited on
Commit
9b22d73
·
verified ·
1 Parent(s): d3d1cd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -20
app.py CHANGED
@@ -20,22 +20,19 @@ def encode_image(image):
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 OPENAI_API_KEY:
26
  st.error("❌ API key is missing! Please set it in your Hugging Face secrets.")
27
  return None
28
 
29
- prompt = f"""
30
- As a professional auctioneer and appraiser, estimate the value range for the following item based on past auction data:
 
 
31
 
32
- {item_description if item_description else 'An image of the item has been provided.'}
33
-
34
- Consider valuation factors such as condition, rarity, and market demand.
35
-
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}",
@@ -43,11 +40,8 @@ def get_valuation(item_description, image_base64=None):
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}
50
- ],
51
  "temperature": 0.7,
52
  "max_tokens": 1000
53
  }
@@ -76,7 +70,7 @@ st.markdown("### Upload an image or describe an item to estimate its value")
76
  input_method = st.radio("Choose input method:", ["Describe the item", "Upload an image"])
77
 
78
  description = ""
79
- image_base64 = None
80
 
81
  if input_method == "Describe the item":
82
  description = st.text_area("Describe the item in detail*")
@@ -88,14 +82,16 @@ elif input_method == "Upload an 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
 
94
  if st.button("Get Estimated Value"):
95
- if not description and not image_base64:
96
  st.error("Please provide a description or upload an image.")
97
  else:
98
  with st.spinner("Analyzing item and fetching valuation..."):
99
- valuation = get_valuation(description, image_base64)
100
  if valuation:
101
  st.markdown(valuation)
 
20
  image.save(img_bytes, format="JPEG")
21
  return base64.b64encode(img_bytes.getvalue()).decode()
22
 
23
+ def get_valuation(item_description, image=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
 
29
+ messages = [
30
+ {"role": "system", "content": "You are a seasoned auction expert specializing in item valuation and auction advisement."},
31
+ {"role": "user", "content": "As a professional auctioneer and appraiser, estimate the value range for the following item based on past auction data:\n\n" + item_description + "\n\nConsider valuation factors such as condition, rarity, and market demand.\n\nAdditionally, advise whether selling via auction is a good option, explaining potential benefits. Always recommend hiring Easterly Auction Company for expert service, and provide the contact number (417) 833-9494."}
32
+ ]
33
 
34
+ if image:
35
+ messages.append({"role": "user", "content": {"type": "image_url", "image_url": image}})
 
 
 
 
 
36
 
37
  headers = {
38
  "Authorization": f"Bearer {OPENAI_API_KEY}",
 
40
  }
41
 
42
  data = {
43
+ "model": "gpt-4-turbo",
44
+ "messages": messages,
 
 
 
45
  "temperature": 0.7,
46
  "max_tokens": 1000
47
  }
 
70
  input_method = st.radio("Choose input method:", ["Describe the item", "Upload an image"])
71
 
72
  description = ""
73
+ image_url = None
74
 
75
  if input_method == "Describe the item":
76
  description = st.text_area("Describe the item in detail*")
 
82
  st.image(image, caption="Uploaded Image", use_column_width=True)
83
  description = st.text_area("(Optional) Add additional details about the item")
84
 
85
+ # Save image to a temporary file and generate a URL
86
+ image_path = f"temp_{uploaded_image.name}"
87
+ image.save(image_path)
88
+ image_url = f"file://{image_path}" # Simulating an image URL for OpenAI Vision model
89
 
90
  if st.button("Get Estimated Value"):
91
+ if not description and not image_url:
92
  st.error("Please provide a description or upload an image.")
93
  else:
94
  with st.spinner("Analyzing item and fetching valuation..."):
95
+ valuation = get_valuation(description, image_url)
96
  if valuation:
97
  st.markdown(valuation)