Spaces:
Sleeping
Sleeping
File size: 2,020 Bytes
780a315 b8cb847 780a315 a0f971c 780a315 a0f971c 780a315 a0f971c 780a315 a0f971c 780a315 | 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 | from fastapi import FastAPI, Request, Header, HTTPException
from pydantic import BaseModel
from typing import Optional
import os
import json
import requests
import tensorflow as tf
from tensorflow.keras.preprocessing.text import tokenizer_from_json
from keras.utils import custom_object_scope
from utils.attention_layer import AttentionLayer
import nltk
nltk.download('punkt_tab')
import pickle
from utils.text_utils import (
build_slang_dictionary,
build_stopwords,
predict_sentiment_per_sentence
)
model = tf.keras.models.load_model('model/best_model.h5',
custom_objects={'AttentionLayer': AttentionLayer})
with open('model/tokenizer.json') as f:
tokenizer = tokenizer_from_json(json.load(f))
with open('model/label_encoder.pkl', 'rb') as f:
label_encoder = pickle.load(f)
max_len = 20
slangwords = build_slang_dictionary()
stopwords = build_stopwords()
# === API ===
app = FastAPI()
class SentimentRequest(BaseModel):
userId: str
content: str
def verify_token(authorization: Optional[str]):
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Invalid or missing token")
token = authorization.split(" ")[1]
return token
@app.post("/analyze")
async def analyze_sentiment(request: SentimentRequest, authorization: Optional[str] = Header(None)):
token = verify_token(authorization)
results = predict_sentiment_per_sentence(
request.content, model, tokenizer, label_encoder, max_len, slangwords, stopwords
)
payload = {
"content": request.content,
"sentiment": results
}
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
response = requests.post("https://tenangin-backend.vercel.app/api/journal/add", headers=headers, json=payload)
return {
"userId": request.userId,
"results": results,
# "api_response": response.status_code
}
|