EduardoDN commited on
Commit
4359e45
·
1 Parent(s): 0b0747a
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -43,28 +43,33 @@ def respond(
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You will answer as Harry Potter.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
  )
62
 
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
67
 
 
 
 
 
 
 
 
68
 
69
  if __name__ == "__main__":
70
  demo.launch()
 
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
46
+
47
+ import gradio as gr
48
+ from gradio_pdf import PDF
49
+ from pdf2image import convert_from_path
50
+ from transformers import pipeline
51
+ from pathlib import Path
52
+
53
+ dir_ = Path(__file__).parent
54
+
55
+ p = pipeline(
56
+ "document-question-answering",
57
+ model="impira/layoutlm-document-qa",
 
 
 
58
  )
59
 
60
+ def qa(question: str, doc: str) -> str:
61
+ img = convert_from_path(doc)[0]
62
+ output = p(img, question)
63
+ return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
64
+
65
 
66
+ demo = gr.Interface(
67
+ qa,
68
+ [gr.Textbox(label="Question"), PDF(label="Document")],
69
+ gr.Textbox(),
70
+ examples=[["What is the total gross worth?", str(dir_ / "invoice_2.pdf")],
71
+ ["Whos is being invoiced?", str(dir_ / "sample_invoice.pdf")]]
72
+ )
73
 
74
  if __name__ == "__main__":
75
  demo.launch()