itacaiunas commited on
Commit
025996e
1 Parent(s): f66d2ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -6,32 +6,34 @@ import PyPDF2
6
  # Configure sua chave de API do OpenAI
7
  openai.api_key = "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"
8
 
9
- def buscar_cdu(palavras_chave):
10
- # Carregar o arquivo PDF da URL
11
- url = "https://huggingface.co/spaces/itacaiunas/gerador-cdu/blob/main/TABELA%20CDU%20DE%20C%C3%93DIGO%20DE%20ASSUNTO.pdf"
12
  response = requests.get(url)
13
- with open("dados-cdu.pdf", "wb") as file:
14
- file.write(response.content)
15
-
16
- # Extrair informações do arquivo PDF
17
- classificacoes = extrair_classificacoes("dados-cdu.pdf")
18
-
19
- # Encontrar a classificação relacionada às palavras-chave
20
- for palavra_chave in palavras_chave:
21
- for classificacao in classificacoes:
22
- if palavra_chave.lower() in classificacao.lower():
23
- return classificacao
24
-
25
- return "Nenhuma classificação encontrada para as palavras-chave fornecidas."
26
-
27
- def extrair_classificacoes(file_path):
28
- classificacoes = []
29
- with open(file_path, "rb") as file:
30
- reader = PyPDF2.PdfFileReader(file)
31
- for page in range(reader.numPages):
32
- text = reader.getPage(page).extractText()
33
- classificacoes.extend(text.split("\n"))
34
- return classificacoes
 
 
 
35
 
36
  # Interface Gradio
37
  input_text = gr.inputs.Textbox(label="Inserir palavras-chave separadas por vírgulas")
@@ -40,7 +42,7 @@ output_text = gr.outputs.Textbox(label="Resultado")
40
 
41
  def generate_cdu(palavras_chave):
42
  if palavras_chave:
43
- return buscar_cdu(palavras_chave.split(","))
44
  else:
45
  return ""
46
 
@@ -48,3 +50,4 @@ title = "Gerador de CDU"
48
  description = "Insira palavras-chave separadas por vírgulas e clique em 'Gerar CDU' para obter a classificação relacionada."
49
 
50
  gr.Interface(fn=generate_cdu, inputs=input_text, outputs=output_text, title=title, description=description, model_name="gpt-3.5-turbo", button_text=button_label).launch()
 
 
6
  # Configure sua chave de API do OpenAI
7
  openai.api_key = "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"
8
 
9
+ # Função para obter o texto do arquivo PDF a partir do link
10
+ def extrair_texto_pdf(url):
 
11
  response = requests.get(url)
12
+ with open("dados-cdu.pdf", "wb") as f:
13
+ f.write(response.content)
14
+
15
+ pdf_file = open("dados-cdu.pdf", "rb")
16
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
17
+ text = ""
18
+ for page in pdf_reader.pages:
19
+ text += page.extract_text()
20
+ pdf_file.close()
21
+
22
+ return text
23
+
24
+ def gerar_cdu(palavras_chave):
25
+ texto_base = extrair_texto_pdf("https://huggingface.co/spaces/itacaiunas/gerador-cdu/blob/main/dados-cdu.pdf")
26
+ prompt = f"analise as palavras-chave: {palavras_chave} e retorne com o número de Classificação Decimal Universal (CDU) mais relacionado com as palavras-chave. Dados da base: {texto_base}"
27
+ response = openai.Completion.create(
28
+ model="text-davinci-003",
29
+ prompt=prompt,
30
+ max_tokens=100,
31
+ n=1,
32
+ stop=None,
33
+ temperature=0.7
34
+ )
35
+ cdu = response.choices[0].text.strip()
36
+ return cdu
37
 
38
  # Interface Gradio
39
  input_text = gr.inputs.Textbox(label="Inserir palavras-chave separadas por vírgulas")
 
42
 
43
  def generate_cdu(palavras_chave):
44
  if palavras_chave:
45
+ return gerar_cdu(palavras_chave)
46
  else:
47
  return ""
48
 
 
50
  description = "Insira palavras-chave separadas por vírgulas e clique em 'Gerar CDU' para obter a classificação relacionada."
51
 
52
  gr.Interface(fn=generate_cdu, inputs=input_text, outputs=output_text, title=title, description=description, model_name="gpt-3.5-turbo", button_text=button_label).launch()
53
+