Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,9 @@ import fitz # PyMuPDF
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("Locutusque/gpt2-xl-conversational")
|
| 7 |
model = AutoModelForCausalLM.from_pretrained("Locutusque/gpt2-xl-conversational")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Função para ler o PDF
|
| 10 |
def read_pdf(file_path):
|
| 11 |
doc = fitz.open(file_path)
|
|
@@ -15,13 +18,18 @@ def read_pdf(file_path):
|
|
| 15 |
text += page.get_text()
|
| 16 |
return text
|
| 17 |
|
| 18 |
-
# Função para
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 26 |
outputs = model.generate(
|
| 27 |
inputs.input_ids,
|
|
@@ -34,12 +42,25 @@ def generate_response_from_pdf(pdf_file, max_length=200, temperature=0.7, top_k=
|
|
| 34 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
return response
|
| 36 |
|
| 37 |
-
# Interface do Gradio
|
| 38 |
-
|
| 39 |
-
fn=
|
| 40 |
inputs=gr.File(label="Carregue um PDF"),
|
| 41 |
outputs="text",
|
| 42 |
-
title="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("Locutusque/gpt2-xl-conversational")
|
| 7 |
model = AutoModelForCausalLM.from_pretrained("Locutusque/gpt2-xl-conversational")
|
| 8 |
|
| 9 |
+
# Variável global para armazenar o conteúdo do PDF
|
| 10 |
+
pdf_content = ""
|
| 11 |
+
|
| 12 |
# Função para ler o PDF
|
| 13 |
def read_pdf(file_path):
|
| 14 |
doc = fitz.open(file_path)
|
|
|
|
| 18 |
text += page.get_text()
|
| 19 |
return text
|
| 20 |
|
| 21 |
+
# Função para carregar o PDF e armazenar o conteúdo
|
| 22 |
+
def load_pdf(pdf_file):
|
| 23 |
+
global pdf_content
|
| 24 |
+
pdf_content = read_pdf(pdf_file.name)
|
| 25 |
+
return "PDF carregado com sucesso!"
|
| 26 |
+
|
| 27 |
+
# Função para responder perguntas com base no conteúdo do PDF
|
| 28 |
+
def answer_question(question, max_length=200, temperature=0.7, top_k=50, top_p=0.95):
|
| 29 |
+
global pdf_content
|
| 30 |
+
if not pdf_content:
|
| 31 |
+
return "Por favor, carregue um PDF primeiro."
|
| 32 |
+
prompt = f"Conteúdo do PDF: {pdf_content}\nPergunta: {question}\nResposta em português:"
|
| 33 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 34 |
outputs = model.generate(
|
| 35 |
inputs.input_ids,
|
|
|
|
| 42 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 43 |
return response
|
| 44 |
|
| 45 |
+
# Interface do Gradio para carregar PDF e fazer perguntas
|
| 46 |
+
pdf_loader = gr.Interface(
|
| 47 |
+
fn=load_pdf,
|
| 48 |
inputs=gr.File(label="Carregue um PDF"),
|
| 49 |
outputs="text",
|
| 50 |
+
title="Carregar PDF"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
question_answerer = gr.Interface(
|
| 54 |
+
fn=answer_question,
|
| 55 |
+
inputs=gr.Textbox(lines=2, label="Pergunta"),
|
| 56 |
+
outputs="text",
|
| 57 |
+
title="Perguntas sobre o PDF"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Combinar as interfaces em uma aplicação
|
| 61 |
+
iface = gr.TabbedInterface(
|
| 62 |
+
[pdf_loader, question_answerer],
|
| 63 |
+
["Carregar PDF", "Fazer Perguntas"]
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|