InvictusRudra/question_answering
0
1# -*- coding: utf-8 -*-2"""Hackathon_Illuminati.ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7 https://colab.research.google.com/drive/1B-SaMQ85UdV9DnqZ6_OWqg8oOAlgcZ5V8 9# LangChain QA Panel App10 11This notebook shows how to make this app:12"""13 14# !pip install transformers15# !pip install easyocr16# !pip install pdf2image17# !apt-get install poppler-utils18# 19# ! pip install PyPDF220 21import panel as pn22from transformers import pipeline23from pdf2image import convert_from_path24import easyocr25 26pn.extension('texteditor', template="bootstrap", sizing_mode='stretch_width')27pn.state.template.param.update(28 main_max_width="690px",29 header_background="#F08080",30)31 32file_input = pn.widgets.FileInput(width=300)33 34prompt = pn.widgets.TextEditor(35 value="", placeholder="Enter your questions here...", height=160, toolbar=False36)37run_button = pn.widgets.Button(name="Run")38 39widgets = pn.Row(40 pn.Column(prompt, run_button, margin=5), width = 63041)42 43def 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 pdf50 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_text58 model = pipeline("question-answering", model='deepset/roberta-base-squad2')59 context = text60 result = model(question=query, context=context)61 print(f"Answer: {result['answer']}")62 return result63 64convos = [] # list of all panel objects65 66def qa_result(_):67 # saving pdf as a temp file68 if file_input.value is not None:69 file_input.save("/content/temp.pdf")70 71 prompt_text = prompt.value72 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=60079 ),80 pn.Row(81 pn.panel("A: ", width=10),82 pn.Column(83 result["answer"],84 )85 )86 ])87 #return convos88 return pn.Column(*convos, margin=15, width=575, min_height=400)89 90qa_interactive = pn.panel(91 pn.bind(qa_result, run_button),92 loading_indicator=True,93)94 95output = pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)96 97# layout98pn.Column(99 pn.pane.Markdown("""100 Question Answering with your PDF file!101 102 1) Upload a PDF. \n103 2) Type a question and click "Run".104 105 """),106 pn.Row(file_input),107 output,108 widgets109 110).servable()