|
|
|
import os |
|
from PIL import Image |
|
import torch |
|
from transformers import AutoImageProcessor, AutoModelForImageClassification |
|
import gradio as gr |
|
import openai |
|
|
|
|
|
model_name = "beingamit99/car_damage_detection" |
|
processor = AutoImageProcessor.from_pretrained(model_name) |
|
model = AutoModelForImageClassification.from_pretrained(model_name) |
|
|
|
|
|
openai_api_key = os.getenv("OpenAI4oMini") |
|
|
|
|
|
if openai_api_key is None: |
|
raise ValueError("OpenAI API key is not set. Make sure to set the OpenAI4oMini secret in Hugging Face.") |
|
|
|
|
|
client = openai.OpenAI(api_key=openai_api_key) |
|
|
|
|
|
car_companies = ["Select", "Toyota", "Honda", "Ford", "BMW", "Mercedes", "Audi", "Hyundai", "Kia", "Nissan"] |
|
car_models = [ |
|
"Select", |
|
"Corolla", "Camry", "RAV4", "Highlander", |
|
"Civic", "Accord", "CR-V", "Pilot", |
|
"Fiesta", "Focus", "Explorer", "Mustang", |
|
"3 Series", "5 Series", "X3", "X5", |
|
"C-Class", "E-Class", "GLC", "GLE", |
|
"A3", "A4", "Q5", "Q7", |
|
"Elantra", "Sonata", "Tucson", "Santa Fe", |
|
"Rio", "Optima", "Sportage", "Sorento", |
|
"Sentra", "Altima", "Rogue", "Murano" |
|
] |
|
|
|
years = [str(year) for year in range(2000, 2025)] |
|
countries = ["Select", "Pakistan", "USA", "UK", "Canada", "Australia", "Germany", "India", "Japan"] |
|
|
|
|
|
def estimate_repair_cost(damage_type, company, model, year, country): |
|
prompt = ( |
|
f"Estimate the repair cost for {damage_type} on a {year} {company} {model} in {country}. " |
|
f"Provide the approximate total cost in local currency with your confidence level, concisely in 2 lines." |
|
) |
|
|
|
try: |
|
|
|
response = client.chat.completions.create( |
|
model="gpt-4o-mini", |
|
messages=[ |
|
{"role": "system", "content": "You are an expert in car repair cost estimation."}, |
|
{"role": "user", "content": prompt} |
|
], |
|
temperature=0.5, |
|
max_tokens=100 |
|
) |
|
|
|
return response.choices[0].message.content.strip() |
|
except Exception as e: |
|
print(f"Error in GPT-4.0 API call: {e}") |
|
return f"Error: {e}" |
|
|
|
|
|
def detect_damage(image): |
|
inputs = processor(images=image, return_tensors="pt") |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
probs = torch.nn.functional.softmax(outputs.logits, dim=-1) |
|
confidences, predicted_class = torch.max(probs, dim=-1) |
|
predicted_label = model.config.id2label[predicted_class.item()] |
|
return predicted_label, confidences.item() |
|
|
|
|
|
def process_image(image, company, model, year, country): |
|
damage_type, confidence = detect_damage(image) |
|
cost_estimate = estimate_repair_cost(damage_type, company, model, year, country) |
|
|
|
result = { |
|
"Major Detected Damage": damage_type, |
|
"Confidence": f"{confidence * 100:.2f}%", |
|
"Estimated Repair Cost": cost_estimate |
|
} |
|
return result |
|
|
|
|
|
with gr.Blocks() as interface: |
|
gr.Markdown("# Car Damage Detection and Cost Estimation") |
|
gr.Markdown("Upload an image of a damaged car to detect the type of damage and estimate the repair cost.") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
image_input = gr.Image(type="pil", label="Upload Car Image") |
|
company_input = gr.Dropdown(choices=car_companies, label="Car Company", value="Select") |
|
model_input = gr.Dropdown(choices=car_models, label="Car Model", value="Select") |
|
year_input = gr.Dropdown(choices=years, label="Year of Manufacture", value=years[-1]) |
|
country_input = gr.Dropdown(choices=countries, label="Your Country", value="Select") |
|
|
|
submit_button = gr.Button("Estimate Repair Cost") |
|
output = gr.JSON(label="Result") |
|
|
|
submit_button.click(process_image, inputs=[image_input, company_input, model_input, year_input, country_input], outputs=output) |
|
|
|
|
|
interface.launch() |
|
|