Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| import pickle | |
| import numpy as np | |
| from pydantic import BaseModel | |
| from tensorflow.keras.models import model_from_json | |
| import uvicorn | |
| # Load model | |
| with open("model.json", "r") as json_file: | |
| loaded_model_json = json_file.read() | |
| model = model_from_json(loaded_model_json) | |
| model.load_weights("model.weights.h5") | |
| # Load vectorizer | |
| with open("vectorizer.pkl", "rb") as file: | |
| vectorizer = pickle.load(file) | |
| # Load label encoder | |
| with open("label_encoder.pkl", "rb") as file: | |
| label_encoder = pickle.load(file) | |
| # FastAPI app | |
| app = FastAPI() | |
| # Request schema | |
| class CommentInput(BaseModel): | |
| text: str | |
| def predict(data: CommentInput): | |
| text_vectorized = vectorizer.transform([data.text]).toarray() | |
| prediction = model.predict(text_vectorized) | |
| predicted_label = int(label_encoder.inverse_transform([np.argmax(prediction)])[0]) | |
| return { | |
| "text": data.text, | |
| "predicted_label": predicted_label | |
| } | |
| # Run API locally (Opsional) | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |