Spaces:
Runtime error
Runtime error
some refactor
Browse filesFiles changed (4)
- .vscode/launch.jsonAdded+14
- app.py+21-63
- src/templates.py+3-11
- src/viewer_api.pyAdded+51
1234567891011121314{ "version": "0.2.0", "configurations": [ { "name": "Python Debugger: Current File", "type": "debugpy", "request": "launch", "program": "${file}", "console": "integratedTerminal", "purpose": ["debug-test"], "justMyCode": false } ]}4 unmodified lines567891011121314 unmodified lines28293031323334353637383940414243444546474849505152531 unmodified line555657585960616263646566676829 unmodified lines98991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371384 unmodified lines143144145146147148149104 unmodified lines25425525625725825926026126226326426526626726826927027119 unmodified lines29129229329429529629722 unmodified lines320321322323324325326327328329131 unmodified lines4614624634644654664674684694704714724734744754764774784794804814 unmodified linesimport osimport datamapplotimport duckdbimport numpy as npimport requestsfrom dotenv import load_dotenvfrom gradio_huggingfacehub_search import HuggingfaceHubSearch14 unmodified linesfrom src.hub import create_space_with_contentfrom src.templates import REPRESENTATION_PROMPT, SPACE_REPO_CARD_CONTENTload_dotenv()HF_TOKEN = os.getenv("HF_TOKEN")assert HF_TOKEN is not None, "You need to set HF_TOKEN in your environment variables"EXPORTS_REPOSITORY = os.getenv("EXPORTS_REPOSITORY")assert (EXPORTS_REPOSITORY is not None), "You need to set EXPORTS_REPOSITORY in your environment variables"MAX_ROWS = int(os.getenv("MAX_ROWS", "8_000"))CHUNK_SIZE = int(os.getenv("CHUNK_SIZE", "2_000"))DATASET_VIEWE_API_URL = "https://datasets-server.huggingface.co/"DATASETS_TOPICS_ORGANIZATION = os.getenv("DATASETS_TOPICS_ORGANIZATION", "datasets-topics")USE_ARROW_STYLE = int(os.getenv("USE_ARROW_STYLE", "0"))USE_CUML = int(os.getenv("USE_CUML", "0"))if USE_CUML:from cuml.manifold import UMAPfrom cuml.cluster import HDBSCAN1 unmodified linefrom umap import UMAPfrom hdbscan import HDBSCANUSE_LLM_TEXT_GENERATION = int(os.getenv("USE_LLM_TEXT_GENERATION", "1"))logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")api = HfApi(token=HF_TOKEN)session = requests.Session()sentence_model = SentenceTransformer("all-MiniLM-L6-v2")# Representation model29 unmodified linesvectorizer_model = CountVectorizer(stop_words="english")def get_split_rows(dataset, config, split):config_size = session.get(f"{DATASET_VIEWE_API_URL}/size?dataset={dataset}&config={config}",timeout=20,).json()if "error" in config_size:raise Exception(f"Error fetching config size: {config_size['error']}")split_size = next((s for s in config_size["size"]["splits"] if s["split"] == split),None,)if split_size is None:raise Exception(f"Error fetching split {split} in config {config}")return split_size["num_rows"]def get_parquet_urls(dataset, config, split):parquet_files = session.get(f"{DATASET_VIEWE_API_URL}/parquet?dataset={dataset}&config={config}&split={split}",timeout=20,).json()if "error" in parquet_files:raise Exception(f"Error fetching parquet files: {parquet_files['error']}")parquet_urls = [file["url"] for file in parquet_files["parquet_files"]]logging.debug(f"Parquet files: {parquet_urls}")return ",".join(f"'{url}'" for url in parquet_urls)def get_docs_from_parquet(parquet_urls, column, offset, limit):SQL_QUERY = f"SELECT {column} FROM read_parquet([{parquet_urls}]) LIMIT {limit} OFFSET {offset};"df = duckdb.sql(SQL_QUERY).to_df()return df[column].tolist()# @spaces.GPUdef calculate_embeddings(docs):return sentence_model.encode(docs, show_progress_bar=True, batch_size=32)4 unmodified linesreturn n_neighbors, n_components# @spaces.GPUdef fit_model(docs, embeddings, n_neighbors, n_components):umap_model = UMAP(n_neighbors=n_neighbors,104 unmodified linesreduced_embeddings_array = np.vstack(reduced_embeddings_list)topics_info = base_model.get_topic_info()all_topics, _ = base_model.transform(all_docs)all_topics = np.array(all_topics)sub_title = (f"Data map for the entire dataset ({limit} rows) using the column '{column}'"if full_processingelse f"Data map for a sample of the dataset (first {limit} rows) using the column '{column}'")topic_plot = (base_model.visualize_document_datamap(docs=all_docs,reduced_embeddings=reduced_embeddings_array,title=dataset,sub_title=sub_title,19 unmodified linestitle=dataset,))rows_processed += len(docs)progress = min(rows_processed / limit, 1.0)logging.info(f"Progress: {progress} % - {rows_processed} of {limit}")22 unmodified lineselse:topic_plot.write_image(plot_png)all_topics, _ = base_model.transform(all_docs)topic_info = base_model.get_topic_info()topic_names = {row["Topic"]: row["Name"] for index, row in topic_info.iterrows()}topic_names_array = np.array([topic_names.get(topic, "No Topic").split("_")[1].strip("-")131 unmodified linestext_column_dropdown: gr.Dropdown(label="Text column name"),nested_text_column_dropdown: gr.Dropdown(visible=False),}info_resp = session.get(f"{DATASET_VIEWE_API_URL}/info?dataset={dataset}", timeout=20).json()if "error" in info_resp:return {subset_dropdown: gr.Dropdown(visible=False),split_dropdown: gr.Dropdown(visible=False),text_column_dropdown: gr.Dropdown(label="Text column name"),nested_text_column_dropdown: gr.Dropdown(visible=False),}subsets: list[str] = list(info_resp["dataset_info"])subset = default_subset if default_subset in subsets else subsets[0]splits: list[str] = list(info_resp["dataset_info"][subset]["splits"])split = default_split if default_split in splits else splits[0]features = info_resp["dataset_info"][subset]["features"]def _is_string_feature(feature):return isinstance(feature, dict) and feature.get("dtype") == "string"4 unmodified lines56789101114 unmodified lines262728293031323334353637383940414243444546474849505152531 unmodified line55565758596061626364656629 unmodified lines969798991001014 unmodified lines106107108109110111104 unmodified lines21621721821922022122222322422522622722822923023119 unmodified lines25125225325425525622 unmodified lines279280281282283284285286287288131 unmodified lines4204214224234244254264274284294304314324334344354364374384394 unmodified linesimport osimport datamapplotimport numpy as npfrom dotenv import load_dotenvfrom gradio_huggingfacehub_search import HuggingfaceHubSearch14 unmodified linesfrom src.hub import create_space_with_contentfrom src.templates import REPRESENTATION_PROMPT, SPACE_REPO_CARD_CONTENTfrom src.viewer_api import (get_split_rows,get_parquet_urls,get_docs_from_parquet,get_info,)# Load environment variablesload_dotenv()HF_TOKEN = os.getenv("HF_TOKEN")assert HF_TOKEN is not None, "You need to set HF_TOKEN in your environment variables"MAX_ROWS = int(os.getenv("MAX_ROWS", "8_000"))CHUNK_SIZE = int(os.getenv("CHUNK_SIZE", "2_000"))DATASETS_TOPICS_ORGANIZATION = os.getenv("DATASETS_TOPICS_ORGANIZATION", "datasets-topics")USE_ARROW_STYLE = int(os.getenv("USE_ARROW_STYLE", "0"))USE_CUML = int(os.getenv("USE_CUML", "1"))USE_LLM_TEXT_GENERATION = int(os.getenv("USE_LLM_TEXT_GENERATION", "1"))# Use cuml lib only if configuredif USE_CUML:from cuml.manifold import UMAPfrom cuml.cluster import HDBSCAN1 unmodified linefrom umap import UMAPfrom hdbscan import HDBSCANlogging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")api = HfApi(token=HF_TOKEN)sentence_model = SentenceTransformer("all-MiniLM-L6-v2")# Representation model29 unmodified linesvectorizer_model = CountVectorizer(stop_words="english")def calculate_embeddings(docs):return sentence_model.encode(docs, show_progress_bar=True, batch_size=32)4 unmodified linesreturn n_neighbors, n_componentsdef fit_model(docs, embeddings, n_neighbors, n_components):umap_model = UMAP(n_neighbors=n_neighbors,104 unmodified linesreduced_embeddings_array = np.vstack(reduced_embeddings_list)topics_info = base_model.get_topic_info()all_topics = base_model.topics_sub_title = (f"Data map for the entire dataset ({limit} rows) using the column '{column}'"if full_processingelse f"Data map for a sample of the dataset (first {limit} rows) using the column '{column}'")topic_plot = (base_model.visualize_document_datamap(docs=all_docs,topics=all_topics,reduced_embeddings=reduced_embeddings_array,title=dataset,sub_title=sub_title,19 unmodified linestitle=dataset,))rows_processed += len(docs)progress = min(rows_processed / limit, 1.0)logging.info(f"Progress: {progress} % - {rows_processed} of {limit}")22 unmodified lineselse:topic_plot.write_image(plot_png)all_topics = base_model.topics_topic_info = base_model.get_topic_info()topic_names = {row["Topic"]: row["Name"] for _, row in topic_info.iterrows()}topic_names_array = np.array([topic_names.get(topic, "No Topic").split("_")[1].strip("-")131 unmodified linestext_column_dropdown: gr.Dropdown(label="Text column name"),nested_text_column_dropdown: gr.Dropdown(visible=False),}try:info_resp = get_info(dataset)except Exception:return {subset_dropdown: gr.Dropdown(visible=False),split_dropdown: gr.Dropdown(visible=False),text_column_dropdown: gr.Dropdown(label="Text column name"),nested_text_column_dropdown: gr.Dropdown(visible=False),}subsets: list[str] = list(info_resp)subset = default_subset if default_subset in subsets else subsets[0]splits: list[str] = list(info_resp[subset]["splits"])split = default_split if default_split in splits else splits[0]features = info_resp[subset]["features"]def _is_string_feature(feature):return isinstance(feature, dict) and feature.get("dtype") == "string"
4 unmodified lines56789101112131415162 unmodified lines192021222324252627283 unmodified lines323334353637384 unmodified lines"""EXAMPLE_PROMPT = """I have a topic that contains the following documents:- Traditional diets in most cultures were primarily plant-based with a little meat on top, but with the rise of industrial style meat production and factory farming, meat has become a staple food.- Meat, but especially beef, is the word food in terms of emissions.- Eating meat doesn't make you a bad person, not eating meat doesn't make you a good one.The topic is described by the following keywords: 'meat, beef, eat, eating, emissions, steak, food, health, processed, chicken'.Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.2 unmodified linesMAIN_PROMPT = """[INST]I have a topic that contains the following documents:[DOCUMENTS]The topic is described by the following keywords: '[KEYWORDS]'.Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.[/INST]3 unmodified linesSPACE_REPO_CARD_CONTENT = """---title: {dataset_id} topic modelingsdk: staticpinned: falsedatasets:4 unmodified lines5678910112 unmodified lines141516171819203 unmodified lines242526272829304 unmodified lines"""EXAMPLE_PROMPT = """I have a topic that is described by the following keywords: 'meat, beef, eat, eating, emissions, steak, food, health, processed, chicken'.Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.2 unmodified linesMAIN_PROMPT = """[INST]I have a topic that is described by the following keywords: '[KEYWORDS]'.Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.[/INST]3 unmodified linesSPACE_REPO_CARD_CONTENT = """---title: {dataset_id}sdk: staticpinned: falsedatasets:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import requestsimport duckdb
DATASET_VIEWER_API_URL = "https://datasets-server.huggingface.co/"session = requests.Session()
def fetch_json(url, params=None, timeout=20): response = session.get(url, params=params, timeout=timeout) response.raise_for_status() data = response.json() if "error" in data: raise Exception(f"Error fetching data: {data['error']}") return data
def get_split_rows(dataset, config, split): url = f"{DATASET_VIEWER_API_URL}/size" params = {"dataset": dataset, "config": config} config_size = fetch_json(url, params)
split_size = next( (s for s in config_size["size"]["splits"] if s["split"] == split), None ) if split_size is None: raise Exception(f"Error fetching split {split} in config {config}")
return split_size["num_rows"]
def get_parquet_urls(dataset, config, split): url = f"{DATASET_VIEWER_API_URL}/parquet" params = {"dataset": dataset, "config": config, "split": split} parquet_files = fetch_json(url, params)
parquet_urls = [file["url"] for file in parquet_files["parquet_files"]] return ",".join(f"'{url}'" for url in parquet_urls)
def get_docs_from_parquet(parquet_urls, column, offset, limit): sql_query = f"SELECT {column} FROM read_parquet([{parquet_urls}]) LIMIT {limit} OFFSET {offset};" df = duckdb.sql(sql_query).to_df() return df[column].tolist()
def get_info(dataset): url = f"{DATASET_VIEWER_API_URL}/info" params = {"dataset": dataset} info_resp = fetch_json(url, params)
return info_resp["dataset_info"].vscode/launch.jsonAdded+14
app.py+21-63
src/templates.py+3-11
src/viewer_api.pyAdded+51