Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 5 |
+
|
| 6 |
+
# Carregando o modelo e o tokenizador do GPT-2
|
| 7 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
| 8 |
+
model = GPT2LMHeadModel.from_pretrained('gpt2')
|
| 9 |
+
|
| 10 |
+
df = pd.read_csv('anomalies.csv')
|
| 11 |
+
|
| 12 |
+
# Função para gerar resposta
|
| 13 |
+
def response(question):
|
| 14 |
+
prompt = f"Considerando os dados: {df.to_string(index=False)}. Pergunta: {question} Resposta:"
|
| 15 |
+
inputs = tokenizer(prompt, return_tensors='pt', padding='max_length', truncation=True, max_length=512)
|
| 16 |
+
attention_mask = inputs['attention_mask']
|
| 17 |
+
input_ids = inputs['input_ids']
|
| 18 |
+
|
| 19 |
+
generated_ids = model.generate(
|
| 20 |
+
input_ids,
|
| 21 |
+
attention_mask=attention_mask,
|
| 22 |
+
max_length=len(input_ids[0]) + 100, # Aumentar o limite de geração
|
| 23 |
+
temperature=0.65, # Ajustar a criatividade
|
| 24 |
+
top_p=0.9, # Usar nucleus sampling
|
| 25 |
+
no_repeat_ngram_size=2 # Evitar repetições desnecessárias
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 29 |
+
# Processando para extrair apenas a resposta após "Resposta:"
|
| 30 |
+
response_part = generated_text.split("Resposta:")[1] if "Resposta:" in generated_text else "Resposta não encontrada."
|
| 31 |
+
final_response = response_part.split(".")[0] + "." # Assumindo que a resposta termina na primeira sentença.
|
| 32 |
+
|
| 33 |
+
return final_response
|
| 34 |
+
|
| 35 |
+
# Interface Streamlit
|
| 36 |
+
st.title("Chatbot com Streamlit")
|
| 37 |
+
|
| 38 |
+
# Histórico de conversas
|
| 39 |
+
if 'history' not in st.session_state:
|
| 40 |
+
st.session_state['history'] = []
|
| 41 |
+
|
| 42 |
+
# Caixa de entrada para a pergunta
|
| 43 |
+
user_question = st.text_input("Escreva sua questão aqui:", "")
|
| 44 |
+
|
| 45 |
+
if user_question:
|
| 46 |
+
# Adiciona emoji de pessoa quando a pergunta está sendo digitada
|
| 47 |
+
st.session_state['history'].append(('👤', user_question))
|
| 48 |
+
st.write(f"👤 {user_question}")
|
| 49 |
+
|
| 50 |
+
# Gera a resposta
|
| 51 |
+
bot_response = response(user_question)
|
| 52 |
+
|
| 53 |
+
# Adiciona emoji de robô quando a resposta está sendo gerada
|
| 54 |
+
st.session_state['history'].append(('🤖', bot_response))
|
| 55 |
+
st.write(f"🤖 {bot_response}")
|
| 56 |
+
|
| 57 |
+
# Botão para limpar o histórico
|
| 58 |
+
if st.button("Limpar"):
|
| 59 |
+
st.session_state['history'] = []
|
| 60 |
+
|
| 61 |
+
# Exibe o histórico de conversas
|
| 62 |
+
for sender, message in st.session_state['history']:
|
| 63 |
+
if sender == '👤':
|
| 64 |
+
st.write(f"👤 {message}")
|
| 65 |
+
elif sender == '🤖':
|
| 66 |
+
st.write(f"🤖 {message}")
|