Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, HTTPException | |
import tensorflow as tf | |
import joblib | |
import numpy as np | |
import requests | |
app = FastAPI() | |
# Load model and scaler | |
model_bagemann = tf.keras.models.load_model('./bagemann_model.h5') | |
scaler_bagemann = joblib.load('./scaler_bagemann.joblib') | |
model_shertmann = tf.keras.models.load_model('./shermann_model.h5') | |
scaler_shertmann = joblib.load('./scaler_shertmann.joblib') | |
# Set the URL of your external API endpoint | |
API_BASE_URL = "https://soil-api-rho.vercel.app/projects" # Replace with the actual URL | |
async def predict_and_add(data: dict): | |
id_project = data.get("id") | |
depth = data.get("depth") | |
HL = data.get("HL") | |
HB = data.get("HB") | |
FR = data.get("FR") | |
point = data.get("point") | |
# Check if input values are provided | |
if id_project is None or depth is None or HL is None or HB is None or FR is None or point is None: | |
raise HTTPException(status_code=400, detail="Please provide correct values") | |
# Scale and predict | |
input_bagemann = np.array([[HB,HL]]) | |
bagemann_scaled = scaler_bagemann.transform(input_bagemann) | |
prediction_bagemann = model_bagemann.predict(bagemann_scaled) | |
predicted_class_bagemann = int(np.argmax(prediction_bagemann, axis=1)[0]) | |
# Scale and predict | |
input_shertmann = np.array([[HB,FR]]) | |
shertmann_scaled = scaler_shertmann.transform(input_shertmann) | |
prediction_shertmann = model_shertmann.predict(shertmann_scaled) | |
predicted_class_shertmann = int(np.argmax(prediction_shertmann, axis=1)[0]) | |
# Prepare data to send to /add/data/:id endpoint | |
add_data_payload = { | |
"kedalaman": depth, | |
"HL": HL, | |
"HB": HB, | |
"FR": FR, | |
"bagemann": predicted_class_bagemann, # Use predicted class here if applicable | |
"shertmann": predicted_class_shertmann, # Or map appropriately | |
"titik": point | |
} | |
try: | |
# Send POST request to add data to the project | |
response = requests.post(f"{API_BASE_URL}/add/data/{id_project}", json=add_data_payload) | |
response.raise_for_status() | |
result = response.json() | |
except requests.exceptions.RequestException as e: | |
raise HTTPException(status_code=500, detail=f"Error adding data to project: {e}") | |
return { "add_data_response": result} | |