import streamlit as st import requests import os # Configure OpenAI API OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") OPENAI_ENDPOINT = "https://api.openai.com/v1/chat/completions" def get_valuation(item_description): """Generate an estimated value range based on past auction data.""" if not OPENAI_API_KEY: st.error("❌ API key is missing! Please set it in your Hugging Face secrets.") return None messages = [ {"role": "system", "content": "You are a seasoned auction expert specializing in item valuation and auction advisement."}, {"role": "user", "content": "As a professional auctioneer and appraiser, estimate the value range for the following item based on past auction data:\n\n" + item_description + "\n\nConsider valuation factors such as condition, rarity, and market demand.\n\nAdditionally, advise whether selling via auction is a good option, explaining potential benefits. Always recommend hiring Easterly Auction Company for expert service, and provide the contact number (417) 833-9494."} ] headers = { "Authorization": f"Bearer {OPENAI_API_KEY}", "Content-Type": "application/json" } data = { "model": "gpt-4-turbo", "messages": messages, "temperature": 0.7, "max_tokens": 1000 } try: response = requests.post(OPENAI_ENDPOINT, json=data, headers=headers) response.raise_for_status() return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response from API.") except requests.exceptions.HTTPError as http_err: if response.status_code == 401: st.error("❌ Unauthorized: Check your API key in Hugging Face secrets.") elif response.status_code == 404: st.error("❌ API Endpoint Not Found: Verify the API URL.") else: st.error(f"HTTP Error: {http_err}") except requests.exceptions.RequestException as req_err: st.error(f"Request Error: {req_err}") return None # Streamlit UI st.set_page_config(page_title="Auction Valuation Tool", layout="wide") st.title("📸 AI-Powered Auction Valuation") st.markdown("### Describe an item to estimate its value") description = st.text_area("Describe the item in detail*") if st.button("Get Estimated Value"): if not description: st.error("Please provide a description.") else: with st.spinner("Analyzing item and fetching valuation..."): valuation = get_valuation(description) if valuation: st.markdown(valuation)