## (c) 2024 import streamlit as st import requests import json import os # Get the API key from the environment variable API_KEY = os.getenv('PERPLEXITY_API_KEY') # Define the API endpoint (same as before) API_URL = "https://api.perplexity.ai/v1/estimate-costs" # Function to get dollar estimates for damaged objects def get_damaged_object_estimates(damage_description): # Check if API key is available if not API_KEY: st.error("API key not found. Make sure it's set in the environment variables.") return None # Prepare the request payload payload = { "description": damage_description # Sending the full string as a description } # Prepare headers with the API key headers = { "Content-Type": "application/json", "Authorization": f"Bearer {API_KEY}" } # Make the API request response = requests.post(API_URL, headers=headers, data=json.dumps(payload)) # Check for a successful response if response.status_code == 200: # Parse the JSON response response_data = response.json() # Return the itemized list with estimated costs return response_data["itemized_list"] else: # Handle errors st.error(f"Error: {response.status_code}, {response.text}") return None # Streamlit interface st.title("Disaster Damage Cost Estimator") # Input textbox for the user to enter the damage description damage_description = st.text_area("Enter the damage description:") # Button to trigger the API call if st.button("Get Estimated Costs"): if damage_description: with st.spinner("Fetching cost estimates..."): # Call the function to get estimates estimated_costs = get_damaged_object_estimates(damage_description) if estimated_costs: st.success("Here are the estimated costs:") # Display the itemized list with dollar estimates for item in estimated_costs: st.write(f"- {item['object']}: ${item['estimated_cost']}") else: st.error("Failed to retrieve estimates.") else: st.error("Please enter a damage description before making a request.")