import gradio as gr import fitz def runfile(files): if not isinstance(files, list):…
- User
import gradio as gr import fitz
def run_file(files):
if not isinstance(files, list): files = [files] for file in files: print(f"{file.name}") file_suffix = file.name.split('.')[-1] file_dir = None if str(file_suffix).lower() == "pdf": print(f"pdf : {file.name}") doc = fitz.open(file.name) print(doc.page_count)if name == "main": with gr.Blocks(mode="demo") as demo: btn = gr.UploadButton("📁", file_types=[".pdf"], file_count="multiple") btn.upload(run_file, [btn], [], show_progress = True) demo.queue().launch(debug=True). Modify this code to save the pdf files to a directory
- Assistant
To modify the code to save the PDF files to a directory, you need to perform a few steps:
- Import the necessary
ospackage to handle files and directories. - Specify a directory where you want to save the PDF files.
- Import the necessary
- User
Running on local URL: http://127.0.0.1:7860
To create a public link, set
share=Trueinlaunch(). C:\Users\priya\AppData\Local\Temp\gradio\56aed7551b506b40d8903185e3571e356f6d4577\document.pdf pdf : C:\Users\priya\AppData\Local\Temp\gradio\56aed7551b506b40d8903185e3571e356f6d4577\document.pdf Traceback (most recent call last): File "C:\Users\priya\anaconda3\lib\site-packages\gradio\queueing.py", line 501, in call_prediction output = await route_utils.call_process_api( File "C:\Users\priya\anaconda3\lib\site-packages\gradio\route_utils.py", line 253, in call_process_api output = await app.get_blocks().process_api( File "C:\Users\priya\anaconda3\lib\site-packages\gradio\blocks.py", line 1695, in process_api result = await self.call_function( File "C:\Users\priya\anaconda3\lib\site-packages\gradio\blocks.py", line 1235, in call_function prediction = await anyio.to_thread.run_sync( File "C:\Users\priya\anaconda3\lib\site-packages\anyio\to_thread.py", line 28, in run_sync return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable, File "C:\Users\priya\anaconda3\lib\site-packages\anyio_backends_asyncio.py", line 818, in run_sync_in_worker_thread return await future File "C:\Users\priya\anaconda3\lib\site-packages\anyio_backends_asyncio.py", line 754, in run result = context.run(func, *args) File "C:\Users\priya\anaconda3\lib\site-packages\gradio\utils.py", line 692, in wrapper response = f(*args, **kwargs) File "D:\Projects\gradio_experiments\app.py", line 215, in run_file f.write(file.read()) # Write the content of the file to the save path AttributeError: 'NamedString' object has no attribute 'read' - Assistant
It seems like the
fileobject being passed to therun_filefunction is aNamedStringand not a file-like object that provides a method, which is necessary to read the file's binary data. - User
You can take inspiration from this code - # The GRADIO Interface with gr.Blocks() as demo: with gr.Row(): with gr.Row(): # Chatbot interface chatbot = gr.Chatbot(label="DeciLM-7B-instruct bot", value=[], elem_id='chatbot') with gr.Row(): # Uploaded PDFs window file_output = gr.File(label="Your PDFs")
with gr.Column(): # PDF upload button btn = gr.UploadButton("📁 Upload a PDF(s)", file_types=[".pdf"], file_count="multiple") with gr.Column(): with gr.Column(): # Ask question input field txt = gr.Text(show_label=False, placeholder="Enter question") with gr.Column(): # button to submit question to the bot submit_btn = gr.Button('Ask') # Event handler for uploading a PDF btn.upload(fn=upload_file, inputs=[btn], outputs=[file_output]) # Gradio EVENTS # Event handler for submitting text question and generating response submit_btn.click( fn= add_text, inputs=[chatbot, txt], outputs=[chatbot], queue=False ).success( fn=generate_bot_response, inputs=[chatbot, txt, btn], outputs=[chatbot, txt] ).success( fn=upload_file, inputs=[btn], outputs=[file_output] ) - Assistant