JohnDoee commited on
Commit
25b797a
·
1 Parent(s): ee8d81b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -16
main.py CHANGED
@@ -1,11 +1,14 @@
1
  import os
2
- from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  from transformers import pipeline
5
- import langdetect
6
 
7
- # Set custom cache directory to avoid permission issues
8
- os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
 
 
 
9
 
10
  app = FastAPI()
11
 
@@ -22,9 +25,9 @@ class SentimentResponse(BaseModel):
22
  sentiment: str
23
  confidence_score: float
24
 
25
- def detect_language(text: str) -> str:
26
  try:
27
- return langdetect.detect(text)
28
  except:
29
  return "unknown"
30
 
@@ -34,24 +37,18 @@ def home():
34
 
35
  @app.post("/analyze/", response_model=SentimentResponse)
36
  def analyze_sentiment(request: SentimentRequest):
37
- if not request.text:
38
- raise HTTPException(status_code=400, detail="No text provided")
39
-
40
  text = request.text
41
  language = detect_language(text)
42
-
43
  # Choose the appropriate model based on language
44
  if language == "en":
45
  result = english_model(text)
46
  else:
47
  result = multilingual_model(text)
48
-
49
- sentiment = result[0]["label"].lower()
50
- score = result[0]["score"]
51
-
52
  return SentimentResponse(
53
  original_text=text,
54
  language_detected=language,
55
- sentiment=sentiment,
56
- confidence_score=score
57
  )
 
1
  import os
2
+ from fastapi import FastAPI
3
  from pydantic import BaseModel
4
  from transformers import pipeline
5
+ from langdetect import detect, DetectorFactory
6
 
7
+ # Ensure consistent language detection results
8
+ DetectorFactory.seed = 0
9
+
10
+ # Set HF cache directory instead of TRANSFORMERS_CACHE
11
+ os.environ["HF_HOME"] = "/app/cache"
12
 
13
  app = FastAPI()
14
 
 
25
  sentiment: str
26
  confidence_score: float
27
 
28
+ def detect_language(text):
29
  try:
30
+ return detect(text)
31
  except:
32
  return "unknown"
33
 
 
37
 
38
  @app.post("/analyze/", response_model=SentimentResponse)
39
  def analyze_sentiment(request: SentimentRequest):
 
 
 
40
  text = request.text
41
  language = detect_language(text)
42
+
43
  # Choose the appropriate model based on language
44
  if language == "en":
45
  result = english_model(text)
46
  else:
47
  result = multilingual_model(text)
48
+
 
 
 
49
  return SentimentResponse(
50
  original_text=text,
51
  language_detected=language,
52
+ sentiment=result[0]["label"].lower(),
53
+ confidence_score=result[0]["score"],
54
  )