import gradio as gr from roboflow import Roboflow import openai import cv2 import os import time from dotenv import load_dotenv load_dotenv() # Retrieve API keys from environment variables # ROBOFLOW_API_KEY = os.getenv("ROBOFLOW_API_KEY") OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Check if the environment variables are loaded properly if not OPENAI_API_KEY: raise ValueError("API keys not found in .env file") # Ensure model initialization happens only once here rf = Roboflow(api_key="9p4Y2dY8Y6KAT73koAbq") project = rf.workspace().project("damaged-vehicle-images") roboflow_model = project.version(3).model # Renaming to avoid any potential overwrite openai.api_key = OPENAI_API_KEY def generate_response(prompt): response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k-0613", temperature=0, messages=[ {"role": "system", "content": f"{prompt}"} ], ) message = response.choices[0].message.content return message def vehicle_assessment(make, model, year, image): # Save the image to a temporary file temp_filename = "temp_uploaded_image.jpg" cv2.imwrite(temp_filename, image) result = roboflow_model.predict(temp_filename, confidence=40, overlap=30).json() pred_l = [] for prediction in result['predictions']: pred = prediction['class'].replace("_", " ") pred_l.append(pred) # Generate response using OpenAI prompt = f"""I have this data: Make Model Year Type of Damage Repair Description Severity Estimated Labor Hours Parts Required Estimated Parts Cost Labor Rate Total Estimated Cost Toyota Camry 2015 Front Bumper Dent Repair Minor 2 Bumper, Paint $200 $50/hr $300 Honda Civic 2010 Windshield Replacement Major 3 Windshield, Adhesive $250 $45/hr $385 BMW X5 2019 Rear Bumper Scratch Repair Minor 1.5 Bumper, Paint $180 $65/hr $277.5 etc and I have this info about a car: {make} , {model} , {year} ,{pred_l} predict the rest of the classes in a similar fashion to the data provided, Repair Description Estimated Labor Hours Parts Required Estimated Parts Cost Labor Rate Total Estimated Cost DO NOT write sepertae estimaetuon for each damage, make your predictions based on all of them, so for example, if the damages ar like this: severe scratch, severe scratch, medium deformation, severe scratch, severe scratch, severe scratch ... etc, follow this structure: Car Damage Assessment and Estimation Report Vehicle Information: Make: [Vehicle Make] Model: [Vehicle Model] Year: [Vehicle Year] Damage Description: Description of Damage: [Describe the damage in detail] Estimated Repair Costs: Repair Description: Bodywork and Paint Repair ... etc Estimated Labor Hours: [Labor hours Estimate] Parts Required: Paint, Body Filler, Replacement Parts ... etc Estimated Parts Cost: $[Parts Cost Estimate] Labor Rate: $[Labor Cost Estimate]/hr Paint and Materials: $[Paint and Materials Cost Estimate] Total Estimated Repair Cost: $[Total Estimated Cost] this way you covered all damages and their estimation and you gave me the total thing which I wants """ response = generate_response(prompt) # Clean up the temporary file os.remove(temp_filename) return response with gr.Blocks() as app: # gr.Image(type="pil", value='misbah-green-yellow-logo1.png', width=100, height=100) gr.Markdown( '