fschwartzer commited on
Commit
23171a7
1 Parent(s): 61f9700

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
app.py CHANGED
@@ -3,31 +3,38 @@ import pandas as pd
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
 
6
- tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
7
- model = AutoModelForCausalLM.from_pretrained("distilgpt2", torch_dtype=torch.float16)
8
- model = model.to('cuda') if torch.cuda.is_available() else model.to('cpu')
 
 
 
 
 
 
 
9
 
10
  # Set the padding token to the end-of-sequence token
11
  if tokenizer.pad_token is None:
12
  tokenizer.pad_token = tokenizer.eos_token
13
 
14
- df = pd.read_csv('anomalies.csv')
 
15
 
16
- # Função para gerar resposta
17
  def response(question):
18
- prompt = f"Considerando os dados: {df.to_string(index=False)}, onde 'ds' está em formato DateTime, 'real' é o valor da despesa e 'group' é o grupo da despesa. Pergunta: {question}"
19
- inputs = tokenizer(prompt, return_tensors='pt', padding='max_length', truncation=True, max_length=256)
20
- attention_mask = inputs['attention_mask']
21
- input_ids = inputs['input_ids']
22
 
23
  generated_ids = model.generate(
24
- input_ids,
25
- attention_mask=attention_mask,
26
- max_length=len(input_ids[0]) + 50, # Reduce max_length to speed up response
27
  temperature=0.7,
28
  top_p=0.9,
29
  no_repeat_ngram_size=2,
30
- num_beams=3, # Adding beams for more reliable generation
31
  )
32
 
33
  generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
@@ -35,7 +42,7 @@ def response(question):
35
 
36
  return final_response
37
 
38
- # Interface Streamlit
39
  st.markdown("""
40
  <div style='display: flex; align-items: center;'>
41
  <div style='width: 40px; height: 40px; background-color: green; border-radius: 50%; margin-right: 5px;'></div>
@@ -45,30 +52,30 @@ st.markdown("""
45
  </div>
46
  """, unsafe_allow_html=True)
47
 
48
- # Histórico de conversas
49
  if 'history' not in st.session_state:
50
  st.session_state['history'] = []
51
 
52
- # Caixa de entrada para a pergunta
53
  user_question = st.text_input("Escreva sua questão aqui:", "")
54
 
55
  if user_question:
56
- # Adiciona emoji de pessoa quando a pergunta está sendo digitada
57
  st.session_state['history'].append(('👤', user_question))
58
  st.markdown(f"**👤 {user_question}**")
59
 
60
- # Gera a resposta
61
  bot_response = response(user_question)
62
 
63
- # Adiciona emoji de robô quando a resposta está sendo gerada e alinha à direita
64
  st.session_state['history'].append(('🤖', bot_response))
65
  st.markdown(f"<div style='text-align: right'>**🤖 {bot_response}**</div>", unsafe_allow_html=True)
66
 
67
- # Botão para limpar o histórico
68
  if st.button("Limpar"):
69
  st.session_state['history'] = []
70
 
71
- # Exibe o histórico de conversas
72
  for sender, message in st.session_state['history']:
73
  if sender == '👤':
74
  st.markdown(f"**👤 {message}**")
 
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
 
6
+ # Load the tokenizer and quantized model
7
+ model_name = "meta-llama/Meta-Llama-3.1-8B"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+
10
+ # Use bitsandbytes to load the model in 8-bit precision
11
+ model = AutoModelForCausalLM.from_pretrained(model_name, load_in_8bit=True, device_map='auto')
12
+
13
+ # Move model to the appropriate device (GPU/CPU)
14
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+ model = model.to(device)
16
 
17
  # Set the padding token to the end-of-sequence token
18
  if tokenizer.pad_token is None:
19
  tokenizer.pad_token = tokenizer.eos_token
20
 
21
+ # Load the anomalies data
22
+ df = pd.read_csv('anomalies.csv', sep=',', decimal='.')
23
 
24
+ # Function to generate a response
25
  def response(question):
26
+ prompt = f"Considerando os dados: {df.to_string(index=False)}, onde a coluna 'ds' está em formato DateTime, a coluna 'real' é o valor da despesa e a coluna 'group' é o grupo da despesa. Pergunta: {question}"
27
+
28
+ inputs = tokenizer(prompt, return_tensors='pt', padding='max_length', truncation=True, max_length=256).to(device)
 
29
 
30
  generated_ids = model.generate(
31
+ inputs['input_ids'],
32
+ attention_mask=inputs['attention_mask'],
33
+ max_length=inputs['input_ids'].shape[1] + 50,
34
  temperature=0.7,
35
  top_p=0.9,
36
  no_repeat_ngram_size=2,
37
+ num_beams=3,
38
  )
39
 
40
  generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
 
42
 
43
  return final_response
44
 
45
+ # Streamlit interface
46
  st.markdown("""
47
  <div style='display: flex; align-items: center;'>
48
  <div style='width: 40px; height: 40px; background-color: green; border-radius: 50%; margin-right: 5px;'></div>
 
52
  </div>
53
  """, unsafe_allow_html=True)
54
 
55
+ # Chat history
56
  if 'history' not in st.session_state:
57
  st.session_state['history'] = []
58
 
59
+ # Input box for user question
60
  user_question = st.text_input("Escreva sua questão aqui:", "")
61
 
62
  if user_question:
63
+ # Add person emoji when typing question
64
  st.session_state['history'].append(('👤', user_question))
65
  st.markdown(f"**👤 {user_question}**")
66
 
67
+ # Generate the response
68
  bot_response = response(user_question)
69
 
70
+ # Add robot emoji when generating response and align to the right
71
  st.session_state['history'].append(('🤖', bot_response))
72
  st.markdown(f"<div style='text-align: right'>**🤖 {bot_response}**</div>", unsafe_allow_html=True)
73
 
74
+ # Clear history button
75
  if st.button("Limpar"):
76
  st.session_state['history'] = []
77
 
78
+ # Display chat history
79
  for sender, message in st.session_state['history']:
80
  if sender == '👤':
81
  st.markdown(f"**👤 {message}**")