Spaces:
Running
Running
Commit
·
025996e
1
Parent(s):
f66d2ce
Update app.py
Browse files
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 |
-
|
10 |
-
|
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
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
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
|
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 |
+
|