-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
96 lines (82 loc) · 2.77 KB
/
Copy pathmain.py
File metadata and controls
96 lines (82 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from biomass_yield_predictor import predict_yield, load_model
app = FastAPI(title="EnzyMax AI API (Production)")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Global model variables
g_bundle = None
x_bundle = None
@app.on_event("startup")
def load_models():
global g_bundle, x_bundle
print("Loading ML models for Production...")
try:
g_bundle = load_model(os.path.join('outputs', 'glucose_model.pkl'))
print("✅ Glucose model loaded.")
except Exception as e:
print(f"⚠️ Glucose model error: {e}")
try:
x_bundle = load_model(os.path.join('outputs', 'xylose_model.pkl'))
print("✅ Xylose model loaded.")
except Exception as e:
print(f"⚠️ Xylose model error: {e}")
class BiomassInput(BaseModel):
Glucan: float
Hemi: float
Lignin: float
Moisture: float
PreTemp: float
PreTime: float
HydTemp: float
HydTime: float
pH: float
EnzFPU: float
EnzMg: float
SevFactor: float
Pre_treatment: str
Feed: str
@app.get("/")
def home():
return {"message": "EnzyMax API is Live on Hugging Face Spaces! 🚀"}
@app.post("/predict")
def predict(data: BiomassInput):
try:
input_dict = {
'Glucan Content (% dry basis)': data.Glucan,
'Hemicellulose Content (% dry basis)': data.Hemi,
'Lignin Content (% dry basis)': data.Lignin,
'Moisture Content (kg water/kg dry biomass)': data.Moisture,
'Pretreatment Temperature (°C)': data.PreTemp,
'Pretreatment Time (h)': data.PreTime,
'Conversion Temperature (°C)': data.HydTemp,
'Hydrolysis Time(h)': data.HydTime,
'pH': data.pH,
'Enzyme loading (FPU/g dry biomass)': data.EnzFPU,
'Enzyme loading (mg / g glucan)': data.EnzMg,
'Severity Factor': data.SevFactor,
'Pre-treatment': data.Pre_treatment,
'Feed': data.Feed
}
# Prediction function
result = predict_yield(input_dict, _g_bundle=g_bundle, _x_bundle=x_bundle)
return {
"success": True,
"glucose_yield_pct": result["glucose_yield_pct"],
"xylose_yield_pct": result["xylose_yield_pct"],
"note": result["note"]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
# Hugging Face Spaces defaults to port 7860
port = int(os.environ.get("PORT", 7860))
uvicorn.run("main:app", host="0.0.0.0", port=port)