service-internal commited on
Commit
0a4ba60
·
verified ·
1 Parent(s): 6644ffe

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -5
main.py CHANGED
@@ -11,10 +11,12 @@ app = FastAPI()
11
 
12
  MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
13
 
 
14
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
15
  config = AutoConfig.from_pretrained(MODEL)
16
  model = AutoModelForSequenceClassification.from_pretrained(MODEL)
17
 
 
18
  def preprocess(text):
19
  tokens = []
20
  for t in text.split():
@@ -28,14 +30,34 @@ def preprocess(text):
28
  @app.post("/analyze")
29
  async def analyze(request: Request):
30
  data = await request.json()
31
- text = preprocess(data.get("text", ""))
32
- encoded_input = tokenizer(text, return_tensors='pt')
 
 
 
 
 
 
 
 
 
 
 
 
33
  output = model(**encoded_input)
34
  scores = output[0][0].detach().numpy()
35
- scores = softmax(scores)
 
 
 
 
 
36
  result = [
37
- {"label": config.id2label[i], "score": round(float(scores[i]), 4)}
38
- for i in scores.argsort()[::-1]
39
  ]
 
 
40
  return {"result": result}
41
 
 
 
11
 
12
  MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
13
 
14
+ # Load model and tokenizer
15
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
16
  config = AutoConfig.from_pretrained(MODEL)
17
  model = AutoModelForSequenceClassification.from_pretrained(MODEL)
18
 
19
+ # Preprocessing step for Twitter-style input
20
  def preprocess(text):
21
  tokens = []
22
  for t in text.split():
 
30
  @app.post("/analyze")
31
  async def analyze(request: Request):
32
  data = await request.json()
33
+ raw_text = data.get("text", "")
34
+
35
+ # Logging for debugging
36
+ print(f"Raw input: {raw_text}")
37
+
38
+ if not raw_text.strip():
39
+ return {"error": "Empty input text."}
40
+
41
+ text = preprocess(raw_text)
42
+ print(f"Preprocessed: {text}")
43
+
44
+ encoded_input = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
45
+ print(f"Encoded input: {encoded_input.input_ids}")
46
+
47
  output = model(**encoded_input)
48
  scores = output[0][0].detach().numpy()
49
+ probs = softmax(scores)
50
+
51
+ # Logging output
52
+ print(f"Raw scores: {scores}")
53
+ print(f"Softmax probs: {probs}")
54
+
55
  result = [
56
+ {"label": config.id2label[i], "score": round(float(probs[i]), 4)}
57
+ for i in probs.argsort()[::-1]
58
  ]
59
+
60
+ print(f"Result: {result}")
61
  return {"result": result}
62
 
63
+