shayanSolSameed commited on
Commit
6901ae8
·
1 Parent(s): 41c5772

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +3 -9
  2. gradio_chatwithpdf.py +53 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Ask PDF
3
- emoji: 👀
4
- colorFrom: pink
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 4.5.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Ask_PDF
3
+ app_file: gradio_chatwithpdf.py
 
 
4
  sdk: gradio
5
+ sdk_version: 3.44.4
 
 
6
  ---
 
 
gradio_chatwithpdf.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from openai import OpenAI
3
+ import gradio
4
+ import shutil
5
+ import pdfplumber
6
+ import os
7
+
8
+
9
+ def read_PDF(pdf_path):
10
+ pdf_text = ""
11
+ with pdfplumber.open(pdf_path) as pdf:
12
+ for page in pdf.pages:
13
+ pdf_text += page.extract_text()
14
+ return pdf_text
15
+
16
+
17
+ def read_PDF_2(fileobj):
18
+ pdf_text = ""
19
+
20
+ path = "./" + os.path.basename(fileobj.name)
21
+ shutil.copyfile(fileobj.name, path)
22
+
23
+ with pdfplumber.open(path) as pdf:
24
+ for page in pdf.pages:
25
+ pdf_text += page.extract_text()
26
+
27
+ return pdf_text
28
+
29
+
30
+ def askPDF(upload_PDF, prompt):
31
+ client = OpenAI()
32
+
33
+ pdf_text = read_PDF_2(upload_PDF)
34
+ systemMessage = '"""' + pdf_text + '"""' + \
35
+ '\nAnswer all questions according to the text delimited in tripple quotes above'
36
+ completion = client.chat.completions.create(
37
+ model="gpt-3.5-turbo",
38
+ messages=[
39
+ {"role": "system", "content": systemMessage},
40
+ {"role": "user", "content": prompt}
41
+ ]
42
+ )
43
+
44
+ return (completion.choices[0].message).content
45
+
46
+
47
+ # gradio deployment
48
+ demo = gradio.Interface(fn=askPDF,
49
+ inputs=[gradio.File(file_types=['.pdf']), 'text'],
50
+ outputs="text",
51
+ )
52
+
53
+ demo.launch()