Measterly commited on
Commit
c1b117f
·
verified ·
1 Parent(s): 32f14d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -5,15 +5,26 @@ from PIL import Image
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
 
@@ -22,7 +33,8 @@ def encode_image(image):
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
28
 
@@ -43,7 +55,7 @@ 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}
@@ -88,7 +100,7 @@ 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
- # Convert image to RGB mode before encoding
92
  image_base64 = encode_image(image)
93
 
94
  if st.button("Get Estimated Value"):
 
5
  import io
6
  import base64
7
 
8
+ # Configure API (Keeping DeepSeek for simplicity, but defaulting to OpenAI)
9
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
10
+ DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
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."""
25
  img_bytes = io.BytesIO()
26
 
27
+ # Convert RGBA images to RGB before saving
28
  if image.mode != "RGB":
29
  image = image.convert("RGB")
30
 
 
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
 
 
55
  }
56
 
57
  data = {
58
+ "model": "gpt-4" if "openai" in API_ENDPOINT else "deepseek-model", # Adjust for DeepSeek
59
  "messages": [
60
  {"role": "system", "content": "You are a seasoned auction expert specializing in item valuation and auction advisement."},
61
  {"role": "user", "content": prompt}
 
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
 
106
  if st.button("Get Estimated Value"):