Spaces:
Runtime error
Runtime error
Add app.py for Gradio Space
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Define the repository ID and the model filename
|
| 8 |
+
# Make sure this matches your deployed model on Hugging Face
|
| 9 |
+
repo_id = "farooqhasanDA/logistic-regression-sklearn-model"
|
| 10 |
+
model_filename = "models/logistic_regression_model.joblib"
|
| 11 |
+
|
| 12 |
+
# Global variable to store the loaded model
|
| 13 |
+
loaded_model = None
|
| 14 |
+
|
| 15 |
+
def predict_proba_wrapper(feature1_num, feature2_text, feature3_dropdown, feature4_checkbox):
|
| 16 |
+
global loaded_model
|
| 17 |
+
|
| 18 |
+
# Download and load the model if not already loaded
|
| 19 |
+
if loaded_model is None:
|
| 20 |
+
print("Downloading and loading model for the first time...")
|
| 21 |
+
try:
|
| 22 |
+
model_path_local = hf_hub_download(repo_id=repo_id, filename=model_filename)
|
| 23 |
+
loaded_model = joblib.load(model_path_local)
|
| 24 |
+
print("Model loaded successfully.")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error loading model: {e}"
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Convert text to a numerical value (e.g., length, or some simple encoding)
|
| 30 |
+
f2_val = len(feature2_text) if feature2_text else 0
|
| 31 |
+
# Convert dropdown choice to a numerical value
|
| 32 |
+
f3_val = {'Option A': 0, 'Option B': 1, 'Option C': 2}.get(feature3_dropdown, 0)
|
| 33 |
+
# Convert checkbox (boolean) to 0 or 1
|
| 34 |
+
f4_val = 1 if feature4_checkbox else 0
|
| 35 |
+
|
| 36 |
+
input_array = np.array([[float(feature1_num), float(f2_val), float(f3_val), float(f4_val)]])
|
| 37 |
+
|
| 38 |
+
# Make a prediction and get prediction probabilities
|
| 39 |
+
prediction = loaded_model.predict(input_array)
|
| 40 |
+
probabilities = loaded_model.predict_proba(input_array)
|
| 41 |
+
|
| 42 |
+
# Return formatted string
|
| 43 |
+
return (f"Prediction: Class {prediction[0]}, "
|
| 44 |
+
f"Probabilities: Class 0 = {probabilities[0][0]:.4f}, Class 1 = {probabilities[0][1]:.4f}")
|
| 45 |
+
except ValueError:
|
| 46 |
+
return "Error: Please ensure Feature 1 is a valid number."
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Prediction error: {e}"
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Create gradio input components with different types
|
| 52 |
+
inputs = [
|
| 53 |
+
gr.Number(label="Numerical Feature 1 (e.g., 0.5)", value=0.5),
|
| 54 |
+
gr.Textbox(label="Text Feature 2 (e.g., 'Hello world')", value="Sample text"),
|
| 55 |
+
gr.Dropdown(choices=["Option A", "Option B", "Option C"], label="Categorical Feature 3"),
|
| 56 |
+
gr.Checkbox(label="Boolean Feature 4 (checked/unchecked)", value=True)
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
# Create a gradio output component
|
| 60 |
+
output = gr.Textbox(label="Prediction Result")
|
| 61 |
+
|
| 62 |
+
# Instantiate gr.Interface
|
| 63 |
+
iface = gr.Interface(fn=predict_proba_wrapper, inputs=inputs, outputs=output,
|
| 64 |
+
title="Logistic Regression Model Predictor (Hosted on HF Space)",
|
| 65 |
+
description="Enter different types of features to get a binary classification prediction and probabilities.
|
| 66 |
+
(Note: Text/Dropdown/Checkbox inputs are converted to numbers for this demo model.)")
|
| 67 |
+
|
| 68 |
+
# Launch the Gradio interface
|
| 69 |
+
iface.launch(debug=True)
|