Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import json
|
4 |
import shutil
|
|
|
5 |
from datetime import datetime
|
6 |
from retriever import retriever, reload_retriever
|
7 |
from generator import answer_query
|
@@ -11,7 +12,6 @@ from langchain_community.document_loaders import (
|
|
11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
12 |
from langchain_huggingface import HuggingFaceEmbeddings
|
13 |
from langchain_community.vectorstores import FAISS
|
14 |
-
import html
|
15 |
|
16 |
# Đường dẫn file CSS
|
17 |
CUSTOM_CSS_PATH = "gradio_theme.css"
|
@@ -54,7 +54,7 @@ def process_document(file):
|
|
54 |
elif file_path.endswith(".csv"):
|
55 |
loader = CSVLoader(file_path)
|
56 |
elif file_path.endswith(".txt"):
|
57 |
-
loader = TextLoader(file_path, autodetect_encoding=True)
|
58 |
elif file_path.endswith(".docx") or file_path.endswith(".doc"):
|
59 |
loader = UnstructuredWordDocumentLoader(file_path)
|
60 |
else:
|
@@ -90,8 +90,9 @@ def delete_file(filename):
|
|
90 |
def clear_inputs():
|
91 |
return "", ""
|
92 |
|
93 |
-
def query_function(question,
|
94 |
-
|
|
|
95 |
answer = html.escape(answer)
|
96 |
|
97 |
if include_sources and docs:
|
@@ -109,38 +110,47 @@ def query_function(question, model_choice, temperature, include_sources):
|
|
109 |
answer += sources_text
|
110 |
return answer
|
111 |
|
112 |
-
#
|
113 |
with gr.Blocks(css=CUSTOM_CSS_PATH) as demo:
|
114 |
with gr.Row():
|
115 |
with gr.Column(scale=5):
|
116 |
gr.Markdown("## 🔍 RAGFlow Enterprise Search\nTìm kiếm thông minh từ tài liệu nội bộ", elem_classes="container-box")
|
117 |
|
118 |
with gr.Tabs():
|
|
|
119 |
with gr.TabItem("🔍 Tìm kiếm"):
|
120 |
with gr.Column(elem_classes="container-box"):
|
121 |
-
question
|
122 |
-
with gr.Row():
|
123 |
-
temperature
|
124 |
include_sources = gr.Checkbox(label="Hiển thị nguồn", value=True)
|
125 |
with gr.Row():
|
126 |
search_btn = gr.Button("🔍 Tìm kiếm", variant="primary", elem_classes="button-primary")
|
127 |
-
clear_btn
|
128 |
-
output = gr.Markdown(elem_classes="output-box")
|
129 |
|
130 |
-
search_btn.click(query_function,
|
131 |
-
|
|
|
|
|
|
|
132 |
|
|
|
133 |
with gr.TabItem("📚 Quản lý tài liệu"):
|
134 |
with gr.Column(elem_classes="container-box"):
|
135 |
upload_file = gr.File(label="Tải lên tài liệu", file_types=[".pdf", ".docx", ".doc", ".csv", ".txt"])
|
136 |
-
upload_btn
|
137 |
upload_status = gr.Textbox(label="Trạng thái", lines=3, interactive=False)
|
138 |
uploaded_files_list = gr.Markdown(value=update_uploaded_files(), elem_classes="scroll-box")
|
139 |
with gr.Column(elem_classes="container-box"):
|
140 |
delete_filename = gr.Textbox(label="Tên file muốn xóa")
|
141 |
-
delete_btn
|
142 |
-
|
143 |
-
upload_btn.click(process_document,
|
144 |
-
|
|
|
|
|
|
|
|
|
145 |
|
146 |
demo.launch(share=True)
|
|
|
2 |
import os
|
3 |
import json
|
4 |
import shutil
|
5 |
+
import html
|
6 |
from datetime import datetime
|
7 |
from retriever import retriever, reload_retriever
|
8 |
from generator import answer_query
|
|
|
12 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
from langchain_huggingface import HuggingFaceEmbeddings
|
14 |
from langchain_community.vectorstores import FAISS
|
|
|
15 |
|
16 |
# Đường dẫn file CSS
|
17 |
CUSTOM_CSS_PATH = "gradio_theme.css"
|
|
|
54 |
elif file_path.endswith(".csv"):
|
55 |
loader = CSVLoader(file_path)
|
56 |
elif file_path.endswith(".txt"):
|
57 |
+
loader = TextLoader(file_path, autodetect_encoding=True)
|
58 |
elif file_path.endswith(".docx") or file_path.endswith(".doc"):
|
59 |
loader = UnstructuredWordDocumentLoader(file_path)
|
60 |
else:
|
|
|
90 |
def clear_inputs():
|
91 |
return "", ""
|
92 |
|
93 |
+
def query_function(question, temperature, include_sources):
|
94 |
+
fixed_model = "sentence-transformers/all-MiniLM-L6-v2"
|
95 |
+
answer, docs = answer_query(question, model=fixed_model, temperature=temperature)
|
96 |
answer = html.escape(answer)
|
97 |
|
98 |
if include_sources and docs:
|
|
|
110 |
answer += sources_text
|
111 |
return answer
|
112 |
|
113 |
+
# Tạo giao diện Gradio
|
114 |
with gr.Blocks(css=CUSTOM_CSS_PATH) as demo:
|
115 |
with gr.Row():
|
116 |
with gr.Column(scale=5):
|
117 |
gr.Markdown("## 🔍 RAGFlow Enterprise Search\nTìm kiếm thông minh từ tài liệu nội bộ", elem_classes="container-box")
|
118 |
|
119 |
with gr.Tabs():
|
120 |
+
# Tab Tìm kiếm
|
121 |
with gr.TabItem("🔍 Tìm kiếm"):
|
122 |
with gr.Column(elem_classes="container-box"):
|
123 |
+
question = gr.Textbox(lines=3, label="Câu hỏi")
|
124 |
+
with gr.Row():
|
125 |
+
temperature = gr.Slider(0, 1, value=0.2, step=0.1, label="Temperature")
|
126 |
include_sources = gr.Checkbox(label="Hiển thị nguồn", value=True)
|
127 |
with gr.Row():
|
128 |
search_btn = gr.Button("🔍 Tìm kiếm", variant="primary", elem_classes="button-primary")
|
129 |
+
clear_btn = gr.Button("🗑️ Xóa", variant="secondary", elem_classes="button-secondary")
|
130 |
+
output = gr.Markdown(elem_classes="output-box")
|
131 |
|
132 |
+
search_btn.click(query_function,
|
133 |
+
inputs=[question, temperature, include_sources],
|
134 |
+
outputs=[output])
|
135 |
+
clear_btn.click(clear_inputs,
|
136 |
+
outputs=[question, output])
|
137 |
|
138 |
+
# Tab Quản lý tài liệu
|
139 |
with gr.TabItem("📚 Quản lý tài liệu"):
|
140 |
with gr.Column(elem_classes="container-box"):
|
141 |
upload_file = gr.File(label="Tải lên tài liệu", file_types=[".pdf", ".docx", ".doc", ".csv", ".txt"])
|
142 |
+
upload_btn = gr.Button("📄 Tải lên và xử lý", variant="primary")
|
143 |
upload_status = gr.Textbox(label="Trạng thái", lines=3, interactive=False)
|
144 |
uploaded_files_list = gr.Markdown(value=update_uploaded_files(), elem_classes="scroll-box")
|
145 |
with gr.Column(elem_classes="container-box"):
|
146 |
delete_filename = gr.Textbox(label="Tên file muốn xóa")
|
147 |
+
delete_btn = gr.Button("🗑️ Xóa tài liệu", variant="secondary")
|
148 |
+
|
149 |
+
upload_btn.click(process_document,
|
150 |
+
inputs=[upload_file],
|
151 |
+
outputs=[upload_status, uploaded_files_list])
|
152 |
+
delete_btn.click(delete_file,
|
153 |
+
inputs=[delete_filename],
|
154 |
+
outputs=[uploaded_files_list])
|
155 |
|
156 |
demo.launch(share=True)
|