Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import joblib | |
| import requests | |
| import os | |
| MODEL_URL = "https://huggingface.co/munnabhaimbbsfail/linear_regression_model/resolve/main/model.pkl" | |
| MODEL_PATH = "model.pkl" | |
| # Download the model if not already present | |
| if not os.path.exists(MODEL_PATH): | |
| response = requests.get(MODEL_URL) | |
| with open(MODEL_PATH, "wb") as f: | |
| f.write(response.content) | |
| # Load model | |
| model = joblib.load(MODEL_PATH) | |
| # Define prediction function | |
| def predict(x_values: str): | |
| try: | |
| x_list = [float(x.strip()) for x in x_values.split(",")] | |
| df = pd.DataFrame({"x": x_list}) | |
| preds = model.predict(df) | |
| return {"predictions": preds.tolist()} | |
| except Exception as e: | |
| return {"error": str(e)} | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Textbox(label="Enter x values (comma-separated)", placeholder="e.g., 5, 10, 15"), | |
| outputs="json", | |
| title="Linear Regression Predictor", | |
| description="Enter a list of x values to get predictions from the trained model." | |
| ) | |
| iface.launch() | |