jpangas commited on
Commit
688c931
·
verified ·
1 Parent(s): eac086e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -15
app.py CHANGED
@@ -16,12 +16,29 @@ import xmltodict
16
  qa_graph = None
17
  current_file = None
18
 
19
-
20
  class State(TypedDict):
21
  question: str
22
  context: List[Document]
23
  answer: str
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def initiate_graph(file):
27
  global qa_graph, current_file
@@ -70,7 +87,6 @@ def initiate_graph(file):
70
  name = file.name.split("/")[-1]
71
  return f"The paper {name} has been loaded and is ready for questions!"
72
 
73
-
74
  def answer_question(question, history):
75
  global qa_graph, current_file
76
 
@@ -80,7 +96,6 @@ def answer_question(question, history):
80
  response = qa_graph.invoke({"question": question})
81
  return response["answer"]
82
 
83
-
84
  def slow_echo(message, history):
85
  answer = answer_question(message, history)
86
  if answer == "Please upload a PDF file first!":
@@ -91,20 +106,22 @@ def slow_echo(message, history):
91
  time.sleep(0.01)
92
  yield answer[: i + 1]
93
 
 
 
 
 
 
 
94
 
95
- with gr.Blocks() as demo:
96
- file_input = gr.File(
97
- label="Upload a research paper as a pdf file wait for it to be loaded",
98
- file_types=[".pdf"],
99
- )
100
-
101
- textbox = gr.Textbox(
102
- label="Status of Upload", value="No Paper Uploaded", interactive=False
103
- )
104
 
105
- chat_interface = gr.ChatInterface(slow_echo, type="messages")
106
 
107
- file_input.upload(fn=initiate_graph, inputs=file_input, outputs=textbox)
108
 
 
109
 
110
- demo.queue()
 
 
16
  qa_graph = None
17
  current_file = None
18
 
 
19
  class State(TypedDict):
20
  question: str
21
  context: List[Document]
22
  answer: str
23
 
24
+ def get_extra_docs(file_name):
25
+ # TODO: Add the code to extract the title, authors and abstract from the PDF file
26
+ client = GrobidClient(config_path="./config.json")
27
+ information = client.process_pdf(
28
+ "processHeaderDocument",
29
+ file_name,
30
+ generateIDs=False,
31
+ consolidate_header=False,
32
+ consolidate_citations=False,
33
+ include_raw_citations=False,
34
+ include_raw_affiliations=False,
35
+ tei_coordinates=False,
36
+ segment_sentences=False,
37
+ )
38
+ dict_information = xmltodict.parse(information[2])
39
+ title = dict_information["tei"]["teiHeader"]["fileDesc"]["titleStmt"]["title"]
40
+ abstract = dict_information["tei"]["teiHeader"]["profileDesc"]["abstract"]["p"]
41
+ return title
42
 
43
  def initiate_graph(file):
44
  global qa_graph, current_file
 
87
  name = file.name.split("/")[-1]
88
  return f"The paper {name} has been loaded and is ready for questions!"
89
 
 
90
  def answer_question(question, history):
91
  global qa_graph, current_file
92
 
 
96
  response = qa_graph.invoke({"question": question})
97
  return response["answer"]
98
 
 
99
  def slow_echo(message, history):
100
  answer = answer_question(message, history)
101
  if answer == "Please upload a PDF file first!":
 
106
  time.sleep(0.01)
107
  yield answer[: i + 1]
108
 
109
+ def main():
110
+ with gr.Blocks() as demo:
111
+ file_input = gr.File(
112
+ label="Upload a research paper as a pdf file and wait for it to be loaded",
113
+ file_types=[".pdf"],
114
+ )
115
 
116
+ textbox = gr.Textbox(
117
+ label="Status of Upload", value="No Paper Uploaded", interactive=False
118
+ )
 
 
 
 
 
 
119
 
120
+ chat_interface = gr.ChatInterface(slow_echo, type="messages")
121
 
122
+ file_input.upload(fn=initiate_graph, inputs=file_input, outputs=textbox)
123
 
124
+ demo.queue().launch()
125
 
126
+ if __name__ == "__main__":
127
+ main()