Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,7 @@ import json
|
|
5 |
import os
|
6 |
|
7 |
# Get the API key from the environment variable
|
8 |
-
API_KEY = os.getenv('
|
9 |
-
|
10 |
-
# Define the API endpoint (same as before)
|
11 |
-
API_URL = "https://api.perplexity.ai/v1/estimate-costs"
|
12 |
|
13 |
# Function to get dollar estimates for damaged objects
|
14 |
def get_damaged_object_estimates(damage_description):
|
@@ -17,31 +14,33 @@ def get_damaged_object_estimates(damage_description):
|
|
17 |
st.error("API key not found. Make sure it's set in the environment variables.")
|
18 |
return None
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
"Content-Type": "application/json",
|
28 |
-
"Authorization": f"Bearer {API_KEY}"
|
29 |
-
}
|
30 |
|
31 |
-
#
|
32 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
# Check for a successful response
|
35 |
-
if response.status_code == 200:
|
36 |
-
# Parse the JSON response
|
37 |
-
response_data = response.json()
|
38 |
-
|
39 |
-
# Return the itemized list with estimated costs
|
40 |
-
return response_data["itemized_list"]
|
41 |
-
else:
|
42 |
-
# Handle errors
|
43 |
-
st.error(f"Error: {response.status_code}, {response.text}")
|
44 |
-
return None
|
45 |
|
46 |
# Streamlit interface
|
47 |
st.title("Disaster Damage Cost Estimator")
|
@@ -58,9 +57,7 @@ if st.button("Get Estimated Costs"):
|
|
58 |
|
59 |
if estimated_costs:
|
60 |
st.success("Here are the estimated costs:")
|
61 |
-
|
62 |
-
for item in estimated_costs:
|
63 |
-
st.write(f"- {item['object']}: ${item['estimated_cost']}")
|
64 |
else:
|
65 |
st.error("Failed to retrieve estimates.")
|
66 |
else:
|
|
|
5 |
import os
|
6 |
|
7 |
# Get the API key from the environment variable
|
8 |
+
API_KEY = os.getenv('PPX_KEY')
|
|
|
|
|
|
|
9 |
|
10 |
# Function to get dollar estimates for damaged objects
|
11 |
def get_damaged_object_estimates(damage_description):
|
|
|
14 |
st.error("API key not found. Make sure it's set in the environment variables.")
|
15 |
return None
|
16 |
|
17 |
+
# Messages to send
|
18 |
+
messages = [
|
19 |
+
{
|
20 |
+
"role": "system",
|
21 |
+
"content": (
|
22 |
+
"You are an artificial intelligence assistant to help hurricane-afflicted homeowners file for insurance."
|
23 |
+
),
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"role": "user",
|
27 |
+
"content": (
|
28 |
+
f"Can you use this general list of damages to my home to prepare an organized, itemized list with dollar estimates that I can submit to my insurance company? \n\n {damage_description}"
|
29 |
+
),
|
30 |
+
},
|
31 |
+
]
|
32 |
|
33 |
+
# instantiate client to make Perplexity requests
|
34 |
+
client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
|
|
|
|
|
|
|
35 |
|
36 |
+
# chat completion without streaming
|
37 |
+
response = client.chat.completions.create(
|
38 |
+
model="llama-3-sonar-large-32k-online",
|
39 |
+
messages=messages,
|
40 |
+
)
|
41 |
+
|
42 |
+
return response
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Streamlit interface
|
46 |
st.title("Disaster Damage Cost Estimator")
|
|
|
57 |
|
58 |
if estimated_costs:
|
59 |
st.success("Here are the estimated costs:")
|
60 |
+
st.write(estimated_costs)
|
|
|
|
|
61 |
else:
|
62 |
st.error("Failed to retrieve estimates.")
|
63 |
else:
|