Sonny4Sonnix commited on
Commit
bf5f79d
·
1 Parent(s): 0e268e0

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +76 -0
main.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query, Request, HTTPException
2
+ import pandas as pd
3
+ import transformers as pipeline
4
+ from transformers import AutoTokenizer,AutoModelForSequenceClassification
5
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
6
+
7
+ model_name = "Sonny4Sonnix/Movie_Sentiments_Analysis_with_FastAPI" # Replace with the name of the pre-trained model you want to use
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+
11
+ app = FastAPI()
12
+
13
+ @app.get("/")
14
+ async def read_root():
15
+ return {"message": "Welcome to the Sepsis Prediction using FastAPI"}
16
+
17
+ def classify(prediction):
18
+ if prediction == 0:
19
+ return "Sentence is positive"
20
+ else:
21
+ return "Sentence is negative"
22
+
23
+
24
+ @app.post("/predict/")
25
+ async def predict_sepsis(
26
+ request: Request,
27
+ Text: float = Query(..., description="Please type a sentence"),
28
+ # pl: float = Query(..., description="Blood Work Result-1 (mu U/ml)"),
29
+ # pr: float = Query(..., description="Blood Pressure (mm Hg)"),
30
+ # sk: float = Query(..., description="Blood Work Result-2 (mm)"),
31
+ # ts: float = Query(..., description="Blood Work Result-3 (mu U/ml)"),
32
+ # m11: float = Query(..., description="Body mass index (weight in kg/(height in m)^2"),
33
+ # bd2: float = Query(..., description="Blood Work Result-4 (mu U/ml)"),
34
+ # age: int = Query(..., description="Patient's age (years)")
35
+ ):
36
+ #input_data = [prg, pl, pr, sk, ts, m11, bd2, age]
37
+ input_data = [Text]
38
+
39
+ # input_df = pd.DataFrame([input_data], columns=[
40
+ # "Plasma glucose", "Blood Work Result-1", "Blood Pressure",
41
+ # "Blood Work Result-2", "Blood Work Result-3",
42
+ # "Body mass index", "Blood Work Result-4", "Age"
43
+
44
+ input_df = pd.DataFrame([input_data], columns=[
45
+ "Text"
46
+ ])
47
+
48
+ pred = model.predict(input_df)
49
+ output = classify(pred[0])
50
+
51
+ response = {
52
+ "prediction": output
53
+ }
54
+
55
+ return response
56
+
57
+ # Run the app using Uvicorn
58
+ if __name__ == "__main__":
59
+ import uvicorn
60
+ uvicorn.run(app, host="127.0.0.1", port=7860)
61
+ sentiment = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
62
+
63
+ # def get_sentiment(input_text):
64
+ # result = sentiment(input_text)
65
+ # sentiment_label = result[0]['label']
66
+ # sentiment_score = result[0]['score']
67
+
68
+ # if sentiment_label == 'LABEL_1':
69
+ # sentiment_label = "positive"
70
+ # elif sentiment_label == 'LABEL_0':
71
+ # sentiment_label = "neutral"
72
+ # else:
73
+ # sentiment_label = "negative"
74
+
75
+ # return f"Sentiment: {sentiment_label.capitalize()}, Score: {sentiment_score:.2f}"
76
+