Update app.py
Browse files
app.py
CHANGED
@@ -76,6 +76,27 @@ def get_response(history, query):
|
|
76 |
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
77 |
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Function to render file
|
80 |
def render_file(file) -> Image.Image:
|
81 |
doc = fitz.open(file.name)
|
@@ -112,50 +133,50 @@ def set_api_key(api_key):
|
|
112 |
|
113 |
# Gradio interface
|
114 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
with gr.Tab("Inst RAG"):
|
116 |
with gr.Column():
|
117 |
-
|
118 |
-
api_key_btn = gr.Button("Set API Key")
|
119 |
-
api_key_status = gr.Textbox(value="API Key status", interactive=False)
|
120 |
-
|
121 |
-
api_key_btn.click(
|
122 |
-
fn=set_api_key,
|
123 |
-
inputs=[api_key_input],
|
124 |
-
outputs=[api_key_status]
|
125 |
-
)
|
126 |
-
|
127 |
-
with gr.Tab("Upload PDF"):
|
128 |
btn = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"])
|
129 |
show_img = gr.Image(label="Uploaded PDF")
|
130 |
-
|
131 |
btn.upload(
|
132 |
fn=purge_chat_and_render_first,
|
133 |
inputs=[btn],
|
134 |
outputs=[show_img],
|
135 |
)
|
136 |
|
137 |
-
with gr.
|
138 |
process_btn = gr.Button("Process PDF")
|
139 |
show_img_processed = gr.Image(label="Processed PDF")
|
140 |
process_status = gr.Textbox(label="Processing Status", interactive=False)
|
141 |
-
|
142 |
process_btn.click(
|
143 |
fn=lambda file: (app.process_file(file), "Processing complete!"),
|
144 |
inputs=[btn],
|
145 |
outputs=[show_img_processed, process_status],
|
146 |
)
|
147 |
|
148 |
-
with gr.
|
149 |
build_vector_btn = gr.Button("Build Vector Database")
|
150 |
status_text = gr.Textbox(label="Status", value="", interactive=False)
|
151 |
-
|
152 |
build_vector_btn.click(
|
153 |
fn=app.build_chain,
|
154 |
inputs=[btn],
|
155 |
outputs=[status_text],
|
156 |
)
|
157 |
|
158 |
-
with gr.
|
159 |
chatbot = gr.Chatbot(elem_id="chatbot")
|
160 |
txt = gr.Textbox(
|
161 |
show_label=False,
|
@@ -193,9 +214,6 @@ with gr.Blocks() as demo:
|
|
193 |
refresh_btn_current = gr.Button("Refresh Chat", scale=1)
|
194 |
source_texts_output_current = gr.Textbox(label="Source Texts", interactive=False)
|
195 |
|
196 |
-
def get_response_current(history, query):
|
197 |
-
return get_response(history, query, open("track_training.pdf", 'rb'))
|
198 |
-
|
199 |
submit_btn_current.click(
|
200 |
fn=add_text,
|
201 |
inputs=[chatbot_current, txt_current],
|
|
|
76 |
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
77 |
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
78 |
|
79 |
+
# Function to get response for the current RAG tab
|
80 |
+
def get_response_current(history, query):
|
81 |
+
if app.chain is None:
|
82 |
+
raise gr.Error("The chain has not been built yet. Please ensure the vector database is built before querying.")
|
83 |
+
|
84 |
+
try:
|
85 |
+
result = app.chain.invoke(
|
86 |
+
{"question": query, "chat_history": app.chat_history}
|
87 |
+
)
|
88 |
+
app.chat_history.append((query, result["answer"]))
|
89 |
+
source_docs = result["source_documents"]
|
90 |
+
source_texts = []
|
91 |
+
for doc in source_docs:
|
92 |
+
source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
|
93 |
+
source_texts_str = "\n\n".join(source_texts)
|
94 |
+
history[-1] = (history[-1][0], result["answer"])
|
95 |
+
return history, source_texts_str
|
96 |
+
except Exception as e:
|
97 |
+
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
98 |
+
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
99 |
+
|
100 |
# Function to render file
|
101 |
def render_file(file) -> Image.Image:
|
102 |
doc = fitz.open(file.name)
|
|
|
133 |
|
134 |
# Gradio interface
|
135 |
with gr.Blocks() as demo:
|
136 |
+
api_key_input = gr.Textbox(label="OpenAI API Key", type="password", placeholder="Enter your OpenAI API Key")
|
137 |
+
api_key_btn = gr.Button("Set API Key")
|
138 |
+
api_key_status = gr.Textbox(value="API Key status", interactive=False)
|
139 |
+
|
140 |
+
api_key_btn.click(
|
141 |
+
fn=set_api_key,
|
142 |
+
inputs=[api_key_input],
|
143 |
+
outputs=[api_key_status]
|
144 |
+
)
|
145 |
+
|
146 |
with gr.Tab("Inst RAG"):
|
147 |
with gr.Column():
|
148 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
btn = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"])
|
150 |
show_img = gr.Image(label="Uploaded PDF")
|
151 |
+
|
152 |
btn.upload(
|
153 |
fn=purge_chat_and_render_first,
|
154 |
inputs=[btn],
|
155 |
outputs=[show_img],
|
156 |
)
|
157 |
|
158 |
+
with gr.Row():
|
159 |
process_btn = gr.Button("Process PDF")
|
160 |
show_img_processed = gr.Image(label="Processed PDF")
|
161 |
process_status = gr.Textbox(label="Processing Status", interactive=False)
|
162 |
+
|
163 |
process_btn.click(
|
164 |
fn=lambda file: (app.process_file(file), "Processing complete!"),
|
165 |
inputs=[btn],
|
166 |
outputs=[show_img_processed, process_status],
|
167 |
)
|
168 |
|
169 |
+
with gr.Row():
|
170 |
build_vector_btn = gr.Button("Build Vector Database")
|
171 |
status_text = gr.Textbox(label="Status", value="", interactive=False)
|
172 |
+
|
173 |
build_vector_btn.click(
|
174 |
fn=app.build_chain,
|
175 |
inputs=[btn],
|
176 |
outputs=[status_text],
|
177 |
)
|
178 |
|
179 |
+
with gr.Row():
|
180 |
chatbot = gr.Chatbot(elem_id="chatbot")
|
181 |
txt = gr.Textbox(
|
182 |
show_label=False,
|
|
|
214 |
refresh_btn_current = gr.Button("Refresh Chat", scale=1)
|
215 |
source_texts_output_current = gr.Textbox(label="Source Texts", interactive=False)
|
216 |
|
|
|
|
|
|
|
217 |
submit_btn_current.click(
|
218 |
fn=add_text,
|
219 |
inputs=[chatbot_current, txt_current],
|