Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,35 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from config.rag_config import RAGConfig
|
| 4 |
+
from src.rag_pipeline import RAGPipeline
|
| 5 |
+
|
| 6 |
+
config = RAGConfig()
|
| 7 |
+
|
| 8 |
+
# 加载向量库
|
| 9 |
+
with open(config.vector_db_path, "rb") as f:
|
| 10 |
+
data = pickle.load(f)
|
| 11 |
+
|
| 12 |
+
docs, doc_embeddings = data["texts"], data["embeddings"]
|
| 13 |
+
pipeline = RAGPipeline(config, docs, doc_embeddings)
|
| 14 |
+
|
| 15 |
+
def answer_question(query, threshold):
|
| 16 |
+
pipeline.config.similarity_threshold = threshold
|
| 17 |
+
answer, retrieved = pipeline.ask(query)
|
| 18 |
+
context = "\n\n".join([f"Score: {s:.4f}\n{t}" for t, s in retrieved])
|
| 19 |
+
return answer, context
|
| 20 |
+
|
| 21 |
+
demo = gr.Interface(
|
| 22 |
+
fn=answer_question,
|
| 23 |
+
inputs=[
|
| 24 |
+
gr.Textbox(label="Enter your question"),
|
| 25 |
+
gr.Slider(0.0, 1.0, value=0.4, step=0.05, label="Similarity Threshold")
|
| 26 |
+
],
|
| 27 |
+
outputs=[
|
| 28 |
+
gr.Textbox(label="Answer"),
|
| 29 |
+
gr.Textbox(label="Retrieved Contexts")
|
| 30 |
+
],
|
| 31 |
+
title="📘 Multi-PDF RAG System"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|