Rahatara commited on
Commit
e0ac1ce
1 Parent(s): c11b454

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -65,18 +65,22 @@ def get_response(history, query, file):
65
  if not file:
66
  raise gr.Error(message="Upload a PDF")
67
  chain = app(file)
68
- result = chain(
69
- {"question": query, "chat_history": app.chat_history}, return_only_outputs=True
70
- )
71
- app.chat_history.append((query, result["answer"]))
72
- source_docs = result["source_documents"]
73
- source_texts = []
74
- for doc in source_docs:
75
- source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
76
- source_texts_str = "\n\n".join(source_texts)
77
- for char in result["answer"]:
78
- history[-1][-1] += char
79
- yield history, "", source_texts_str
 
 
 
 
80
 
81
  def render_file(file) -> Image.Image:
82
  doc = fitz.open(file.name)
@@ -107,6 +111,7 @@ with gr.Blocks() as demo:
107
  with gr.Tab("Step 2: Process File"):
108
  process_btn = gr.Button("Process PDF")
109
  show_img_processed = gr.Image(label="Processed PDF")
 
110
 
111
  with gr.Tab("Step 3: Build Vector Database"):
112
  build_vector_btn = gr.Button("Build Vector Database")
@@ -130,9 +135,9 @@ with gr.Blocks() as demo:
130
  )
131
 
132
  process_btn.click(
133
- fn=app.process_file,
134
  inputs=[btn],
135
- outputs=[show_img_processed],
136
  )
137
 
138
  build_vector_btn.click(
 
65
  if not file:
66
  raise gr.Error(message="Upload a PDF")
67
  chain = app(file)
68
+ try:
69
+ result = chain.invoke(
70
+ {"question": query, "chat_history": app.chat_history}
71
+ )
72
+ app.chat_history.append((query, result["answer"]))
73
+ source_docs = result["source_documents"]
74
+ source_texts = []
75
+ for doc in source_docs:
76
+ source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
77
+ source_texts_str = "\n\n".join(source_texts)
78
+ for char in result["answer"]:
79
+ history[-1][-1] += char
80
+ yield history, "", source_texts_str
81
+ except Exception:
82
+ app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
83
+ yield history, "", "I have no information about it. Feed me knowledge, please!"
84
 
85
  def render_file(file) -> Image.Image:
86
  doc = fitz.open(file.name)
 
111
  with gr.Tab("Step 2: Process File"):
112
  process_btn = gr.Button("Process PDF")
113
  show_img_processed = gr.Image(label="Processed PDF")
114
+ process_status = gr.Textbox(label="Processing Status", interactive=False)
115
 
116
  with gr.Tab("Step 3: Build Vector Database"):
117
  build_vector_btn = gr.Button("Build Vector Database")
 
135
  )
136
 
137
  process_btn.click(
138
+ fn=lambda file: (app.process_file(file), "Processing complete!"),
139
  inputs=[btn],
140
+ outputs=[show_img_processed, process_status],
141
  )
142
 
143
  build_vector_btn.click(