ps1221/user-car-agent
0
1import gradio as gr2import requests3import json4import os5 6# ===== CONFIG =====7OPENAI_KEY = os.getenv("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") # set in Secrets if using OpenAI8MODEL = "gpt-3.5-turbo" # or your HF model9OPENAI_URL = "https://api.openai.com/v1/chat/completions"10 11SYSTEM_PROMPT = """12You are a used-car advisor. 13Always respond with valid JSON ONLY:14{15 "recommendation": "<buy|negotiate|skip_for_now|inspect_first>",16 "predicted_price": <integer>,17 "confidence": "<low|medium|high>",18 "reason_short": "<1 sentence>",19 "percent_diff_from_asking": <float>,20 "inspection_checklist": ["item1", "item2"],21 "negotiation_script": "<short paragraph>",22 "disclaimer": "Advisory estimate only. Verify condition with a mechanic."23}24Do not add extra commentary.25"""26 27def evaluate_car(make, model, year, mileage, asking_price, location, condition_text, num_owners):28 # Build GPT prompt29 user_prompt = f"""30Car details:31Make: {make}32Model: {model}33Year: {year}34Mileage: {mileage}35Asking Price: {asking_price}36Location: {location or 'Not provided'}37Condition: {condition_text or 'Not provided'}38Number of owners: {num_owners or 'Not provided'}39 40User question: Should I buy this car?41"""42 headers = {43 "Authorization": f"Bearer {OPENAI_KEY}",44 "Content-Type": "application/json"45 }46 try:47 r = requests.post(OPENAI_URL, headers=headers,48 json={"model": MODEL, "messages": [{"role": "system", "content": SYSTEM_PROMPT},49 {"role": "user", "content": user_prompt}],50 "temperature": 0})51 r.raise_for_status()52 content = r.json()["choices"][0]["message"]["content"].strip()53 return content54 except Exception as e:55 return json.dumps({56 "recommendation": "inspect_first",57 "predicted_price": 0,58 "confidence": "low",59 "reason_short": f"GPT call failed: {str(e)}",60 "percent_diff_from_asking": 0.0,61 "inspection_checklist": [],62 "negotiation_script": "",63 "disclaimer": "Advisory estimate only. Verify condition with a mechanic."64 })65 66# ===== GRADIO UI =====67demo = gr.Interface(68 fn=evaluate_car,69 inputs=[70 gr.Textbox(label="Make"),71 gr.Textbox(label="Model"),72 gr.Number(label="Year"),73 gr.Number(label="Mileage (km)"),74 gr.Number(label="Asking Price"),75 gr.Textbox(label="Location"),76 gr.Textbox(label="Condition"),77 gr.Number(label="Num Owners")78 ],79 outputs=gr.Textbox(label="GPT Agent Response (JSON)"),80 title="Used Car Advisor",81 description="Enter car details and get recommendation, estimated price, and inspection checklist."82)83 84demo.launch()85 