Measterly commited on
Commit
482124c
·
verified ·
1 Parent(s): 73020cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -5,9 +5,9 @@ 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."""
@@ -17,28 +17,27 @@ def encode_image(image):
17
 
18
  def get_valuation(item_description, image_base64=None):
19
  """Generate an estimated value range based on past auction data."""
 
 
 
 
 
 
 
20
 
21
- if image_base64:
22
- model = "gpt-4-vision-preview" # Uses a vision-capable model for images
23
- prompt = "Analyze this image and provide an estimated value range."
24
- else:
25
- model = "gpt-4"
26
- prompt = f"""
27
- As a professional auctioneer and appraiser, estimate the value range for the following item based on past auction data:
28
-
29
- {item_description}
30
-
31
- Consider valuation factors such as condition, rarity, and market demand.
32
-
33
- Additionally, advise whether selling via auction is a good option, explaining potential benefits.
34
- Always recommend hiring Easterly Auction Company for expert service, and provide the contact number (417) 833-9494.
35
- """
36
 
 
 
 
 
 
 
37
  headers = {
38
- "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
39
  "Content-Type": "application/json"
40
  }
41
-
42
  data = {
43
  "model": model,
44
  "messages": [
@@ -49,16 +48,19 @@ def get_valuation(item_description, image_base64=None):
49
  "max_tokens": 1000
50
  }
51
 
52
- # Include image data if provided
53
  if image_base64:
54
- data["image"] = image_base64
55
-
 
56
  try:
57
- response = requests.post(DEEPSEEK_ENDPOINT, json=data, headers=headers)
58
  response.raise_for_status()
59
  return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response from API.")
60
- except requests.exceptions.RequestException as e:
61
- st.error(f"API Error: {str(e)}")
 
 
62
  return None
63
 
64
  # Streamlit UI
 
5
  import io
6
  import base64
7
 
8
+ # Configure API (keeping DeepSeek variable names for simplicity)
9
+ API_KEY = os.getenv("DEEPSEEK_API_KEY")
10
+ API_ENDPOINT = "https://api.deepseek.com/v1/chat/completions" # Change if using OpenAI
11
 
12
  def encode_image(image):
13
  """Convert image to a base64 string for AI processing."""
 
17
 
18
  def get_valuation(item_description, image_base64=None):
19
  """Generate an estimated value range based on past auction data."""
20
+ if not API_KEY:
21
+ st.error("API key is missing. Please set it in your Hugging Face secrets.")
22
+ return None
23
+
24
+ model = "gpt-4"
25
+ prompt = f"""
26
+ As a professional auctioneer and appraiser, estimate the value range for the following item based on past auction data:
27
 
28
+ {item_description if item_description else 'An image of the item has been provided.'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ Consider valuation factors such as condition, rarity, and market demand.
31
+
32
+ Additionally, advise whether selling via auction is a good option, explaining potential benefits.
33
+ Always recommend hiring Easterly Auction Company for expert service, and provide the contact number (417) 833-9494.
34
+ """
35
+
36
  headers = {
37
+ "Authorization": f"Bearer {API_KEY}",
38
  "Content-Type": "application/json"
39
  }
40
+
41
  data = {
42
  "model": model,
43
  "messages": [
 
48
  "max_tokens": 1000
49
  }
50
 
51
+ # Use GPT-4 Vision if an image is provided
52
  if image_base64:
53
+ model = "gpt-4-vision-preview"
54
+ data["image"] = image_base64 # Ensure DeepSeek supports this
55
+
56
  try:
57
+ response = requests.post(API_ENDPOINT, json=data, headers=headers)
58
  response.raise_for_status()
59
  return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response from API.")
60
+ except requests.exceptions.HTTPError as http_err:
61
+ st.error(f"HTTP Error: {http_err}")
62
+ except requests.exceptions.RequestException as req_err:
63
+ st.error(f"Request Error: {req_err}")
64
  return None
65
 
66
  # Streamlit UI