Msaqibsharif commited on
Commit
55408fd
·
verified ·
1 Parent(s): eeb5a06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -1,7 +1,7 @@
1
  # Import necessary libraries
2
  import os
3
- import torch
4
  from PIL import Image
 
5
  from transformers import AutoImageProcessor, AutoModelForImageClassification
6
  import gradio as gr
7
  import openai
@@ -11,10 +11,15 @@ model_name = "beingamit99/car_damage_detection"
11
  processor = AutoImageProcessor.from_pretrained(model_name)
12
  model = AutoModelForImageClassification.from_pretrained(model_name)
13
 
14
- # Set your OpenAI API key from environment variable
15
  openai_api_key = os.getenv("OpenAI4oMini")
16
- openai.api_key = openai_api_key # Correct way to set the API key
17
- client = openai.OpenAI(api_key=openai_api_key) # Ensure client is initialized
 
 
 
 
 
18
 
19
  # Dropdown Options
20
  car_companies = ["Select", "Toyota", "Honda", "Ford", "BMW", "Mercedes", "Audi", "Hyundai", "Kia", "Nissan"]
@@ -37,12 +42,13 @@ countries = ["Select", "Pakistan", "USA", "UK", "Canada", "Australia", "Germany"
37
  # Function to Estimate Repair Cost using GPT-4.0 Mini
38
  def estimate_repair_cost(damage_type, company, model, year, country):
39
  prompt = (
40
- f"Estimate the repair cost for {damage_type} on a {year} {company} {model} in {country}. "
41
- f"Provide the approximate total cost in local currency with your confidence level, concisely in 2 lines."
42
- )
43
 
44
  try:
45
- response = client.chat.completions.create(
 
46
  model="gpt-4o-mini",
47
  messages=[
48
  {"role": "system", "content": "You are an expert in car repair cost estimation."},
@@ -51,6 +57,7 @@ def estimate_repair_cost(damage_type, company, model, year, country):
51
  temperature=0.5,
52
  max_tokens=100
53
  )
 
54
  return response['choices'][0]['message']['content'].strip()
55
  except Exception as e:
56
  print(f"Error in GPT-4.0 API call: {e}")
@@ -85,11 +92,11 @@ with gr.Blocks() as interface:
85
 
86
  with gr.Row():
87
  with gr.Column():
88
- image_input = gr.Image(type="pil", label="Upload Car Image")
89
- company_input = gr.Dropdown(choices=car_companies, label="Car Company", value="Select")
90
- model_input = gr.Dropdown(choices=car_models, label="Car Model", value="Select")
91
- year_input = gr.Dropdown(choices=years, label="Year of Manufacture", value=years[-1])
92
- country_input = gr.Dropdown(choices=countries, label="Your Country", value="Select")
93
 
94
  submit_button = gr.Button("Estimate Repair Cost")
95
  output = gr.JSON(label="Result")
 
1
  # Import necessary libraries
2
  import os
 
3
  from PIL import Image
4
+ import torch
5
  from transformers import AutoImageProcessor, AutoModelForImageClassification
6
  import gradio as gr
7
  import openai
 
11
  processor = AutoImageProcessor.from_pretrained(model_name)
12
  model = AutoModelForImageClassification.from_pretrained(model_name)
13
 
14
+ # Set your OpenAI API key
15
  openai_api_key = os.getenv("OpenAI4oMini")
16
+
17
+ # Validate API Key
18
+ if openai_api_key is None:
19
+ raise ValueError("OpenAI API key is not set. Make sure to set the OpenAI4oMini secret in Hugging Face.")
20
+
21
+ # Initialize OpenAI Client
22
+ client = openai.OpenAI(api_key=openai_api_key)
23
 
24
  # Dropdown Options
25
  car_companies = ["Select", "Toyota", "Honda", "Ford", "BMW", "Mercedes", "Audi", "Hyundai", "Kia", "Nissan"]
 
42
  # Function to Estimate Repair Cost using GPT-4.0 Mini
43
  def estimate_repair_cost(damage_type, company, model, year, country):
44
  prompt = (
45
+ f"Estimate the repair cost for {damage_type} on a {year} {company} {model} in {country}. "
46
+ f"Provide the approximate total cost in local currency with your confidence level, concisely in 2 lines."
47
+ )
48
 
49
  try:
50
+ # Using client for API call
51
+ response = client.ChatCompletion.create(
52
  model="gpt-4o-mini",
53
  messages=[
54
  {"role": "system", "content": "You are an expert in car repair cost estimation."},
 
57
  temperature=0.5,
58
  max_tokens=100
59
  )
60
+ # Correctly access the response content
61
  return response['choices'][0]['message']['content'].strip()
62
  except Exception as e:
63
  print(f"Error in GPT-4.0 API call: {e}")
 
92
 
93
  with gr.Row():
94
  with gr.Column():
95
+ image_input = gr.Image(type="pil", label="Upload Car Image")
96
+ company_input = gr.Dropdown(choices=car_companies, label="Car Company", value="Select")
97
+ model_input = gr.Dropdown(choices=car_models, label="Car Model", value="Select")
98
+ year_input = gr.Dropdown(choices=years, label="Year of Manufacture", value=years[-1])
99
+ country_input = gr.Dropdown(choices=countries, label="Your Country", value="Select")
100
 
101
  submit_button = gr.Button("Estimate Repair Cost")
102
  output = gr.JSON(label="Result")