Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
|
| 6 |
+
# Load your model from your existing model repository
|
| 7 |
+
repo_id = "YOUR_USERNAME/random-forest-model" # CHANGE THIS
|
| 8 |
+
model_path = hf_hub_download(repo_id=repo_id, filename="random_forest_model.pkl")
|
| 9 |
+
|
| 10 |
+
with open(model_path, 'rb') as f:
|
| 11 |
+
model = pickle.load(f)
|
| 12 |
+
|
| 13 |
+
# Prediction function - adjust feature names as needed
|
| 14 |
+
def predict(feature1, feature2, feature3):
|
| 15 |
+
df = pd.DataFrame([[feature1, feature2, feature3]],
|
| 16 |
+
columns=['feature1', 'feature2', 'feature3'])
|
| 17 |
+
pred = model.predict(df)[0]
|
| 18 |
+
prob = model.predict_proba(df)[0].max()
|
| 19 |
+
return f"Prediction: {pred} (Confidence: {prob:.2f})"
|
| 20 |
+
|
| 21 |
+
# Create the API endpoint
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=predict,
|
| 24 |
+
inputs=[
|
| 25 |
+
gr.Number(label="Feature 1"),
|
| 26 |
+
gr.Number(label="Feature 2"),
|
| 27 |
+
gr.Number(label="Feature 3")
|
| 28 |
+
],
|
| 29 |
+
outputs=gr.Textbox(label="Result"),
|
| 30 |
+
title="Random Forest API"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
demo.launch()
|