Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1AAv25Dmp5rMgoK7FgBmpAip9IZCt1KFK
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from fastapi import FastAPI
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
|
| 14 |
+
# FastAPI instance initialization
|
| 15 |
+
app = FastAPI()
|
| 16 |
+
|
| 17 |
+
# Allowing CORS to let Streamlit access the API
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"], # Allow any origin
|
| 21 |
+
allow_methods=["*"], # Allow any method
|
| 22 |
+
allow_headers=["*"], # Allow any headers
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Request model for task input
|
| 26 |
+
class TaskRequest(BaseModel):
|
| 27 |
+
task: str
|
| 28 |
+
|
| 29 |
+
@app.post("/predict")
|
| 30 |
+
def predict_model(req: TaskRequest):
|
| 31 |
+
# Example logic: differentiate between regression and classification tasks
|
| 32 |
+
if "price" in req.task.lower():
|
| 33 |
+
return {
|
| 34 |
+
"model": "Linear Regression",
|
| 35 |
+
"r_squared": 0.82,
|
| 36 |
+
"coefficients": {"area": 2.3, "bedrooms": -0.7}
|
| 37 |
+
}
|
| 38 |
+
else:
|
| 39 |
+
return {
|
| 40 |
+
"model": "Logistic Regression",
|
| 41 |
+
"accuracy": 0.78,
|
| 42 |
+
"coefficients": {"feature1": 1.1, "feature2": -0.3}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Start FastAPI app on Hugging Face Space
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
import uvicorn
|
| 48 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|