Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,15 @@ import json
|
|
| 12 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
| 13 |
|
| 14 |
# Set the OpenAI API Key securely (from Hugging Face secrets)
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# API Call Limits (To Protect Against Abuse)
|
| 18 |
MAX_REQUESTS_PER_SESSION = 3 # Maximum API calls per user session
|
|
@@ -165,26 +173,46 @@ def call_openai_api(messages):
|
|
| 165 |
}
|
| 166 |
|
| 167 |
payload = {
|
| 168 |
-
"model":
|
| 169 |
"messages": messages,
|
| 170 |
"max_tokens": 1500
|
| 171 |
}
|
| 172 |
|
| 173 |
try:
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
except requests.exceptions.RequestException as e:
|
| 186 |
st.error(f"❌ API request failed: {str(e)}")
|
| 187 |
return None
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
# Function to generate home improvement recommendations
|
| 190 |
def generate_recommendations(images, property_type, price_range, timeframe, additional_details, messages):
|
|
|
|
| 12 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
| 13 |
|
| 14 |
# Set the OpenAI API Key securely (from Hugging Face secrets)
|
| 15 |
+
try:
|
| 16 |
+
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
| 17 |
+
except:
|
| 18 |
+
# For testing, can use this but NEVER commit actual keys
|
| 19 |
+
OPENAI_API_KEY = "YOUR_API_KEY_HERE"
|
| 20 |
+
st.warning("⚠️ Using demo mode - API calls will not work. Add your API key to secrets.")
|
| 21 |
+
|
| 22 |
+
# Define which model to use - use a model that supports vision capabilities
|
| 23 |
+
OPENAI_MODEL = "gpt-4-vision-preview" # The correct model name for vision capabilities
|
| 24 |
|
| 25 |
# API Call Limits (To Protect Against Abuse)
|
| 26 |
MAX_REQUESTS_PER_SESSION = 3 # Maximum API calls per user session
|
|
|
|
| 173 |
}
|
| 174 |
|
| 175 |
payload = {
|
| 176 |
+
"model": OPENAI_MODEL,
|
| 177 |
"messages": messages,
|
| 178 |
"max_tokens": 1500
|
| 179 |
}
|
| 180 |
|
| 181 |
try:
|
| 182 |
+
with st.status("Analyzing your home photos..."):
|
| 183 |
+
st.write("Connecting to AI service...")
|
| 184 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
| 185 |
+
|
| 186 |
+
# Check for common error cases
|
| 187 |
+
if response.status_code == 404:
|
| 188 |
+
st.error("❌ API endpoint not found. The model may not be available or the URL is incorrect.")
|
| 189 |
+
return None
|
| 190 |
+
elif response.status_code == 401:
|
| 191 |
+
st.error("❌ Authentication failed. Please check your API key.")
|
| 192 |
+
return None
|
| 193 |
+
elif response.status_code == 429:
|
| 194 |
+
st.error("❌ Rate limit exceeded. Please try again later.")
|
| 195 |
+
return None
|
| 196 |
+
|
| 197 |
+
response.raise_for_status() # Raise exception for other 4XX/5XX status codes
|
| 198 |
+
|
| 199 |
+
st.write("Processing analysis results...")
|
| 200 |
+
response_data = response.json()
|
| 201 |
+
|
| 202 |
+
if response_data and "choices" in response_data:
|
| 203 |
+
st.session_state["api_calls"] += 1
|
| 204 |
+
st.session_state["last_request_time"] = time.time()
|
| 205 |
+
return response_data["choices"][0]["message"]["content"]
|
| 206 |
+
else:
|
| 207 |
+
st.error("❌ API response format unexpected. Please try again.")
|
| 208 |
+
st.write(f"Response: {response_data}")
|
| 209 |
+
return None
|
| 210 |
except requests.exceptions.RequestException as e:
|
| 211 |
st.error(f"❌ API request failed: {str(e)}")
|
| 212 |
return None
|
| 213 |
+
except Exception as e:
|
| 214 |
+
st.error(f"❌ An unexpected error occurred: {str(e)}")
|
| 215 |
+
return None
|
| 216 |
|
| 217 |
# Function to generate home improvement recommendations
|
| 218 |
def generate_recommendations(images, property_type, price_range, timeframe, additional_details, messages):
|