Update app.py
Browse files
app.py
CHANGED
@@ -75,44 +75,6 @@ def get_response(history, query):
|
|
75 |
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
76 |
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
77 |
|
78 |
-
# Function to get response for the current RAG tab
|
79 |
-
def get_response_current(history, query):
|
80 |
-
if app.chain is None:
|
81 |
-
raise gr.Error("The chain has not been built yet. Please ensure the vector database is built before querying.")
|
82 |
-
|
83 |
-
try:
|
84 |
-
result = app.chain.invoke(
|
85 |
-
{"question": query, "chat_history": app.chat_history}
|
86 |
-
)
|
87 |
-
app.chat_history.append((query, result["answer"]))
|
88 |
-
source_docs = result["source_documents"]
|
89 |
-
source_texts = []
|
90 |
-
for doc in source_docs:
|
91 |
-
source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
|
92 |
-
source_texts_str = "\n\n".join(source_texts)
|
93 |
-
history[-1] = (history[-1][0], result["answer"])
|
94 |
-
return history, source_texts_str
|
95 |
-
except Exception as e:
|
96 |
-
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
97 |
-
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
98 |
-
|
99 |
-
# Function to render file
|
100 |
-
def render_file(file) -> Image.Image:
|
101 |
-
doc = fitz.open(file.name)
|
102 |
-
page = doc[0]
|
103 |
-
pix = page.get_pixmap(dpi=150)
|
104 |
-
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
105 |
-
return image
|
106 |
-
|
107 |
-
# Function to purge chat and render first page of PDF
|
108 |
-
def purge_chat_and_render_first(file) -> Image.Image:
|
109 |
-
app.chat_history = []
|
110 |
-
doc = fitz.open(file.name)
|
111 |
-
page = doc[0]
|
112 |
-
pix = page.get_pixmap(dpi=150)
|
113 |
-
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
114 |
-
return image
|
115 |
-
|
116 |
# Function to refresh chat
|
117 |
def refresh_chat():
|
118 |
app.chat_history = []
|
@@ -167,49 +129,45 @@ with gr.Blocks() as demo:
|
|
167 |
outputs=[api_key_status]
|
168 |
)
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
196 |
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
question_submit_btn.click(
|
206 |
-
fn=add_text,
|
207 |
-
inputs=[chatbot_current, question_dropdown],
|
208 |
-
outputs=[chatbot_current],
|
209 |
-
queue=False,
|
210 |
-
).success(
|
211 |
-
fn=get_response_current, inputs=[chatbot_current, question_dropdown], outputs=[chatbot_current, source_texts_output_current]
|
212 |
-
)
|
213 |
|
214 |
demo.queue()
|
215 |
demo.launch()
|
|
|
75 |
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
76 |
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
# Function to refresh chat
|
79 |
def refresh_chat():
|
80 |
app.chat_history = []
|
|
|
129 |
outputs=[api_key_status]
|
130 |
)
|
131 |
|
132 |
+
chatbot_current = gr.Chatbot(elem_id="chatbot_current")
|
133 |
+
txt_current = gr.Textbox(
|
134 |
+
show_label=False,
|
135 |
+
placeholder="Enter text and press submit",
|
136 |
+
scale=2
|
137 |
+
)
|
138 |
+
submit_btn_current = gr.Button("Submit", scale=1)
|
139 |
+
refresh_btn_current = gr.Button("Refresh Chat", scale=1)
|
140 |
+
source_texts_output_current = gr.Textbox(label="Source Texts", interactive=False)
|
141 |
+
|
142 |
+
submit_btn_current.click(
|
143 |
+
fn=add_text,
|
144 |
+
inputs=[chatbot_current, txt_current],
|
145 |
+
outputs=[chatbot_current],
|
146 |
+
queue=False,
|
147 |
+
).success(
|
148 |
+
fn=get_response, inputs=[chatbot_current, txt_current], outputs=[chatbot_current, source_texts_output_current]
|
149 |
+
)
|
150 |
+
|
151 |
+
refresh_btn_current.click(
|
152 |
+
fn=refresh_chat,
|
153 |
+
inputs=[],
|
154 |
+
outputs=[chatbot_current],
|
155 |
+
)
|
156 |
+
|
157 |
+
question_dropdown = gr.Dropdown(
|
158 |
+
label="Select an example question",
|
159 |
+
choices=questions
|
160 |
+
)
|
161 |
+
question_submit_btn = gr.Button("Submit Question")
|
162 |
|
163 |
+
question_submit_btn.click(
|
164 |
+
fn=add_text,
|
165 |
+
inputs=[chatbot_current, question_dropdown],
|
166 |
+
outputs=[chatbot_current],
|
167 |
+
queue=False,
|
168 |
+
).success(
|
169 |
+
fn=get_response, inputs=[chatbot_current, question_dropdown], outputs=[chatbot_current, source_texts_output_current]
|
170 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
demo.queue()
|
173 |
demo.launch()
|