Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -86,31 +86,27 @@ def add_html_to_word(html_text, doc):
|
|
86 |
elif element.name:
|
87 |
doc.add_paragraph(element.get_text()) # For any other tags
|
88 |
|
89 |
-
def
|
90 |
-
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
shutil.copyfile(pdf_file.name, saved_file_path)
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
subprocess.run(["pdftotext", saved_file_path, "/tmp/text_file.txt"])
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
questions = user_questions.strip().split('\n')
|
105 |
-
report_text = generate_report(chat_session, questions)
|
106 |
-
|
107 |
-
combined_report_text += report_text + "\n\n"
|
108 |
|
109 |
doc = Document()
|
110 |
-
html_text = convert_markdown_to_html(
|
111 |
add_html_to_word(html_text, doc)
|
112 |
|
113 |
-
doc_name = "
|
|
|
114 |
doc.save(f"/tmp/{doc_name}")
|
115 |
|
116 |
return html_text, f"/tmp/{doc_name}"
|
@@ -125,18 +121,18 @@ questions = [
|
|
125 |
questions_str = "\n".join(questions)
|
126 |
|
127 |
iface = gr.Interface(
|
128 |
-
fn=
|
129 |
inputs=[
|
130 |
-
gr.
|
131 |
gr.TextArea(label="Enter Questions", placeholder="Type your questions here, one per line.", value=questions_str)
|
132 |
],
|
133 |
outputs=[
|
134 |
gr.HTML(label="HTML Formatted Report"),
|
135 |
gr.File(label="DOCX File Output", type="binary")
|
136 |
],
|
137 |
-
title="REPORT GENERATOR: ASK YOUR QUESTIONS TO PDF
|
138 |
-
description="Upload
|
139 |
)
|
140 |
|
141 |
setup_api_key()
|
142 |
-
iface.launch(
|
|
|
86 |
elif element.name:
|
87 |
doc.add_paragraph(element.get_text()) # For any other tags
|
88 |
|
89 |
+
def process_pdf(pdf_file, user_questions):
|
90 |
+
file_name = pdf_file.split('/')[-1]
|
91 |
+
saved_file_path = f"/tmp/{file_name}"
|
92 |
+
shutil.copyfile(pdf_file, saved_file_path)
|
93 |
|
94 |
+
subprocess.run(["apt-get", "update"])
|
95 |
+
subprocess.run(["apt-get", "install", "-y", "poppler-utils"])
|
96 |
+
subprocess.run(["pdftotext", saved_file_path, "/tmp/text_file.txt"])
|
|
|
97 |
|
98 |
+
text_file = upload_file("/tmp/text_file.txt")
|
99 |
+
chat_session = build_model(text_file)
|
|
|
100 |
|
101 |
+
questions = user_questions.strip().split('\n')
|
102 |
+
report_text = generate_report(chat_session, questions)
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
doc = Document()
|
105 |
+
html_text = convert_markdown_to_html(report_text)
|
106 |
add_html_to_word(html_text, doc)
|
107 |
|
108 |
+
doc_name = file_name.replace(".pdf", ".docx")
|
109 |
+
doc_name = "Report_" + doc_name
|
110 |
doc.save(f"/tmp/{doc_name}")
|
111 |
|
112 |
return html_text, f"/tmp/{doc_name}"
|
|
|
121 |
questions_str = "\n".join(questions)
|
122 |
|
123 |
iface = gr.Interface(
|
124 |
+
fn=process_pdf,
|
125 |
inputs=[
|
126 |
+
gr.File(label="Upload PDF", type="filepath"),
|
127 |
gr.TextArea(label="Enter Questions", placeholder="Type your questions here, one per line.", value=questions_str)
|
128 |
],
|
129 |
outputs=[
|
130 |
gr.HTML(label="HTML Formatted Report"),
|
131 |
gr.File(label="DOCX File Output", type="binary")
|
132 |
],
|
133 |
+
title="REPORT GENERATOR: ASK YOUR QUESTIONS TO A PDF FILE BY @YED",
|
134 |
+
description="Upload a PDF to ask questions and get the answers."
|
135 |
)
|
136 |
|
137 |
setup_api_key()
|
138 |
+
iface.launch()
|