Spaces:
Sleeping
Sleeping
File size: 7,766 Bytes
a5c096a 7ddbf9d a5c096a 7ddbf9d a5c096a 7ddbf9d a5c096a 7ddbf9d a5c096a |
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 |
import asyncio
import gradio as gr
import numpy as np
import time
import json
import os
import tempfile
import requests
import logging
from aiohttp import ClientSession
from langchain.text_splitter import RecursiveCharacterTextSplitter
from datasets import Dataset, load_dataset
from tqdm import tqdm
from tqdm.asyncio import tqdm_asyncio
HF_TOKEN = os.getenv("HF_TOKEN")
SEMAPHORE_BOUND = os.getenv("SEMAPHORE_BOUND", "5")
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Chunker:
def __init__(self, strategy, split_seq=".", chunk_len=512):
self.split_seq = split_seq
self.chunk_len = chunk_len
if strategy == "recursive":
self.split = RecursiveCharacterTextSplitter().split_text
if strategy == "sequence":
self.split = self.seq_splitter
if strategy == "constant":
self.split = self.const_splitter
def seq_splitter(self, text):
return text.split(self.split_seq)
def const_splitter(self, text):
return [
text[i * self.chunk_len:(i + 1) * self.chunk_len]
for i in range(int(np.ceil(len(text) / self.chunk_len)))
]
def generator(input_ds, input_text_col, chunker):
for i in tqdm(range(len(input_ds))):
chunks = chunker.split(input_ds[i][input_text_col])
for chunk in chunks:
if chunk:
yield {input_text_col: chunk}
def chunk(input_ds, input_splits, input_text_col, output_ds, strategy, split_seq, chunk_len, private):
input_splits = [spl.strip() for spl in input_splits.split(",") if spl]
input_ds = load_dataset(input_ds, split="+".join(input_splits))
chunker = Chunker(strategy, split_seq, chunk_len)
gen_kwargs = {
"input_ds": input_ds,
"input_text_col": input_text_col,
"chunker": chunker
}
dataset = Dataset.from_generator(generator, gen_kwargs=gen_kwargs)
dataset.push_to_hub(
output_ds,
private=private,
token=HF_TOKEN
)
logger.info("Done chunking")
async def embed_sent(sentence, embed_in_text_col, semaphore, tei_url, tmp_file):
async with semaphore:
payload = {
"inputs": sentence,
"truncate": True
}
async with ClientSession(
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {HF_TOKEN}"
}
) as session:
async with session.post(tei_url, json=payload) as resp:
if resp.status != 200:
raise RuntimeError(await resp.text())
result = await resp.json()
tmp_file.write(
json.dumps({"vector": result[0], embed_in_text_col: sentence}) + "\n"
)
async def embed_ds(input_ds, tei_url, embed_in_text_col, temp_file):
semaphore = asyncio.BoundedSemaphore(int(SEMAPHORE_BOUND))
jobs = [
asyncio.create_task(embed_sent(row[embed_in_text_col], embed_in_text_col, semaphore, tei_url, temp_file))
for row in input_ds if row[embed_in_text_col].strip()
]
logger.info(f"num chunks to embed: {len(jobs)}")
tic = time.time()
await tqdm_asyncio.gather(*jobs)
logger.info(f"embed time: {time.time() - tic}")
def wake_up_endpoint(url):
n_loop = 0
while requests.get(
url=url,
headers={"Authorization": f"Bearer {HF_TOKEN}"}
).status_code != 200:
time.sleep(2)
n_loop += 1
if n_loop > 30:
raise TimeoutError("TEI endpoint is unavailable")
logger.info("TEI endpoint is up")
def run_embed(input_ds, input_splits, embed_in_text_col, output_ds, tei_url, private):
wake_up_endpoint(tei_url)
input_splits = [spl.strip() for spl in input_splits.split(",") if spl]
input_ds = load_dataset(input_ds, split="+".join(input_splits))
with tempfile.NamedTemporaryFile(mode="a", suffix=".jsonl") as temp_file:
asyncio.run(embed_ds(input_ds, tei_url, embed_in_text_col, temp_file))
dataset = Dataset.from_json(temp_file.name)
dataset.push_to_hub(
output_ds,
private=private,
token=HF_TOKEN
)
logger.info("Done embedding")
def change_dropdown(choice):
if choice == "recursive" or choice == "sequence":
return [
gr.Textbox(visible=True),
gr.Textbox(visible=False)
]
else:
return [
gr.Textbox(visible=False),
gr.Textbox(visible=True)
]
with gr.Blocks() as demo:
gr.Markdown(
"""
## Chunk your dataset before embedding
"""
)
with gr.Tab("Chunk"):
chunk_in_ds = gr.Textbox(lines=1, label="Input dataset name")
with gr.Row():
chunk_in_splits = gr.Textbox(lines=1, label="Input dataset splits", placeholder="train, test")
chunk_in_text_col = gr.Textbox(lines=1, label="Input text column name", placeholder="text")
with gr.Row():
chunk_out_ds = gr.Textbox(lines=1, label="Output dataset name", scale=6)
chunk_private = gr.Checkbox(label="Make chunked dataset private")
with gr.Row():
dropdown = gr.Dropdown(
["recursive", "sequence", "constant"], label="Chunking strategy",
info="'recursive' uses a Langchain recursive tokenizer, 'sequence' splits texts by a chosen sequence, "
"'constant' makes chunks of the constant size",
scale=2
)
split_seq = gr.Textbox(
lines=1,
interactive=True,
visible=False,
label="Sequence",
info="A text sequence to split on",
placeholder="\n\n"
)
chunk_len = gr.Textbox(
lines=1,
interactive=True,
visible=False,
label="Length",
info="The length of chunks to split into",
placeholder="512"
)
dropdown.change(fn=change_dropdown, inputs=dropdown, outputs=[split_seq, chunk_len])
with gr.Row():
gr.ClearButton(
components=[
chunk_in_ds, chunk_in_splits, chunk_in_text_col, chunk_out_ds,
dropdown, split_seq, chunk_len, chunk_private
]
)
chunk_btn = gr.Button("Chunk")
chunk_btn.click(
fn=chunk,
inputs=[chunk_in_ds, chunk_in_splits, chunk_in_text_col, chunk_out_ds,
dropdown, split_seq, chunk_len, chunk_private]
)
with gr.Tab("Embed"):
embed_in_ds = gr.Textbox(lines=1, label="Input dataset name")
with gr.Row():
embed_in_splits = gr.Textbox(lines=1, label="Input dataset splits", placeholder="train, test")
embed_in_text_col = gr.Textbox(lines=1, label="Input text column name", placeholder="text")
with gr.Row():
embed_out_ds = gr.Textbox(lines=1, label="Output dataset name", scale=6)
embed_private = gr.Checkbox(label="Make embedded dataset private")
tei_url = gr.Textbox(lines=1, label="TEI endpoint url")
with gr.Row():
gr.ClearButton(
components=[embed_in_ds, embed_in_splits, embed_in_text_col, embed_out_ds, tei_url, embed_private]
)
embed_btn = gr.Button("Run embed")
embed_btn.click(
fn=run_embed,
inputs=[embed_in_ds, embed_in_splits, embed_in_text_col, embed_out_ds, tei_url, embed_private]
)
demo.launch(debug=True) |