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environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from agent import Agent
|
| 6 |
+
from config import Config
|
| 7 |
+
from memory.chat_memory import MemoryManager
|
| 8 |
+
from utils.html_template import HtmlTemplates
|
| 9 |
+
from vector_db.qdrant_db import QdrantDBClient
|
| 10 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class WebApp:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.title = "RAGent Chatbot"
|
| 16 |
+
self.uploaded_files = None
|
| 17 |
+
self.upload_btn = None
|
| 18 |
+
self.progress_output = None
|
| 19 |
+
self.status_output = None
|
| 20 |
+
self.css = HtmlTemplates.css()
|
| 21 |
+
|
| 22 |
+
self.agent = Agent()
|
| 23 |
+
self.memory = MemoryManager()
|
| 24 |
+
self.qdrant_client = QdrantDBClient()
|
| 25 |
+
|
| 26 |
+
def build_ui(self):
|
| 27 |
+
with gr.Blocks(theme=gr.themes.Default(), css=self.css) as demo:
|
| 28 |
+
self.build_header()
|
| 29 |
+
with gr.Row():
|
| 30 |
+
self.build_upload_section()
|
| 31 |
+
self.build_chat_section()
|
| 32 |
+
return demo
|
| 33 |
+
|
| 34 |
+
def build_header(self):
|
| 35 |
+
with gr.Row():
|
| 36 |
+
with gr.Column():
|
| 37 |
+
gr.HTML(f"<h1 id='title'>π¬ {self.title}</h1>")
|
| 38 |
+
|
| 39 |
+
def clear_outputs(self):
|
| 40 |
+
return "", ""
|
| 41 |
+
|
| 42 |
+
def build_upload_section(self):
|
| 43 |
+
with gr.Column(scale=3):
|
| 44 |
+
gr.Markdown("### π Drag & Drop Files Below")
|
| 45 |
+
self.uploaded_files = gr.File(
|
| 46 |
+
file_types=Config.FILE_EXTENSIONS,
|
| 47 |
+
file_count="multiple",
|
| 48 |
+
label="pdf, docx, xlsx, pptx, csv, txt, json"
|
| 49 |
+
)
|
| 50 |
+
self.upload_btn = gr.Button(value="Upload Files", elem_id="upload-btn", icon=Config.UPLOAD_ICON)
|
| 51 |
+
self.progress_output = gr.HTML()
|
| 52 |
+
self.status_output = gr.Markdown()
|
| 53 |
+
|
| 54 |
+
self.upload_btn.click(
|
| 55 |
+
fn=self.clear_outputs,
|
| 56 |
+
inputs=[],
|
| 57 |
+
outputs=[self.progress_output, self.status_output]
|
| 58 |
+
).then(
|
| 59 |
+
fn=self.upload_and_process,
|
| 60 |
+
inputs=self.uploaded_files,
|
| 61 |
+
outputs=[self.progress_output, self.status_output],
|
| 62 |
+
show_progress="hidden"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
def build_chat_section(self):
|
| 66 |
+
with gr.Column(scale=7):
|
| 67 |
+
gr.Markdown("### π€ Ask Your Question")
|
| 68 |
+
gr.ChatInterface(
|
| 69 |
+
fn=self.run_agent,
|
| 70 |
+
type="messages",
|
| 71 |
+
show_progress="full",
|
| 72 |
+
save_history=False,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def run_agent(self, query, history):
|
| 76 |
+
session_id = Config.SESSION_ID
|
| 77 |
+
|
| 78 |
+
# Get history
|
| 79 |
+
past_messages = self.memory.get(session_id)
|
| 80 |
+
|
| 81 |
+
# Run agent (it appends the user query internally)
|
| 82 |
+
response = self.agent.run(query, past_messages)
|
| 83 |
+
#print("##### response : ", response)
|
| 84 |
+
|
| 85 |
+
# convert response to string. If response is a dict like {'input': ..., 'output': ...}
|
| 86 |
+
if isinstance(response, dict) and "output" in response:
|
| 87 |
+
answer = response["output"]
|
| 88 |
+
else:
|
| 89 |
+
answer = str(response)
|
| 90 |
+
|
| 91 |
+
# Save user + assistant message to memory
|
| 92 |
+
self.memory.add(session_id, HumanMessage(content=query))
|
| 93 |
+
self.memory.add(session_id, AIMessage(content=answer))
|
| 94 |
+
|
| 95 |
+
return f"βπ€ {answer}"
|
| 96 |
+
|
| 97 |
+
def upload_and_process(self, files):
|
| 98 |
+
if not files or len(files) == 0:
|
| 99 |
+
yield HtmlTemplates.error_bar(), ""
|
| 100 |
+
return
|
| 101 |
+
|
| 102 |
+
total = len(files)
|
| 103 |
+
failed_files = []
|
| 104 |
+
|
| 105 |
+
for i, file in enumerate(files):
|
| 106 |
+
file_path = file.name # path to temp file
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
# Load, chunk, and insert to vector DB
|
| 110 |
+
file_chunks = self.qdrant_client.load_and_chunk_docs(file_path)
|
| 111 |
+
self.qdrant_client.insert_chunks(file_chunks)
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
failed_files.append(file_path)
|
| 115 |
+
yield HtmlTemplates.progress_bar(int((i + 1) / total * 100), i + 1, total), (
|
| 116 |
+
f"β οΈ Skipped file {i + 1}/{total}: {os.path.basename(file_path)} - {str(e)}"
|
| 117 |
+
)
|
| 118 |
+
continue
|
| 119 |
+
|
| 120 |
+
percent = int((i + 1) / total * 100)
|
| 121 |
+
yield HtmlTemplates.progress_bar(percent, i + 1, total), f"π Processed {i + 1}/{total} file(s)..."
|
| 122 |
+
|
| 123 |
+
success_count = total - len(failed_files)
|
| 124 |
+
final_msg = f"β
{success_count}/{total} file(s) processed and stored in DB!"
|
| 125 |
+
|
| 126 |
+
if failed_files:
|
| 127 |
+
failed_list = "\n".join(f"β {os.path.basename(f)}" for f in failed_files)
|
| 128 |
+
final_msg += f"\n\nβ οΈ Failed to process:\n{failed_list}"
|
| 129 |
+
|
| 130 |
+
yield HtmlTemplates.progress_bar(100, total, total), final_msg
|
| 131 |
+
|
| 132 |
+
def upload_and_process1(self, files):
|
| 133 |
+
if not files or len(files) == 0:
|
| 134 |
+
yield HtmlTemplates.error_bar(), ""
|
| 135 |
+
return
|
| 136 |
+
|
| 137 |
+
total = len(files)
|
| 138 |
+
|
| 139 |
+
for i, file in enumerate(files):
|
| 140 |
+
file_path = file.name # get file path of temporary folder
|
| 141 |
+
|
| 142 |
+
# Load, chunk, and insert to vector DB
|
| 143 |
+
file_chunks = self.qdrant_client.load_and_chunk_docs(file_path)
|
| 144 |
+
self.qdrant_client.insert_chunks(file_chunks)
|
| 145 |
+
|
| 146 |
+
percent = int((i + 1) / total * 100)
|
| 147 |
+
yield HtmlTemplates.progress_bar(percent, i + 1, total), f"π Processed {i + 1}/{total} file(s)..."
|
| 148 |
+
|
| 149 |
+
yield HtmlTemplates.progress_bar(100, total, total), f"β
{total} file(s) processed and stored in DB!"
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
if __name__ == "__main__":
|
| 153 |
+
app = WebApp()
|
| 154 |
+
demo = app.build_ui()
|
| 155 |
+
demo.launch()
|