witcher23 commited on
Commit
48e425e
·
verified ·
1 Parent(s): 6e61283

add pdf ext code

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
@@ -40,6 +42,20 @@ def respond(
40
  yield response
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
@@ -59,6 +75,18 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import PyPDF2
4
+ import io
5
 
6
  """
7
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
 
42
  yield response
43
 
44
 
45
+ def extract_text_from_pdf(pdf_file):
46
+ if pdf_file is None:
47
+ return "No file uploaded."
48
+
49
+ try:
50
+ pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file))
51
+ text = ""
52
+ for page in pdf_reader.pages:
53
+ text += page.extract_text() + "\n\n"
54
+ return text.strip()
55
+ except Exception as e:
56
+ return f"An error occurred: {str(e)}"
57
+
58
+
59
  """
60
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
61
  """
 
75
  ],
76
  )
77
 
78
+ pdf_interface = gr.Interface(
79
+ fn=extract_text_from_pdf,
80
+ inputs=gr.File(label="Upload PDF", type="binary"),
81
+ outputs="text",
82
+ title="PDF Text Extractor",
83
+ description="Upload a PDF file to extract its text content."
84
+ )
85
+
86
+ demo = gr.TabbedInterface(
87
+ [demo, pdf_interface],
88
+ ["Chat", "PDF Extractor"]
89
+ )
90
 
91
  if __name__ == "__main__":
92
  demo.launch()