rushankg commited on
Commit
643e149
·
verified ·
1 Parent(s): 2246119

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## (c) 2024
2
+ import streamlit as st
3
+ import requests
4
+ import json
5
+ import os
6
+
7
+ # Get the API key from the environment variable
8
+ API_KEY = os.getenv('PERPLEXITY_API_KEY')
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):
15
+ # Check if API key is available
16
+ if not API_KEY:
17
+ st.error("API key not found. Make sure it's set in the environment variables.")
18
+ return None
19
+
20
+ # Prepare the request payload
21
+ payload = {
22
+ "description": damage_description # Sending the full string as a description
23
+ }
24
+
25
+ # Prepare headers with the API key
26
+ headers = {
27
+ "Content-Type": "application/json",
28
+ "Authorization": f"Bearer {API_KEY}"
29
+ }
30
+
31
+ # Make the API request
32
+ response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
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")
48
+
49
+ # Input textbox for the user to enter the damage description
50
+ damage_description = st.text_area("Enter the damage description:")
51
+
52
+ # Button to trigger the API call
53
+ if st.button("Get Estimated Costs"):
54
+ if damage_description:
55
+ with st.spinner("Fetching cost estimates..."):
56
+ # Call the function to get estimates
57
+ estimated_costs = get_damaged_object_estimates(damage_description)
58
+
59
+ if estimated_costs:
60
+ st.success("Here are the estimated costs:")
61
+ # Display the itemized list with dollar estimates
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:
67
+ st.error("Please enter a damage description before making a request.")