Spaces:
Sleeping
Sleeping
File size: 3,226 Bytes
643e149 87a0931 643e149 75c7833 643e149 2a98bf0 643e149 75c7833 643e149 2a98bf0 643e149 2a98bf0 75c7833 2a98bf0 643e149 42fa404 2a98bf0 42fa404 d29bc99 42fa404 2a98bf0 42fa404 2787fcd 42fa404 643e149 42fa404 643e149 75c7833 643e149 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
## (c) 2024
import streamlit as st
import requests
import json
import os
from openai import OpenAI
# Get the API key from the environment variable
API_KEY = os.getenv('PPX_KEY')
# Function to get dollar estimates for damaged objects
# 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
# Messages to send
messages = [
{
"role": "system",
"content": (
"You are an artificial intelligence assistant to help hurricane-afflicted homeowners file for insurance."
),
},
{
"role": "user",
"content": (
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}"
),
},
]
# Prepare the request payload
payload = {
"model": "llama-3.1-sonar-small-128k-chat",
"messages": messages,
}
# Make a POST request to the Perplexity API
response = requests.post(
"https://api.perplexity.ai/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json=payload
)
# Check if the request was successful
if response.status_code != 200:
st.error(f"Error: {response.status_code} - {response.text}")
return None
# Process the response
return postprocess_message_text(response.json())
def postprocess_message_text(api_response):
"""
Extracts and formats the message content from the API response JSON.
Args:
api_response (dict): JSON response from the API.
Returns:
str: Formatted message content.
"""
st.error(api_response)
# Extract the content from the choices
message_content = api_response["choices"][0]["message"]["content"]
# Clean the message content (remove unnecessary escape characters)
formatted_content = message_content.replace("\\n", "\n")
# Fix dollar sign markdown
formatted_content = formatted_content.replace("$", "\\$")
return formatted_content
# 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 = postprocess_message_text(get_damaged_object_estimates(damage_description))
if estimated_costs:
st.success("Here are the estimated costs:")
st.write(estimated_costs)
else:
st.error("Failed to retrieve estimates.")
else:
st.error("Please enter a damage description before making a request.")
|