yunuseduran commited on
Commit
9ff0896
·
verified ·
1 Parent(s): 96b70b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -23
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 process_pdfs(pdf_files, user_questions):
90
- combined_report_text = ""
 
 
91
 
92
- for pdf_file in pdf_files:
93
- file_name = pdf_file.orig_name
94
- saved_file_path = f"/tmp/{file_name}"
95
- shutil.copyfile(pdf_file.name, saved_file_path)
96
 
97
- subprocess.run(["apt-get", "update"])
98
- subprocess.run(["apt-get", "install", "-y", "poppler-utils"])
99
- subprocess.run(["pdftotext", saved_file_path, "/tmp/text_file.txt"])
100
 
101
- text_file = upload_file("/tmp/text_file.txt")
102
- chat_session = build_model(text_file)
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(combined_report_text)
111
  add_html_to_word(html_text, doc)
112
 
113
- doc_name = "Report_combined.docx"
 
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=process_pdfs,
129
  inputs=[
130
- gr.Files(label="Upload PDF(s)", type="filepath"),
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 FILES @YED",
138
- description="Upload multiple PDFs to ask questions and get the answers."
139
  )
140
 
141
  setup_api_key()
142
- iface.launch(share=True)
 
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()