DHEIVER commited on
Commit
ccb026f
·
verified ·
1 Parent(s): c97d0cf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import PyPDF2
4
+
5
+ # Carregar o modelo de linguagem gratuito da Hugging Face
6
+ nlp = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
7
+
8
+ def extract_text_from_pdf(pdf_file):
9
+ with open(pdf_file.name, "rb") as file:
10
+ reader = PyPDF2.PdfFileReader(file)
11
+ text = ""
12
+ for page_num in range(reader.numPages):
13
+ page = reader.getPage(page_num)
14
+ text += page.extract_text()
15
+ return text
16
+
17
+ def answer_question(pdf_file, question):
18
+ # Extrair texto do PDF
19
+ context = extract_text_from_pdf(pdf_file)
20
+
21
+ # Usar o modelo para responder a pergunta
22
+ result = nlp(question=question, context=context)
23
+ return result['answer']
24
+
25
+ # Interface Gradio
26
+ iface = gr.Interface(
27
+ fn=answer_question,
28
+ inputs=[
29
+ gr.inputs.File(label="Carregar PDF"),
30
+ gr.inputs.Textbox(label="Pergunta")
31
+ ],
32
+ outputs=gr.outputs.Textbox(label="Resposta"),
33
+ title="QA sobre PDF",
34
+ description="Carregue um PDF e faça perguntas sobre o conteúdo."
35
+ )
36
+
37
+ # Iniciar a interface
38
+ iface.launch()