Spaces:
Build error
Build error
Initial Commit
Browse files- app.py +101 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import openai
|
| 9 |
+
|
| 10 |
+
# Load the Hugging Face model for car damage detection
|
| 11 |
+
model_name = "beingamit99/car_damage_detection"
|
| 12 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 13 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 14 |
+
|
| 15 |
+
# Set your OpenAI API key
|
| 16 |
+
openai_api_key = os.getenv("OpenAI4oMini")
|
| 17 |
+
|
| 18 |
+
client = openai.OpenAI(api_key = openai_api_key)
|
| 19 |
+
|
| 20 |
+
# Dropdown Options
|
| 21 |
+
car_companies = ["Select", "Toyota", "Honda", "Ford", "BMW", "Mercedes", "Audi", "Hyundai", "Kia", "Nissan"]
|
| 22 |
+
car_models = [
|
| 23 |
+
"Select", # Default option
|
| 24 |
+
"Corolla", "Camry", "RAV4", "Highlander", # Toyota
|
| 25 |
+
"Civic", "Accord", "CR-V", "Pilot", # Honda
|
| 26 |
+
"Fiesta", "Focus", "Explorer", "Mustang", # Ford
|
| 27 |
+
"3 Series", "5 Series", "X3", "X5", # BMW
|
| 28 |
+
"C-Class", "E-Class", "GLC", "GLE", # Mercedes
|
| 29 |
+
"A3", "A4", "Q5", "Q7", # Audi
|
| 30 |
+
"Elantra", "Sonata", "Tucson", "Santa Fe", # Hyundai
|
| 31 |
+
"Rio", "Optima", "Sportage", "Sorento", # Kia
|
| 32 |
+
"Sentra", "Altima", "Rogue", "Murano" # Nissan
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
years = [str(year) for year in range(2000, 2025)]
|
| 36 |
+
countries = ["Select", "Pakistan", "USA", "UK", "Canada", "Australia", "Germany", "India", "Japan"]
|
| 37 |
+
|
| 38 |
+
# Function to Estimate Repair Cost using GPT-4.0 Mini
|
| 39 |
+
def estimate_repair_cost(damage_type, company, model, year, country):
|
| 40 |
+
prompt = (
|
| 41 |
+
f"Estimate the repair cost for {damage_type} on a {year} {company} {model} in {country}. "
|
| 42 |
+
f"Provide the approximate total cost in local currency with your confidence level, concisely in 2 lines."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
response = client.chat.completions.create(
|
| 47 |
+
model="gpt-4o-mini",
|
| 48 |
+
messages=[
|
| 49 |
+
{"role": "system", "content": "You are an expert in car repair cost estimation."},
|
| 50 |
+
{"role": "user", "content": prompt}
|
| 51 |
+
],
|
| 52 |
+
temperature=0.5,
|
| 53 |
+
max_tokens=100
|
| 54 |
+
)
|
| 55 |
+
return response.choices[0].message.content.strip()
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error in GPT-4o API call: {e}")
|
| 58 |
+
return f"Error: {e}"
|
| 59 |
+
|
| 60 |
+
# Function to Detect Car Damage from Image using Hugging Face Model
|
| 61 |
+
def detect_damage(image):
|
| 62 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
outputs = model(**inputs)
|
| 65 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 66 |
+
confidences, predicted_class = torch.max(probs, dim=-1)
|
| 67 |
+
predicted_label = model.config.id2label[predicted_class.item()]
|
| 68 |
+
return predicted_label, confidences.item()
|
| 69 |
+
|
| 70 |
+
# Function to Process Image and Get Results
|
| 71 |
+
def process_image(image, company, model, year, country):
|
| 72 |
+
damage_type, confidence = detect_damage(image)
|
| 73 |
+
cost_estimate = estimate_repair_cost(damage_type, company, model, year, country)
|
| 74 |
+
|
| 75 |
+
result = {
|
| 76 |
+
"Major Detected Damage": damage_type,
|
| 77 |
+
"Confidence": f"{confidence * 100:.2f}%",
|
| 78 |
+
"Estimated Repair Cost": cost_estimate
|
| 79 |
+
}
|
| 80 |
+
return result
|
| 81 |
+
|
| 82 |
+
# Gradio Interface
|
| 83 |
+
with gr.Blocks() as interface:
|
| 84 |
+
gr.Markdown("# Car Damage Detection and Cost Estimation")
|
| 85 |
+
gr.Markdown("Upload an image of a damaged car to detect the type of damage and estimate the repair cost.")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column():
|
| 89 |
+
image_input = gr.Image(type="pil", label="Upload Car Image")
|
| 90 |
+
company_input = gr.Dropdown(choices=car_companies, label="Car Company", value="Select")
|
| 91 |
+
model_input = gr.Dropdown(choices=car_models, label="Car Model", value="Select")
|
| 92 |
+
year_input = gr.Dropdown(choices=years, label="Year of Manufacture", value=years[-1])
|
| 93 |
+
country_input = gr.Dropdown(choices=countries, label="Your Country", value="Select")
|
| 94 |
+
|
| 95 |
+
submit_button = gr.Button("Estimate Repair Cost")
|
| 96 |
+
output = gr.JSON(label="Result")
|
| 97 |
+
|
| 98 |
+
submit_button.click(process_image, inputs=[image_input, company_input, model_input, year_input, country_input], outputs=output)
|
| 99 |
+
|
| 100 |
+
# Launch the Gradio Interface
|
| 101 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
Pillow
|
| 5 |
+
requests
|
| 6 |
+
openai
|