habulaj commited on
Commit
26e71ce
·
1 Parent(s): b41bf5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -1,19 +1,25 @@
1
  from fastapi import FastAPI, Query
2
- from googletrans import Translator
3
 
4
  app = FastAPI()
5
 
6
- translator = Translator()
7
-
8
- @app.get("/translate/")
9
- def translate_text(text: str = Query(..., description="Texto a ser traduzido"),
10
- target_lang: str = Query(..., description="Idioma de destino")):
11
  """
12
- Traduz o texto para o idioma especificado.
13
  """
14
- translation = translator.translate(text, dest=target_lang)
 
 
 
 
 
 
 
 
 
15
  return {
16
- "original_text": text,
17
- "translated_text": translation.text,
18
- "language": translation.src
19
  }
 
1
  from fastapi import FastAPI, Query
2
+ from textblob import TextBlob
3
 
4
  app = FastAPI()
5
 
6
+ @app.get("/sentiment/")
7
+ def analyze_sentiment(text: str = Query(..., description="Texto a ser analisado")):
 
 
 
8
  """
9
+ Analisa o sentimento de um texto e retorna se é positivo, negativo ou neutro.
10
  """
11
+ blob = TextBlob(text)
12
+ sentiment = blob.sentiment.polarity
13
+
14
+ if sentiment > 0:
15
+ sentiment_label = "positivo"
16
+ elif sentiment < 0:
17
+ sentiment_label = "negativo"
18
+ else:
19
+ sentiment_label = "neutro"
20
+
21
  return {
22
+ "text": text,
23
+ "sentiment": sentiment_label,
24
+ "polarity": sentiment
25
  }