rss9051 commited on
Commit
35d86be
·
verified ·
1 Parent(s): c07f743

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -3,29 +3,45 @@ from huggingface_hub import InferenceClient
3
  import json
4
 
5
  # Inicialize o cliente com o modelo do Hugging Face
6
- #client = InferenceClient(model="ulisesbravo/autotrain-nsuej-5ctie")
7
- #client = InferenceClient(model="ulisesbravo/autotrain-nzog3-ca819");
8
- #client = InferenceClient(model="ulisesbravo/autotrain-po0st-um4bf");
9
- #client = InferenceClient(model="ulisesbravo/autotrain-k9ag8-b7nm0");
10
- #client = InferenceClient(model="ulisesbravo/autotrain-pt-l1sfd-nom8j");
11
- client = InferenceClient(model="rss9051/autotrein-BERT-iiLEX-dgs-0003");
12
-
 
 
 
 
 
13
  def classify_text(text):
14
- # Realize a inferência chamando o método post
15
- response_bytes = client.post(json={"inputs": text}) # Enviar o texto
16
-
17
- # Decodificar a resposta de bytes para string e depois para JSON
18
- response_str = response_bytes.decode('utf-8') # Decodificar de bytes para string
19
- response = json.loads(response_str) # Converter string JSON para um objeto Python
20
-
21
- # Inspecionar a resposta para depuração
22
- print(response)
23
-
24
- # Verificar se a resposta é uma lista válida
25
- if isinstance(response, list) and len(response) > 0:
26
- # Ordenar as classificações pelo score e pegar a de maior valor
27
- sorted_response = sorted(response[0], key=lambda x: x['score'], reverse=True)
28
- predicted_class = sorted_response[0]['label'] # Pegar a classe com maior score
 
 
 
 
 
 
 
 
 
 
 
29
  else:
30
  predicted_class = "Classificação não encontrada"
31
 
@@ -41,4 +57,4 @@ demo = gr.Interface(
41
  )
42
 
43
  if __name__ == "__main__":
44
- demo.launch()
 
3
  import json
4
 
5
  # Inicialize o cliente com o modelo do Hugging Face
6
+ client = InferenceClient(model="rss9051/autotrein-BERT-iiLEX-dgs-0004")
7
+
8
+ # Função para dividir o texto em chunks menores
9
+ def split_text_into_chunks(text, max_tokens=512):
10
+ words = text.split()
11
+ chunks = []
12
+ for i in range(0, len(words), max_tokens):
13
+ chunk = " ".join(words[i:i + max_tokens])
14
+ chunks.append(chunk)
15
+ return chunks
16
+
17
+ # Função para classificar texto, lidando com textos longos
18
  def classify_text(text):
19
+ chunks = split_text_into_chunks(text, max_tokens=512) # Divida o texto em chunks menores
20
+ all_responses = [] # Lista para armazenar respostas de cada chunk
21
+
22
+ for chunk in chunks:
23
+ response_bytes = client.post(json={"inputs": chunk}) # Enviar o chunk
24
+ response_str = response_bytes.decode('utf-8') # Decodificar de bytes para string
25
+ response = json.loads(response_str) # Converter string JSON para objeto Python
26
+
27
+ if isinstance(response, list) and len(response) > 0:
28
+ sorted_response = sorted(response[0], key=lambda x: x['score'], reverse=True)
29
+ all_responses.append(sorted_response[0]) # Adicionar a melhor classificação do chunk
30
+
31
+ # Combinar resultados de todos os chunks
32
+ if all_responses:
33
+ # Contar as classes mais frequentes
34
+ class_scores = {}
35
+ for res in all_responses:
36
+ label = res['label']
37
+ score = res['score']
38
+ if label in class_scores:
39
+ class_scores[label] += score
40
+ else:
41
+ class_scores[label] = score
42
+
43
+ # Obter a classe com maior score combinado
44
+ predicted_class = max(class_scores, key=class_scores.get)
45
  else:
46
  predicted_class = "Classificação não encontrada"
47
 
 
57
  )
58
 
59
  if __name__ == "__main__":
60
+ demo.launch()