Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +48 -0
- linear_regression.pkl +3 -0
- requirements.txt +5 -0
- svr_model.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import joblib
|
3 |
+
import numpy as np
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
# Download models from Hugging Face Hub
|
7 |
+
svr_path = hf_hub_download(repo_id="iamomtiwari/Nutrition-regression-models", filename="svr_model.pkl")
|
8 |
+
lr_path = hf_hub_download(repo_id="iamomtiwari/Nutrition-regression-models", filename="linear_regression.pkl")
|
9 |
+
|
10 |
+
# Load models
|
11 |
+
svr_model = joblib.load(svr_path)
|
12 |
+
linear_reg = joblib.load(lr_path)
|
13 |
+
|
14 |
+
# Selected 10 important features
|
15 |
+
features = ['Caloric Value', 'Fat', 'Saturated Fats', 'Carbohydrates', 'Sugars',
|
16 |
+
'Protein', 'Cholesterol', 'Sodium', 'Calcium', 'Iron']
|
17 |
+
|
18 |
+
# Define prediction function
|
19 |
+
def predict(model_name, *inputs):
|
20 |
+
input_data = np.array([inputs]).reshape(1, -1)
|
21 |
+
|
22 |
+
if model_name == "SVR":
|
23 |
+
prediction = svr_model.predict(input_data)[0]
|
24 |
+
else:
|
25 |
+
prediction = linear_reg.predict(input_data)[0]
|
26 |
+
|
27 |
+
return round(prediction, 4)
|
28 |
+
|
29 |
+
# Gradio Interface
|
30 |
+
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("# Nutrition Density Prediction")
|
32 |
+
|
33 |
+
model_choice = gr.Radio(["SVR", "Linear Regression"], label="Select Model")
|
34 |
+
input_widgets = [gr.Slider(minimum=0, maximum=100, step=0.1, label=feature) for feature in features]
|
35 |
+
predict_button = gr.Button("Predict")
|
36 |
+
clear_button = gr.Button("Clear")
|
37 |
+
output_label = gr.Textbox(label="Prediction")
|
38 |
+
|
39 |
+
predict_button.click(predict, inputs=[model_choice] + input_widgets, outputs=output_label)
|
40 |
+
|
41 |
+
# Reset sliders to their default value (0) on "Clear"
|
42 |
+
def reset_sliders():
|
43 |
+
return [0] * len(features)
|
44 |
+
|
45 |
+
clear_button.click(reset_sliders, inputs=[], outputs=input_widgets)
|
46 |
+
|
47 |
+
# Run the app
|
48 |
+
demo.launch()
|
linear_regression.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:de35ed6ba09c115498091604224187d4e2bda199f8edd9b2027f562d6fabcc48
|
3 |
+
size 1876
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
joblib
|
3 |
+
pandas
|
4 |
+
scikit-learn
|
5 |
+
huggingface_hub
|
svr_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2fba68204dc51a2026d8541ba7132ba7cb47d11da4a25f6e292b33bf0e1769e0
|
3 |
+
size 8370
|