Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
from transformers import BartForConditionalGeneration, TapexTokenizer, T5ForConditionalGeneration, T5Tokenizer
|
4 |
from prophet import Prophet
|
|
|
5 |
|
6 |
# Abrindo e lendo o arquivo CSS
|
7 |
with open("style.css", "r") as css:
|
@@ -26,39 +26,22 @@ html_content = f"""
|
|
26 |
# Aplicar o markdown combinado no Streamlit
|
27 |
st.markdown(html_content, unsafe_allow_html=True)
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
def load_tapex_model():
|
36 |
-
return BartForConditionalGeneration.from_pretrained("microsoft/tapex-large-finetuned-wtq")
|
37 |
-
|
38 |
-
#@st.cache_resource
|
39 |
-
def load_tapex_tokenizer():
|
40 |
-
return TapexTokenizer.from_pretrained("microsoft/tapex-large-finetuned-wtq")
|
41 |
-
|
42 |
-
pt_en_translator = load_translation_model("unicamp-dl/translation-pt-en-t5")
|
43 |
-
en_pt_translator = load_translation_model("unicamp-dl/translation-en-pt-t5")
|
44 |
-
tapex_model = load_tapex_model()
|
45 |
-
tapex_tokenizer = load_tapex_tokenizer()
|
46 |
-
tokenizer = T5Tokenizer.from_pretrained("unicamp-dl/translation-pt-en-t5")
|
47 |
-
|
48 |
-
def translate(text, model, tokenizer, source_lang="pt", target_lang="en"):
|
49 |
-
input_ids = tokenizer.encode(text, return_tensors="pt", add_special_tokens=True)
|
50 |
-
outputs = model.generate(input_ids)
|
51 |
-
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
52 |
-
return translated_text
|
53 |
-
|
54 |
-
# Function to translate and interact with TAPEX model
|
55 |
def response(user_question, all_anomalies):
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
return
|
62 |
|
63 |
# Load and preprocess the data
|
64 |
def load_data(uploaded_file):
|
@@ -77,23 +60,23 @@ def preprocess_data(df):
|
|
77 |
'Mai': '05', 'Jun': '06', 'Jul': '07', 'Ago': '08',
|
78 |
'Set': '09', 'Out': '10', 'Nov': '11', 'Dez': '12'
|
79 |
}
|
80 |
-
|
81 |
def convert_column_name(column_name):
|
82 |
# Check if the column name is 'R贸tulos de Linha'
|
83 |
if column_name == 'R贸tulos de Linha':
|
84 |
return column_name
|
85 |
-
|
86 |
# Otherwise, proceed to convert
|
87 |
parts = column_name.split('/')
|
88 |
month = parts[0].strip()
|
89 |
year = parts[1].strip()
|
90 |
-
|
91 |
# Clean year in case there are extra characters
|
92 |
year = ''.join(filter(str.isdigit, year))
|
93 |
-
|
94 |
# Get month number from the dictionary
|
95 |
month_number = month_dict.get(month, '00') # Default '00' if month is not found
|
96 |
-
|
97 |
# Return formatted date string
|
98 |
return f"{month_number}/{year}"
|
99 |
|
@@ -116,7 +99,7 @@ def apply_prophet(df_clean):
|
|
116 |
|
117 |
# Criar um DataFrame vazio para armazenar todas as anomalias
|
118 |
all_anomalies = pd.DataFrame()
|
119 |
-
|
120 |
# Processar cada linha no DataFrame
|
121 |
for index, row in df_clean.iterrows():
|
122 |
# Extract timestamp and value columns
|
@@ -188,16 +171,11 @@ with tab1:
|
|
188 |
if uploaded_file:
|
189 |
df = load_data(uploaded_file)
|
190 |
df_clean = preprocess_data(df)
|
191 |
-
|
192 |
if df_clean.empty:
|
193 |
st.warning("N茫o h谩 dados v谩lidos para processar.")
|
194 |
else:
|
195 |
# Cache the Prophet results
|
196 |
-
#if st.session_state['all_anomalies'].empty:
|
197 |
-
#with st.spinner('Aplicando modelo de s茅rie temporal...'):
|
198 |
-
#all_anomalies = apply_prophet(df_clean)
|
199 |
-
#st.session_state['all_anomalies'] = all_anomalies
|
200 |
-
# Cache the Prophet results
|
201 |
if st.session_state['all_anomalies'].empty:
|
202 |
all_anomalies = apply_prophet(df_clean)
|
203 |
st.session_state['all_anomalies'] = all_anomalies
|
@@ -209,11 +187,11 @@ with tab2:
|
|
209 |
bot_response = response(user_question, st.session_state['all_anomalies'])
|
210 |
st.session_state['history'].append(('馃懁', user_question))
|
211 |
st.session_state['history'].append(('馃', bot_response))
|
212 |
-
|
213 |
for sender, message in st.session_state['history']:
|
214 |
st.markdown(f"**{sender} {message}**")
|
215 |
-
|
216 |
if st.button("Limpar hist贸rico"):
|
217 |
st.session_state['history'] = []
|
218 |
else:
|
219 |
-
st.warning("Por favor, processe os dados no Meta Prophet primeiro.")
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
from prophet import Prophet
|
4 |
+
import openai
|
5 |
|
6 |
# Abrindo e lendo o arquivo CSS
|
7 |
with open("style.css", "r") as css:
|
|
|
26 |
# Aplicar o markdown combinado no Streamlit
|
27 |
st.markdown(html_content, unsafe_allow_html=True)
|
28 |
|
29 |
+
# Configurar a API do OpenRouter
|
30 |
+
openai.api_base = "https://openrouter.ai/api/v1"
|
31 |
+
openai.api_key = "<OPENROUTER_API_KEY>"
|
32 |
+
openai.default_headers = {
|
33 |
+
"HTTP-Referer": "<https://huggingface.co/spaces/fschwartzer/streamlit_chatbot/>", # Optional. Site URL for rankings on openrouter.ai.
|
34 |
+
"X-Title": "<Streamlit Chatbot>", # Optional. Site title for rankings on openrouter.ai.
|
35 |
+
}
|
36 |
|
37 |
+
# Function to interact with the model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def response(user_question, all_anomalies):
|
39 |
+
prompt = f"Considerando a seguinte tabela:\n{all_anomalies.to_string(index=False)}\nResponda a quest茫o: {user_question}"
|
40 |
+
response = openai.ChatCompletion.create(
|
41 |
+
model="deepseek/deepseek-r1:free",
|
42 |
+
messages=[{"role": "user", "content": prompt}]
|
43 |
+
)
|
44 |
+
return response.choices[0].message['content']
|
45 |
|
46 |
# Load and preprocess the data
|
47 |
def load_data(uploaded_file):
|
|
|
60 |
'Mai': '05', 'Jun': '06', 'Jul': '07', 'Ago': '08',
|
61 |
'Set': '09', 'Out': '10', 'Nov': '11', 'Dez': '12'
|
62 |
}
|
63 |
+
|
64 |
def convert_column_name(column_name):
|
65 |
# Check if the column name is 'R贸tulos de Linha'
|
66 |
if column_name == 'R贸tulos de Linha':
|
67 |
return column_name
|
68 |
+
|
69 |
# Otherwise, proceed to convert
|
70 |
parts = column_name.split('/')
|
71 |
month = parts[0].strip()
|
72 |
year = parts[1].strip()
|
73 |
+
|
74 |
# Clean year in case there are extra characters
|
75 |
year = ''.join(filter(str.isdigit, year))
|
76 |
+
|
77 |
# Get month number from the dictionary
|
78 |
month_number = month_dict.get(month, '00') # Default '00' if month is not found
|
79 |
+
|
80 |
# Return formatted date string
|
81 |
return f"{month_number}/{year}"
|
82 |
|
|
|
99 |
|
100 |
# Criar um DataFrame vazio para armazenar todas as anomalias
|
101 |
all_anomalies = pd.DataFrame()
|
102 |
+
|
103 |
# Processar cada linha no DataFrame
|
104 |
for index, row in df_clean.iterrows():
|
105 |
# Extract timestamp and value columns
|
|
|
171 |
if uploaded_file:
|
172 |
df = load_data(uploaded_file)
|
173 |
df_clean = preprocess_data(df)
|
174 |
+
|
175 |
if df_clean.empty:
|
176 |
st.warning("N茫o h谩 dados v谩lidos para processar.")
|
177 |
else:
|
178 |
# Cache the Prophet results
|
|
|
|
|
|
|
|
|
|
|
179 |
if st.session_state['all_anomalies'].empty:
|
180 |
all_anomalies = apply_prophet(df_clean)
|
181 |
st.session_state['all_anomalies'] = all_anomalies
|
|
|
187 |
bot_response = response(user_question, st.session_state['all_anomalies'])
|
188 |
st.session_state['history'].append(('馃懁', user_question))
|
189 |
st.session_state['history'].append(('馃', bot_response))
|
190 |
+
|
191 |
for sender, message in st.session_state['history']:
|
192 |
st.markdown(f"**{sender} {message}**")
|
193 |
+
|
194 |
if st.button("Limpar hist贸rico"):
|
195 |
st.session_state['history'] = []
|
196 |
else:
|
197 |
+
st.warning("Por favor, processe os dados no Meta Prophet primeiro.")
|