Spaces:
Runtime error
Runtime error
from fastapi import FastAPI, Query, Request, HTTPException | |
import pandas as pd | |
import transformers as pipeline | |
from transformers import AutoTokenizer,AutoModelForSequenceClassification | |
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline | |
model_name = "Sonny4Sonnix/Movie_Sentiments_Analysis_with_FastAPI" # Replace with the name of the pre-trained model you want to use | |
model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
app = FastAPI() | |
async def read_root(): | |
return {"message": "Welcome to the Sepsis Prediction using FastAPI"} | |
def classify(prediction): | |
if prediction == 0: | |
return "Sentence is positive" | |
else: | |
return "Sentence is negative" | |
async def predict_sepsis( | |
request: Request, | |
Text: float = Query(..., description="Please type a sentence"), | |
# pl: float = Query(..., description="Blood Work Result-1 (mu U/ml)"), | |
# pr: float = Query(..., description="Blood Pressure (mm Hg)"), | |
# sk: float = Query(..., description="Blood Work Result-2 (mm)"), | |
# ts: float = Query(..., description="Blood Work Result-3 (mu U/ml)"), | |
# m11: float = Query(..., description="Body mass index (weight in kg/(height in m)^2"), | |
# bd2: float = Query(..., description="Blood Work Result-4 (mu U/ml)"), | |
# age: int = Query(..., description="Patient's age (years)") | |
): | |
#input_data = [prg, pl, pr, sk, ts, m11, bd2, age] | |
input_data = [Text] | |
# input_df = pd.DataFrame([input_data], columns=[ | |
# "Plasma glucose", "Blood Work Result-1", "Blood Pressure", | |
# "Blood Work Result-2", "Blood Work Result-3", | |
# "Body mass index", "Blood Work Result-4", "Age" | |
input_df = pd.DataFrame([input_data], columns=[ | |
"Text" | |
]) | |
pred = model.predict(input_df) | |
output = classify(pred[0]) | |
response = { | |
"prediction": output | |
} | |
return response | |
# Run the app using Uvicorn | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="127.0.0.1", port=7860) | |
sentiment = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) | |
# def get_sentiment(input_text): | |
# result = sentiment(input_text) | |
# sentiment_label = result[0]['label'] | |
# sentiment_score = result[0]['score'] | |
# if sentiment_label == 'LABEL_1': | |
# sentiment_label = "positive" | |
# elif sentiment_label == 'LABEL_0': | |
# sentiment_label = "neutral" | |
# else: | |
# sentiment_label = "negative" | |
# return f"Sentiment: {sentiment_label.capitalize()}, Score: {sentiment_score:.2f}" | |