Spaces:
Runtime error
Runtime error
Adds application
Browse files- app.py +78 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import traceback
|
2 |
+
|
3 |
+
import datasets
|
4 |
+
import gradio as gr
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import numpy as np
|
7 |
+
from sklearn.linear_model import LinearRegression
|
8 |
+
from sklearn.metrics import r2_score
|
9 |
+
|
10 |
+
|
11 |
+
def process_data(vendor, model):
|
12 |
+
data = datasets.load_dataset('anakib1/mango-ria', '13.08.2024')['train']
|
13 |
+
# Handle cases where vendor or model is None or empty string
|
14 |
+
if vendor:
|
15 |
+
vendor = vendor.strip().lower()
|
16 |
+
else:
|
17 |
+
vendor = ''
|
18 |
+
if model:
|
19 |
+
model = model.strip().lower()
|
20 |
+
else:
|
21 |
+
model = ''
|
22 |
+
|
23 |
+
rows = data.filter(lambda x: vendor in x['Title'].lower() and model in x['Title'].lower())
|
24 |
+
|
25 |
+
dots = []
|
26 |
+
for row in rows:
|
27 |
+
# row[2] is the 'Title' field
|
28 |
+
try:
|
29 |
+
price = float(row['Price'])
|
30 |
+
mileage = float(row['Mileage'].split()[0])
|
31 |
+
dots.append((price, mileage))
|
32 |
+
except:
|
33 |
+
print(f"Could not parse row {row}. Ex = {traceback.format_exc()}")
|
34 |
+
|
35 |
+
if not dots:
|
36 |
+
return "No data found for the specified vendor and model.", None, None
|
37 |
+
|
38 |
+
price, mileage = list(zip(*dots))
|
39 |
+
|
40 |
+
# First plot: Histogram of prices
|
41 |
+
fig1, ax1 = plt.subplots()
|
42 |
+
ax1.hist(price)
|
43 |
+
ax1.set_title('Histogram of Prices')
|
44 |
+
ax1.set_xlabel('Price')
|
45 |
+
ax1.set_ylabel('Frequency')
|
46 |
+
|
47 |
+
# Second plot: Scatter plot with regression line
|
48 |
+
fig2, ax2 = plt.subplots()
|
49 |
+
model_lr = LinearRegression()
|
50 |
+
model_lr.fit(np.array(mileage).reshape(-1, 1), price)
|
51 |
+
y_hat = model_lr.predict(np.array(mileage).reshape(-1, 1))
|
52 |
+
ax2.scatter(mileage, price)
|
53 |
+
ax2.plot(mileage, y_hat, color='r',
|
54 |
+
label='y = {:.2f} * x + {:.2f}. R2 = {:.2f}'.format(model_lr.coef_[0], model_lr.intercept_,
|
55 |
+
r2_score(y_true=price, y_pred=y_hat)))
|
56 |
+
ax2.legend()
|
57 |
+
ax2.set_xlabel('Mileage')
|
58 |
+
ax2.set_ylabel('Price')
|
59 |
+
ax2.set_title('Price vs Mileage with Regression Line')
|
60 |
+
|
61 |
+
# Return the figures
|
62 |
+
return None, fig1, fig2
|
63 |
+
|
64 |
+
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
gr.Markdown("# Car Data Analysis")
|
67 |
+
with gr.Row():
|
68 |
+
vendor_input = gr.Textbox(lines=1, label="Vendor", placeholder="Enter vendor, e.g., infiniti")
|
69 |
+
model_input = gr.Textbox(lines=1, label="Model", placeholder="Enter model, e.g., q50")
|
70 |
+
submit_btn = gr.Button("Submit")
|
71 |
+
message_output = gr.Textbox(label="Message", interactive=False)
|
72 |
+
plot_output1 = gr.Plot(label="Histogram of Prices")
|
73 |
+
plot_output2 = gr.Plot(label="Price vs Mileage with Regression Line")
|
74 |
+
|
75 |
+
submit_btn.click(process_data, inputs=[vendor_input, model_input],
|
76 |
+
outputs=[message_output, plot_output1, plot_output2])
|
77 |
+
|
78 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
scikit-learn
|
3 |
+
matplotlib
|
4 |
+
numpy
|
5 |
+
datasets
|