Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.model_selection import train_test_split
|
| 3 |
+
from sklearn.linear_model import LinearRegression
|
| 4 |
+
import joblib
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load the data from the CSV file
|
| 8 |
+
data = pd.read_csv('data.csv')
|
| 9 |
+
|
| 10 |
+
# Encode 'Price' column into numerical values
|
| 11 |
+
data['Price'] = data['Price'].apply(lambda x: 0 if x == 'Free' else 1)
|
| 12 |
+
|
| 13 |
+
# Convert 'Size' and 'Reviews' columns to numerical values
|
| 14 |
+
data['Size'] = data['Size'].str.replace('MB', '').astype(float)
|
| 15 |
+
data['Reviews'] = data['Reviews'].str.replace('M', '').astype(float)
|
| 16 |
+
|
| 17 |
+
# Select the features (reviews, size, and price) and the target variable (rating)
|
| 18 |
+
X = data[['Reviews', 'Size', 'Price']]
|
| 19 |
+
y = data['Rating']
|
| 20 |
+
|
| 21 |
+
# Split the data into training and testing sets
|
| 22 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 23 |
+
|
| 24 |
+
# Create a linear regression model
|
| 25 |
+
model = LinearRegression()
|
| 26 |
+
|
| 27 |
+
# Train the model
|
| 28 |
+
model.fit(X_train, y_train)
|
| 29 |
+
|
| 30 |
+
# Save the trained model
|
| 31 |
+
joblib.dump(model, 'linear_regression_model.pkl')
|
| 32 |
+
|
| 33 |
+
# Define a function to make predictions using the model
|
| 34 |
+
def predict_rating(reviews, size, price):
|
| 35 |
+
# Load the trained model
|
| 36 |
+
loaded_model = joblib.load('linear_regression_model.pkl')
|
| 37 |
+
# Make predictions using the loaded model
|
| 38 |
+
predicted_rating = loaded_model.predict([[reviews, size, price]])
|
| 39 |
+
return predicted_rating[0]
|
| 40 |
+
|
| 41 |
+
# Create a Gradio interface for the model
|
| 42 |
+
iface = gr.Interface(fn=predict_rating, inputs=["number", "number", "number"], outputs="number", title="App Rating Predictor", examples=[[20, 25.1, 0], [45, 26.7, 1], [60, 30.2, 0]], description="Enter the number of reviews, size(without 'MB' word), and price(0 = paid, 1 = free) of your app to predict its rating.")
|
| 43 |
+
|
| 44 |
+
# Launch the Gradio interface with a user guide
|
| 45 |
+
iface.launch(share=False, debug=True, enable_queue=True)
|