Shridhartd commited on
Commit
2b51c04
·
verified ·
1 Parent(s): bce3374

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -1,9 +1,36 @@
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
  import uvicorn
 
 
 
 
4
 
5
  app = FastAPI()
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  class InputText(BaseModel):
8
  text: str
9
 
@@ -16,3 +43,4 @@ def predict_sentiment(data: InputText):
16
 
17
  if __name__ == "__main__":
18
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
  import uvicorn
4
+ import torch
5
+ import numpy as np
6
+ from transformers import AutoTokenizer, AutoModel
7
+ from sklearn.linear_model import LogisticRegression
8
 
9
  app = FastAPI()
10
 
11
+ # Load Hugging Face model
12
+ model_name = "bert-base-uncased"
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+ model = AutoModel.from_pretrained(model_name)
15
+
16
+ # Function to get text embeddings
17
+ def get_embedding(text):
18
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
19
+ with torch.no_grad():
20
+ outputs = model(**inputs)
21
+ return outputs.last_hidden_state[:, 0, :].numpy()
22
+
23
+ # Sample dataset
24
+ texts = ["I love this!", "This is terrible.", "Fantastic experience!", "I hate it.", "Absolutely wonderful!", "Worst ever!"]
25
+ labels = [1, 0, 1, 0, 1, 0] # 1 = Positive, 0 = Negative
26
+ X = np.vstack([get_embedding(text) for text in texts])
27
+ y = np.array(labels)
28
+
29
+ # Train model
30
+ clf = LogisticRegression()
31
+ clf.fit(X, y)
32
+
33
+ # API Input Model
34
  class InputText(BaseModel):
35
  text: str
36
 
 
43
 
44
  if __name__ == "__main__":
45
  uvicorn.run(app, host="0.0.0.0", port=7860)
46
+