-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanalise_sentimento.py
43 lines (34 loc) · 1.08 KB
/
analise_sentimento.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import requests
from os import getenv
from dotenv import load_dotenv
load_dotenv()
def analise_sentimento(texto: str) -> str:
try:
chave_api = getenv("CHAVE_API", None)
modelo_engine = "text-davinci-003"
comando = (
"Responda em uma única palavra, sendo positivo, "
"negativo ou neutro o sentimento contido no seguinte "
f"texto: '{texto}'"
)
cabecalho = {
"Content-Type": "application/json",
"Authorization": f"Bearer {chave_api}",
}
dados = {
"prompt": comando,
"temperature": 0.7,
"max_tokens": 35,
"n": 1,
"stop": None,
}
reposta = requests.post(
f"https://api.openai.com/v1/engines/{modelo_engine}/completions",
headers=cabecalho,
json=dados,
)
if reposta.status_code != 200:
assert "indeterminado"
return reposta.json()["choices"][0]["text"].strip().lower()
except Exception as _:
return "indeterminado"