rafaelcleversystems commited on
Commit
4fb799c
·
verified ·
1 Parent(s): 6c2a93e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +68 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,70 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from openai import OpenAI
3
+ from dotenv import load_dotenv
4
+ import os
5
 
6
+ # Carrega variáveis do arquivo .env
7
+ load_dotenv()
8
+ api_key = os.getenv("OPENAI_API_KEY")
9
+
10
+ # Inicializa o cliente OpenAI
11
+ client = OpenAI(api_key=api_key)
12
+
13
+ # Configuração da página
14
+ st.set_page_config(page_title="Classificador de Texto", page_icon="🤖")
15
+
16
+ st.title("🤖 Classificador de Texto com GPT-4.1-nano")
17
+
18
+ # Entrada de texto multilinha
19
+ texto = st.text_area(
20
+ "Digite o texto que deseja classificar:",
21
+ placeholder="Exemplo: O atendimento foi ótimo, mas o preço é alto.",
22
+ height=150
23
+ )
24
+
25
+ # Botão de ação
26
+ if st.button("Classificar"):
27
+ if not texto.strip():
28
+ st.warning("Por favor, digite um texto antes de classificar.")
29
+ else:
30
+ with st.spinner("Analisando o texto..."):
31
+ prompt = f"""
32
+ Classifique o sentimento do seguinte texto como Positivo, Negativo ou Neutro:
33
+ Texto: "{texto}"
34
+ Responda apenas com uma das opções.
35
+ """
36
+
37
+ resposta = client.chat.completions.create(
38
+ model="gpt-4.1-nano",
39
+ messages=[
40
+ {"role": "system", "content": "Você é um classificador de texto."},
41
+ {"role": "user", "content": prompt}
42
+ ],
43
+ temperature=0
44
+ )
45
+
46
+ classificacao = resposta.choices[0].message.content.strip().lower()
47
+
48
+ # Define cor e ícone conforme classificação
49
+ if "positivo" in classificacao:
50
+ cor = "#00C853" # verde
51
+ icone = "😊"
52
+ texto_label = "Positivo"
53
+ elif "negativo" in classificacao:
54
+ cor = "#D50000" # vermelho
55
+ icone = "😠"
56
+ texto_label = "Negativo"
57
+ else:
58
+ cor = "#FFD600" # amarelo
59
+ icone = "😐"
60
+ texto_label = "Neutro"
61
+
62
+ # Exibe resultado com cor e ícone
63
+ st.markdown(
64
+ f"""
65
+ <div style='background-color:{cor}; padding:15px; border-radius:10px; text-align:center;'>
66
+ <h3 style='color:white;'>{icone} Classificação: {texto_label}</h3>
67
+ </div>
68
+ """,
69
+ unsafe_allow_html=True
70
+ )