Spaces:
Sleeping
Sleeping
File size: 6,890 Bytes
70c89ac cb0e659 70c89ac aacae39 78fe2de aacae39 422be11 70c89ac a1ac10e 70c89ac aacae39 70c89ac 1816e1b a45ad57 559b8d7 a1ac10e 70c89ac 13ac9a8 1816e1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
import gradio as gr
from langchain.embeddings import HuggingFaceEmbeddings, HuggingFaceInstructEmbeddings, OpenAIEmbeddings
from pymilvus import Collection, connections
import json
import os
import subprocess
os.environ["TOKENIZERS_PARALLELISM"] = "false"
MILVUS_COLLECTION = os.environ.get("MILVUS_COLLECTION", "LangChainCollection")
MILVUS_HOST = os.environ.get("MILVUS_HOST", "")
MILVUS_PORT = "19530"
EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "hkunlp/instructor-large")
EMBEDDING_LOADER = os.environ.get("EMBEDDING_LOADER", "HuggingFaceInstructEmbeddings")
EMBEDDING_LIST = ["HuggingFaceInstructEmbeddings", "HuggingFaceEmbeddings"]
# return top-k text chunks from vector store
TOP_K_DEFAULT = 15
TOP_K_MAX = 30
SCORE_DEFAULT = 0.33
BUTTON_MIN_WIDTH = 100
global g_emb
g_emb = None
global g_col
g_col = None
def init_emb(emb_name, emb_loader, db_col_textbox):
global g_emb
global g_col
g_emb = eval(emb_loader)(model_name=emb_name)
connections.connect(
host=MILVUS_HOST,
port=MILVUS_PORT
)
g_col = Collection(db_col_textbox)
g_col.load()
return (str(g_emb), str(g_col))
def get_emb():
return g_emb
def get_col():
return g_col
def remove_duplicates(documents, score_min):
seen_content = set()
unique_documents = []
for (doc, score) in documents:
if (doc.page_content not in seen_content) and (score >= score_min):
seen_content.add(doc.page_content)
unique_documents.append(doc)
return unique_documents
def get_data(query, top_k, score, db_col, db_index):
if not query:
return "Please init db in configuration"
embed_query = g_emb.embed_query(query)
search_params = {"metric_type": "L2",
"params": {"nprobe": 1},
"offset": 0}
results = g_col.search(
data=[embed_query],
anns_field="vector",
param=search_params,
limit=top_k,
expr=None,
output_fields=['source', 'text'],
consistency_level="Strong"
)
jsons = json.dumps([{'source': hit.entity.get('source'),
'text': hit.entity.get('text')}
for hit in results[0]],
indent=0)
return jsons
def run_command(command):
try:
result = subprocess.check_output(command, shell=True, text=True)
return result
except subprocess.CalledProcessError as e:
return f"Error: {e}"
with gr.Blocks(
title = "3GPP Database",
theme = "Base",
css = """.bigbox {
min-height:250px;
}
""") as demo:
with gr.Tab("Matching"):
with gr.Accordion("Vector similarity"):
with gr.Row():
with gr.Column():
top_k = gr.Slider(1,
TOP_K_MAX,
value=TOP_K_DEFAULT,
step=1,
label="Vector similarity top_k",
interactive=True)
with gr.Column():
score = gr.Slider(0.01,
0.99,
value=SCORE_DEFAULT,
step=0.01,
label="Vector similarity score",
interactive=True)
with gr.Row():
with gr.Column(scale=10):
input_box = gr.Textbox(label = "Input", placeholder="What are you looking for?")
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
btn_run = gr.Button("Run", variant="primary")
output_box = gr.JSON(label = "Output")
with gr.Tab("Configuration"):
with gr.Row():
btn_init = gr.Button("Init")
load_emb = gr.Textbox(get_emb, label = 'Embedding Client', show_label=True)
load_col = gr.Textbox(get_col, label = 'Milvus Collection', show_label=True)
with gr.Accordion("Embedding"):
with gr.Row():
with gr.Column():
emb_textbox = gr.Textbox(
label = "Embedding Model",
# show_label = False,
value = EMBEDDING_MODEL,
placeholder = "Paste Your Embedding Model Repo on HuggingFace",
lines=1,
interactive=True,
type='email')
with gr.Column():
emb_dropdown = gr.Dropdown(
EMBEDDING_LIST,
value=EMBEDDING_LOADER,
multiselect=False,
interactive=True,
label="Embedding Loader")
with gr.Accordion("Milvus Database"):
with gr.Row():
db_col_textbox = gr.Textbox(
label = "Milvus Collection",
# show_label = False,
value = MILVUS_COLLECTION,
placeholder = "Paste Your Milvus Collection (xx-xx-xx) and Hit ENTER",
lines=1,
interactive=True,
type='email')
db_index_textbox = gr.Textbox(
label = "Milvus Host",
# show_label = False,
value = MILVUS_HOST,
placeholder = "Paste Your Milvus Index (xxxx) and Hit ENTER",
lines=1,
interactive=True,
type='password')
btn_init.click(fn=init_emb,
inputs=[emb_textbox, emb_dropdown, db_col_textbox],
outputs=[load_emb, load_col])
btn_run.click(fn=get_data,
inputs=[input_box, top_k, score, db_col_textbox, db_index_textbox],
outputs=[output_box])
if __name__ == "__main__":
demo.queue()
demo.launch(server_name="0.0.0.0",
server_port=7860)
'''
import gradio as gr
import subprocess
def run_command(command):
try:
result = subprocess.check_output(command, shell=True, text=True)
return result
except subprocess.CalledProcessError as e:
return f"Error: {e}"
iface = gr.Interface(
fn=run_command,
inputs="text",
outputs="text",
title="Command Output Viewer",
description="Enter a command and view its output.",
examples=[
["ls"],
["pwd"],
["echo 'Hello, Gradio!'"],
["python --version"]
]
)
# Updated line with additional port binding for Milvus server
iface.launch(server_name="0.0.0.0", server_port=7860, share=True, debug=True)
''' |