Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -1,191 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
from roboflow import Roboflow
|
3 |
-
import openai
|
4 |
-
import cv2
|
5 |
-
import os
|
6 |
-
import time
|
7 |
-
from dotenv import load_dotenv
|
8 |
|
9 |
-
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
14 |
-
|
15 |
-
# Check if the environment variables are loaded properly
|
16 |
-
if not OPENAI_API_KEY:
|
17 |
-
raise ValueError("API keys not found in .env file")
|
18 |
-
# Ensure model initialization happens only once here
|
19 |
-
rf = Roboflow(api_key="9p4Y2dY8Y6KAT73koAbq")
|
20 |
-
project = rf.workspace().project("damaged-vehicle-images")
|
21 |
-
roboflow_model = project.version(3).model # Renaming to avoid any potential overwrite
|
22 |
-
|
23 |
-
openai.api_key = OPENAI_API_KEY
|
24 |
-
|
25 |
-
def generate_response(prompt):
|
26 |
-
response = openai.ChatCompletion.create(
|
27 |
-
model="gpt-3.5-turbo-16k-0613",
|
28 |
-
temperature=0,
|
29 |
-
messages=[
|
30 |
-
{"role": "system", "content": f"{prompt}"}
|
31 |
-
],
|
32 |
-
)
|
33 |
-
message = response.choices[0].message.content
|
34 |
-
return message
|
35 |
-
|
36 |
-
def vehicle_assessment(make, model, year, image):
|
37 |
-
# Save the image to a temporary file
|
38 |
-
temp_filename = "temp_uploaded_image.jpg"
|
39 |
-
cv2.imwrite(temp_filename, image)
|
40 |
-
|
41 |
-
result = roboflow_model.predict(temp_filename, confidence=40, overlap=30).json()
|
42 |
-
|
43 |
-
pred_l = []
|
44 |
-
for prediction in result['predictions']:
|
45 |
-
pred = prediction['class'].replace("_", " ")
|
46 |
-
pred_l.append(pred)
|
47 |
-
|
48 |
-
# Generate response using OpenAI
|
49 |
-
prompt = f"""I have this data:
|
50 |
-
Make Model Year Type of Damage Repair Description Severity Estimated Labor Hours Parts Required Estimated Parts Cost Labor Rate Total Estimated Cost
|
51 |
-
Toyota Camry 2015 Front Bumper Dent Repair Minor 2 Bumper, Paint $200 $50/hr $300
|
52 |
-
Honda Civic 2010 Windshield Replacement Major 3 Windshield, Adhesive $250 $45/hr $385
|
53 |
-
BMW X5 2019 Rear Bumper Scratch Repair Minor 1.5 Bumper, Paint $180 $65/hr $277.5
|
54 |
-
etc
|
55 |
-
|
56 |
-
and I have this info about a car:
|
57 |
-
{make} , {model} , {year} ,{pred_l}
|
58 |
-
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
|
59 |
-
|
60 |
-
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:
|
61 |
-
severe scratch, severe scratch, medium deformation, severe scratch, severe scratch, severe scratch ... etc,
|
62 |
-
|
63 |
-
follow this structure:
|
64 |
-
|
65 |
-
Car Damage Assessment and Estimation Report
|
66 |
-
|
67 |
-
Vehicle Information:
|
68 |
-
|
69 |
-
Make: [Vehicle Make]
|
70 |
-
Model: [Vehicle Model]
|
71 |
-
Year: [Vehicle Year]
|
72 |
-
|
73 |
-
Damage Description:
|
74 |
-
|
75 |
-
Description of Damage: [Describe the damage in detail]
|
76 |
-
|
77 |
-
Estimated Repair Costs:
|
78 |
-
|
79 |
-
Repair Description: Bodywork and Paint Repair ... etc
|
80 |
-
Estimated Labor Hours: [Labor hours Estimate]
|
81 |
-
Parts Required: Paint, Body Filler, Replacement Parts ... etc
|
82 |
-
Estimated Parts Cost: $[Parts Cost Estimate]
|
83 |
-
Labor Rate: $[Labor Cost Estimate]/hr
|
84 |
-
Paint and Materials: $[Paint and Materials Cost Estimate]
|
85 |
-
Total Estimated Repair Cost: $[Total Estimated Cost]
|
86 |
-
|
87 |
-
|
88 |
-
this way you covered all damages and their estimation and you gave me the total thing which I wants
|
89 |
-
|
90 |
-
"""
|
91 |
-
|
92 |
-
response = generate_response(prompt)
|
93 |
-
|
94 |
-
# Clean up the temporary file
|
95 |
-
os.remove(temp_filename)
|
96 |
-
return response
|
97 |
-
|
98 |
-
with gr.Blocks() as app:
|
99 |
-
# gr.Image(type="pil", value='misbah-green-yellow-logo1.png', width=100, height=100)
|
100 |
-
|
101 |
-
|
102 |
-
gr.Markdown(
|
103 |
-
'<div style="display: inline-block;width: 100%;">'
|
104 |
-
'<img class="img-fluid AnimatedLogo" alt="misbah logo icons" title="misbah logo icons" src="https://img1.wsimg.com/isteam/ip/8a73a484-6b7c-4a18-9dc5-2526353c4068/misbah-green-yellow-logo1.png/:/rs=w:275,h:200,cg:true,m/cr=w:275,h:200/qt=q:95" width="150px">'
|
105 |
-
'</div>'
|
106 |
-
)
|
107 |
-
|
108 |
-
# Define the layout using the Tabs
|
109 |
-
with gr.Tab("Damage Assessment"):
|
110 |
-
gr.Markdown('## Evaluate vehicle damage and estimate repair costs by inputting vehicle details and uploading an image of the damage. Receive a comprehensive repair estimate.')
|
111 |
-
make_input = gr.inputs.Textbox(label="Make")
|
112 |
-
model_input = gr.inputs.Textbox(label="Model")
|
113 |
-
year_input = gr.inputs.Textbox(label="Year")
|
114 |
-
image_input = gr.inputs.Image(label="Upload Image")
|
115 |
-
vehicle_output = gr.outputs.Textbox(label="Estimation Report")
|
116 |
-
vehicle_button = gr.Button("Assess")
|
117 |
-
|
118 |
-
|
119 |
-
with gr.Tab("Repair Pal Chat"):
|
120 |
-
gr.Markdown('## chat with our Repair Pal about your car problem')
|
121 |
-
|
122 |
-
system_message = {"role": "system", "content": "You are a helpful assistant."}
|
123 |
-
chatbot = gr.Chatbot()
|
124 |
-
msg = gr.Textbox(label='write your problem')
|
125 |
-
clear = gr.Button("Clear")
|
126 |
-
|
127 |
-
state = gr.State([])
|
128 |
-
|
129 |
-
def user(user_message, history):
|
130 |
-
return "", history + [[user_message, None]]
|
131 |
-
|
132 |
-
def bot(history, messages_history):
|
133 |
-
user_message = history[-1][0]
|
134 |
-
bot_message, messages_history = ask_gpt(user_message, messages_history)
|
135 |
-
messages_history += [{"role": "assistant", "content": bot_message}]
|
136 |
-
history[-1][1] = bot_message
|
137 |
-
time.sleep(1)
|
138 |
-
return history, messages_history
|
139 |
-
|
140 |
-
def ask_gpt(message, messages_history):
|
141 |
-
messages_history += [{"role": "user", "content": message}]
|
142 |
-
response = openai.ChatCompletion.create(
|
143 |
-
model="gpt-3.5-turbo-16k-0613",
|
144 |
-
messages=messages_history
|
145 |
-
)
|
146 |
-
return response['choices'][0]['message']['content'], messages_history
|
147 |
-
|
148 |
-
def init_history(messages_history):
|
149 |
-
messages_history = []
|
150 |
-
messages_history += [system_message]
|
151 |
-
return messages_history
|
152 |
-
|
153 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
154 |
-
bot, [chatbot, state], [chatbot, state]
|
155 |
-
)
|
156 |
-
|
157 |
-
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
|
158 |
-
|
159 |
-
def chat_with_bot(chat_input):
|
160 |
-
chat_response = generate_response(chat_input)
|
161 |
-
return chat_response
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
with gr.Tab("Repair shops and spare parts"):
|
167 |
-
gr.Markdown("""
|
168 |
-
## Based on the damage assesment report, you can repair your car at these repair shops:
|
169 |
-
|
170 |
-
[Location](https://maps.app.goo.gl/DwZRi2Kr18fYBQR96?g_st=ic)
|
171 |
-
|
172 |
-
[Location](https://maps.app.goo.gl/knkiyaLMqYLM86H67?g_st=ic)
|
173 |
-
|
174 |
-
[Location](https://maps.app.goo.gl/xQSQMytHA3QmshMn6?g_st=ic)
|
175 |
-
|
176 |
-
## You can find spare parts for your damaged parts at these websites:
|
177 |
-
|
178 |
-
[Rafraf](https://rafraf.com/)
|
179 |
-
|
180 |
-
[Afyal](https://afyal.com/)
|
181 |
-
|
182 |
-
""")
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
# Link the button click to the vehicle assessment function
|
187 |
-
vehicle_button.click(vehicle_assessment, inputs=[make_input, model_input, year_input, image_input], outputs=vehicle_output)
|
188 |
-
# chat_button.click(chat_with_bot, inputs=[chat_input], outputs=chat_output)
|
189 |
-
|
190 |
-
|
191 |
-
app.launch(share=True)
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
def greet(name):
|
4 |
+
return "Hello " + name + "!!"
|
5 |
|
6 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app2.py
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from roboflow import Roboflow
|
3 |
+
import openai
|
4 |
+
import cv2
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Retrieve API keys from environment variables
|
12 |
+
# ROBOFLOW_API_KEY = os.getenv("ROBOFLOW_API_KEY")
|
13 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
14 |
+
|
15 |
+
# Check if the environment variables are loaded properly
|
16 |
+
if not OPENAI_API_KEY:
|
17 |
+
raise ValueError("API keys not found in .env file")
|
18 |
+
# Ensure model initialization happens only once here
|
19 |
+
rf = Roboflow(api_key="9p4Y2dY8Y6KAT73koAbq")
|
20 |
+
project = rf.workspace().project("damaged-vehicle-images")
|
21 |
+
roboflow_model = project.version(3).model # Renaming to avoid any potential overwrite
|
22 |
+
|
23 |
+
openai.api_key = OPENAI_API_KEY
|
24 |
+
|
25 |
+
def generate_response(prompt):
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model="gpt-3.5-turbo-16k-0613",
|
28 |
+
temperature=0,
|
29 |
+
messages=[
|
30 |
+
{"role": "system", "content": f"{prompt}"}
|
31 |
+
],
|
32 |
+
)
|
33 |
+
message = response.choices[0].message.content
|
34 |
+
return message
|
35 |
+
|
36 |
+
def vehicle_assessment(make, model, year, image):
|
37 |
+
# Save the image to a temporary file
|
38 |
+
temp_filename = "temp_uploaded_image.jpg"
|
39 |
+
cv2.imwrite(temp_filename, image)
|
40 |
+
|
41 |
+
result = roboflow_model.predict(temp_filename, confidence=40, overlap=30).json()
|
42 |
+
|
43 |
+
pred_l = []
|
44 |
+
for prediction in result['predictions']:
|
45 |
+
pred = prediction['class'].replace("_", " ")
|
46 |
+
pred_l.append(pred)
|
47 |
+
|
48 |
+
# Generate response using OpenAI
|
49 |
+
prompt = f"""I have this data:
|
50 |
+
Make Model Year Type of Damage Repair Description Severity Estimated Labor Hours Parts Required Estimated Parts Cost Labor Rate Total Estimated Cost
|
51 |
+
Toyota Camry 2015 Front Bumper Dent Repair Minor 2 Bumper, Paint $200 $50/hr $300
|
52 |
+
Honda Civic 2010 Windshield Replacement Major 3 Windshield, Adhesive $250 $45/hr $385
|
53 |
+
BMW X5 2019 Rear Bumper Scratch Repair Minor 1.5 Bumper, Paint $180 $65/hr $277.5
|
54 |
+
etc
|
55 |
+
|
56 |
+
and I have this info about a car:
|
57 |
+
{make} , {model} , {year} ,{pred_l}
|
58 |
+
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
|
59 |
+
|
60 |
+
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:
|
61 |
+
severe scratch, severe scratch, medium deformation, severe scratch, severe scratch, severe scratch ... etc,
|
62 |
+
|
63 |
+
follow this structure:
|
64 |
+
|
65 |
+
Car Damage Assessment and Estimation Report
|
66 |
+
|
67 |
+
Vehicle Information:
|
68 |
+
|
69 |
+
Make: [Vehicle Make]
|
70 |
+
Model: [Vehicle Model]
|
71 |
+
Year: [Vehicle Year]
|
72 |
+
|
73 |
+
Damage Description:
|
74 |
+
|
75 |
+
Description of Damage: [Describe the damage in detail]
|
76 |
+
|
77 |
+
Estimated Repair Costs:
|
78 |
+
|
79 |
+
Repair Description: Bodywork and Paint Repair ... etc
|
80 |
+
Estimated Labor Hours: [Labor hours Estimate]
|
81 |
+
Parts Required: Paint, Body Filler, Replacement Parts ... etc
|
82 |
+
Estimated Parts Cost: $[Parts Cost Estimate]
|
83 |
+
Labor Rate: $[Labor Cost Estimate]/hr
|
84 |
+
Paint and Materials: $[Paint and Materials Cost Estimate]
|
85 |
+
Total Estimated Repair Cost: $[Total Estimated Cost]
|
86 |
+
|
87 |
+
|
88 |
+
this way you covered all damages and their estimation and you gave me the total thing which I wants
|
89 |
+
|
90 |
+
"""
|
91 |
+
|
92 |
+
response = generate_response(prompt)
|
93 |
+
|
94 |
+
# Clean up the temporary file
|
95 |
+
os.remove(temp_filename)
|
96 |
+
return response
|
97 |
+
|
98 |
+
with gr.Blocks() as app:
|
99 |
+
# gr.Image(type="pil", value='misbah-green-yellow-logo1.png', width=100, height=100)
|
100 |
+
|
101 |
+
|
102 |
+
gr.Markdown(
|
103 |
+
'<div style="display: inline-block;width: 100%;">'
|
104 |
+
'<img class="img-fluid AnimatedLogo" alt="misbah logo icons" title="misbah logo icons" src="https://img1.wsimg.com/isteam/ip/8a73a484-6b7c-4a18-9dc5-2526353c4068/misbah-green-yellow-logo1.png/:/rs=w:275,h:200,cg:true,m/cr=w:275,h:200/qt=q:95" width="150px">'
|
105 |
+
'</div>'
|
106 |
+
)
|
107 |
+
|
108 |
+
# Define the layout using the Tabs
|
109 |
+
with gr.Tab("Damage Assessment"):
|
110 |
+
gr.Markdown('## Evaluate vehicle damage and estimate repair costs by inputting vehicle details and uploading an image of the damage. Receive a comprehensive repair estimate.')
|
111 |
+
make_input = gr.inputs.Textbox(label="Make")
|
112 |
+
model_input = gr.inputs.Textbox(label="Model")
|
113 |
+
year_input = gr.inputs.Textbox(label="Year")
|
114 |
+
image_input = gr.inputs.Image(label="Upload Image")
|
115 |
+
vehicle_output = gr.outputs.Textbox(label="Estimation Report")
|
116 |
+
vehicle_button = gr.Button("Assess")
|
117 |
+
|
118 |
+
|
119 |
+
with gr.Tab("Repair Pal Chat"):
|
120 |
+
gr.Markdown('## chat with our Repair Pal about your car problem')
|
121 |
+
|
122 |
+
system_message = {"role": "system", "content": "You are a helpful assistant."}
|
123 |
+
chatbot = gr.Chatbot()
|
124 |
+
msg = gr.Textbox(label='write your problem')
|
125 |
+
clear = gr.Button("Clear")
|
126 |
+
|
127 |
+
state = gr.State([])
|
128 |
+
|
129 |
+
def user(user_message, history):
|
130 |
+
return "", history + [[user_message, None]]
|
131 |
+
|
132 |
+
def bot(history, messages_history):
|
133 |
+
user_message = history[-1][0]
|
134 |
+
bot_message, messages_history = ask_gpt(user_message, messages_history)
|
135 |
+
messages_history += [{"role": "assistant", "content": bot_message}]
|
136 |
+
history[-1][1] = bot_message
|
137 |
+
time.sleep(1)
|
138 |
+
return history, messages_history
|
139 |
+
|
140 |
+
def ask_gpt(message, messages_history):
|
141 |
+
messages_history += [{"role": "user", "content": message}]
|
142 |
+
response = openai.ChatCompletion.create(
|
143 |
+
model="gpt-3.5-turbo-16k-0613",
|
144 |
+
messages=messages_history
|
145 |
+
)
|
146 |
+
return response['choices'][0]['message']['content'], messages_history
|
147 |
+
|
148 |
+
def init_history(messages_history):
|
149 |
+
messages_history = []
|
150 |
+
messages_history += [system_message]
|
151 |
+
return messages_history
|
152 |
+
|
153 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
154 |
+
bot, [chatbot, state], [chatbot, state]
|
155 |
+
)
|
156 |
+
|
157 |
+
clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
|
158 |
+
|
159 |
+
def chat_with_bot(chat_input):
|
160 |
+
chat_response = generate_response(chat_input)
|
161 |
+
return chat_response
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
+
|
166 |
+
with gr.Tab("Repair shops and spare parts"):
|
167 |
+
gr.Markdown("""
|
168 |
+
## Based on the damage assesment report, you can repair your car at these repair shops:
|
169 |
+
|
170 |
+
[Location](https://maps.app.goo.gl/DwZRi2Kr18fYBQR96?g_st=ic)
|
171 |
+
|
172 |
+
[Location](https://maps.app.goo.gl/knkiyaLMqYLM86H67?g_st=ic)
|
173 |
+
|
174 |
+
[Location](https://maps.app.goo.gl/xQSQMytHA3QmshMn6?g_st=ic)
|
175 |
+
|
176 |
+
## You can find spare parts for your damaged parts at these websites:
|
177 |
+
|
178 |
+
[Rafraf](https://rafraf.com/)
|
179 |
+
|
180 |
+
[Afyal](https://afyal.com/)
|
181 |
+
|
182 |
+
""")
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
+
# Link the button click to the vehicle assessment function
|
187 |
+
vehicle_button.click(vehicle_assessment, inputs=[make_input, model_input, year_input, image_input], outputs=vehicle_output)
|
188 |
+
# chat_button.click(chat_with_bot, inputs=[chat_input], outputs=chat_output)
|
189 |
+
|
190 |
+
|
191 |
+
app.launch(share=True)
|