Spaces:
Sleeping
Sleeping
from langchain.vectorstores import Qdrant | |
from langchain.embeddings import HuggingFaceEmbeddings | |
from qdrant_client import QdrantClient | |
import gradio as gr | |
def process_text(input_text, top_k): | |
embeddings = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-base") | |
client = QdrantClient( | |
path="./local_qdrant", prefer_grpc=True | |
) | |
db = Qdrant(client=client, embeddings=embeddings, collection_name="qa_data") | |
query = input_text | |
all_answers = [] | |
docs = db.similarity_search_with_score(query=query, k=top_k) | |
for i in docs: | |
doc, score = i | |
print({"score": score, "content": doc.page_content, "metadata": doc.metadata} ) | |
all_answers.append(doc.metadata["source"]) | |
return "\n***\\n".join(all_answers) | |
CSS =""" | |
.contain { display: flex; flex-direction: column; } | |
.gradio-container { height: 100vh !important; } | |
#component-0 { height: 100%; } | |
#textbox { flex-grow: 1; overflow: auto; resize: vertical; } | |
.secondary {background-color: #6366f1; } | |
""" | |
#with gr.Blocks() as demo: | |
with gr.Blocks(theme=gr.themes.Monochrome(radius_size=gr.themes.sizes.radius_sm)) as demo: | |
with gr.Row(): | |
gr.Markdown("# 裁定検索") | |
with gr.Row(): | |
output = gr.TextArea( | |
elem_id="検索結果", | |
label="検索結果", | |
) | |
with gr.Row(): | |
input = gr.Textbox( | |
label="質問", | |
placeholder="芸魔龍王アメイジンの出た時の効果は、後から出たクリーチャーも影響しますか", | |
lines=3, | |
) | |
with gr.Row(): | |
submit = gr.Button(value="検索", variant="secondary").style(full_width=True) | |
top_k = gr.Slider(1, 10, label="表示数", step=1, value=5, interactive=True) | |
submit_click_event = submit.click(fn=process_text, inputs=[input, top_k], outputs=output) | |
demo.launch() | |
# demo.queue(max_size=128, concurrency_count=48).launch(debug=True, server_name="0.0.0.0", server_port=7860) | |