CaerusTech commited on
Commit
5dd965b
·
1 Parent(s): c064f5d
Files changed (1) hide show
  1. app.py +52 -6
app.py CHANGED
@@ -1,26 +1,72 @@
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
 
3
  from transformers import pipeline
4
 
5
  app = FastAPI(title="Sentimind API")
6
 
7
  # Load sentiment analysis model
8
- sentiment_analyzer = pipeline("sentiment-analysis",
9
- model="distilbert-base-uncased-finetuned-sst-2-english")
 
 
10
 
11
  class InputText(BaseModel):
12
  text: str
13
 
 
 
 
14
  @app.get("/")
15
  def root():
16
  return {"message": "Hello from Sentimind API!"}
17
 
18
- @app.post("/analyze/sentiment")
19
- def analyze_sentiment(input_data: InputText):
20
- # Run model inference
21
- result = sentiment_analyzer(input_data.text)[0] # returns {'label': 'POSITIVE', 'score': 0.99}
 
 
 
 
 
22
  return {
23
  "input_text": input_data.text,
24
  "sentiment": result['label'].lower(),
25
  "confidence": float(result['score'])
26
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
+ from typing import List
4
  from transformers import pipeline
5
 
6
  app = FastAPI(title="Sentimind API")
7
 
8
  # Load sentiment analysis model
9
+ sentiment_analyzer = pipeline(
10
+ "sentiment-analysis",
11
+ model="distilbert-base-uncased-finetuned-sst-2-english"
12
+ )
13
 
14
  class InputText(BaseModel):
15
  text: str
16
 
17
+ class InputBatch(BaseModel):
18
+ texts: List[str]
19
+
20
  @app.get("/")
21
  def root():
22
  return {"message": "Hello from Sentimind API!"}
23
 
24
+ @app.get("/v1/health")
25
+ def health_check():
26
+ """Lightweight health check endpoint"""
27
+ return {"status": "ok", "service": "Sentimind API", "version": "v1"}
28
+
29
+ @app.post("/v1/analyze/sentiment/single")
30
+ def analyze_single(input_data: InputText):
31
+ """Analyze sentiment of a single text"""
32
+ result = sentiment_analyzer(input_data.text)[0]
33
  return {
34
  "input_text": input_data.text,
35
  "sentiment": result['label'].lower(),
36
  "confidence": float(result['score'])
37
  }
38
+
39
+ @app.post("/v1/analyze/sentiment/batch")
40
+ def analyze_batch(input_data: InputBatch):
41
+ """Analyze sentiment of multiple texts and provide overall summary"""
42
+ results = []
43
+ positive_count = 0
44
+ negative_count = 0
45
+
46
+ for text in input_data.texts:
47
+ result = sentiment_analyzer(text)[0]
48
+ sentiment = result['label'].lower()
49
+ confidence = float(result['score'])
50
+ results.append({
51
+ "text": text,
52
+ "sentiment": sentiment,
53
+ "confidence": confidence
54
+ })
55
+
56
+ if sentiment == "positive":
57
+ positive_count += 1
58
+ else:
59
+ negative_count += 1
60
+
61
+ total = len(input_data.texts)
62
+ positive_ratio = positive_count / total if total > 0 else 0
63
+ negative_ratio = negative_count / total if total > 0 else 0
64
+
65
+ overall_sentiment = "positive" if positive_count >= negative_count else "negative"
66
+
67
+ return {
68
+ "results": results,
69
+ "overall_sentiment": overall_sentiment,
70
+ "positive_ratio": round(positive_ratio, 2),
71
+ "negative_ratio": round(negative_ratio, 2)
72
+ }