rudradeep22 commited on
Commit
9a0d2e1
1 Parent(s): dde7c4f

panel app for hackathon

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -0
  2. app.py +110 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "InvictusRudra-question-answering.hf.space"]
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Hackathon_Illuminati.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1B-SaMQ85UdV9DnqZ6_OWqg8oOAlgcZ5V
8
+
9
+ # LangChain QA Panel App
10
+
11
+ This notebook shows how to make this app:
12
+ """
13
+
14
+ # !pip install transformers
15
+ # !pip install easyocr
16
+ # !pip install pdf2image
17
+ # !apt-get install poppler-utils
18
+ #
19
+ # ! pip install PyPDF2
20
+
21
+ import panel as pn
22
+ from transformers import pipeline
23
+ from pdf2image import convert_from_path
24
+ import easyocr
25
+
26
+ pn.extension('texteditor', template="bootstrap", sizing_mode='stretch_width')
27
+ pn.state.template.param.update(
28
+ main_max_width="690px",
29
+ header_background="#F08080",
30
+ )
31
+
32
+ file_input = pn.widgets.FileInput(width=300)
33
+
34
+ prompt = pn.widgets.TextEditor(
35
+ value="", placeholder="Enter your questions here...", height=160, toolbar=False
36
+ )
37
+ run_button = pn.widgets.Button(name="Run")
38
+
39
+ widgets = pn.Row(
40
+ pn.Column(prompt, run_button, margin=5), width = 630
41
+ )
42
+
43
+ def qa(file, query):
44
+
45
+ images = convert_from_path(file)
46
+ reader = easyocr.Reader(['en'])
47
+ result = []
48
+ for i in range(len(images)):
49
+ # Save pages as images in the pdf
50
+ images[i].save('page'+ str(i) +'.jpg', 'JPEG')
51
+ x=str(i)
52
+ t='page'+x+'.jpg'
53
+ result.append(reader.readtext(t, detail = 0))
54
+ text = ""
55
+ for page in result:
56
+ page_text = " ".join(page)
57
+ text += page_text
58
+ model = pipeline("question-answering", model='deepset/roberta-base-squad2')
59
+ context = text
60
+ result = model(question=query, context=context)
61
+ print(f"Answer: {result['answer']}")
62
+ return result
63
+
64
+ convos = [] # list of all panel objects
65
+
66
+ def qa_result(_):
67
+ # saving pdf as a temp file
68
+ if file_input.value is not None:
69
+ file_input.save("/content/temp.pdf")
70
+
71
+ prompt_text = prompt.value
72
+ if prompt_text:
73
+ result = qa(file="/content/temp.pdf", query=prompt_text)
74
+ convos.extend([
75
+ pn.Row(
76
+ pn.panel("Q: ", width=10),
77
+ prompt_text,
78
+ width=600
79
+ ),
80
+ pn.Row(
81
+ pn.panel("A: ", width=10),
82
+ pn.Column(
83
+ result["answer"],
84
+ )
85
+ )
86
+ ])
87
+ #return convos
88
+ return pn.Column(*convos, margin=15, width=575, min_height=400)
89
+
90
+ qa_interactive = pn.panel(
91
+ pn.bind(qa_result, run_button),
92
+ loading_indicator=True,
93
+ )
94
+
95
+ output = pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)
96
+
97
+ # layout
98
+ pn.Column(
99
+ pn.pane.Markdown("""
100
+ Question Answering with your PDF file!
101
+
102
+ 1) Upload a PDF. \n
103
+ 2) Type a question and click "Run".
104
+
105
+ """),
106
+ pn.Row(file_input),
107
+ output,
108
+ widgets
109
+
110
+ ).servable()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ panel
2
+ transformers
3
+ pdf2image
4
+ easyocr