Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """app.ipynb | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1AAv25Dmp5rMgoK7FgBmpAip9IZCt1KFK | |
| """ | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| # FastAPI instance initialization | |
| app = FastAPI() | |
| # Allowing CORS to let Streamlit access the API | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Allow any origin | |
| allow_methods=["*"], # Allow any method | |
| allow_headers=["*"], # Allow any headers | |
| ) | |
| # Request model for task input | |
| class TaskRequest(BaseModel): | |
| task: str | |
| def predict_model(req: TaskRequest): | |
| # Example logic: differentiate between regression and classification tasks | |
| if "price" in req.task.lower(): | |
| return { | |
| "model": "Linear Regression", | |
| "r_squared": 0.82, | |
| "coefficients": {"area": 2.3, "bedrooms": -0.7} | |
| } | |
| else: | |
| return { | |
| "model": "Logistic Regression", | |
| "accuracy": 0.78, | |
| "coefficients": {"feature1": 1.1, "feature2": -0.3} | |
| } | |
| # Start FastAPI app on Hugging Face Space | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("app:app", host="0.0.0.0", port=7860) |