Spaces:
Runtime error
Runtime error
Aryan Mishra commited on
Commit ·
7fe1de1
1
Parent(s): d9f3e06
feat: Phase 1 - project scaffold and data download
Browse files- notebooks/01_data_exploration.ipynb +14 -19
- requirements.txt +2 -2
- scripts/download_data.py +41 -101
notebooks/01_data_exploration.ipynb
CHANGED
|
@@ -4,16 +4,7 @@
|
|
| 4 |
"cell_type": "markdown",
|
| 5 |
"metadata": {},
|
| 6 |
"source": [
|
| 7 |
-
"#
|
| 8 |
-
"Exploring the SemEval 2014 ABSA and Amazon Hindi datasets."
|
| 9 |
-
]
|
| 10 |
-
},
|
| 11 |
-
{
|
| 12 |
-
"cell_type": "markdown",
|
| 13 |
-
"metadata": {},
|
| 14 |
-
"source": [
|
| 15 |
-
"## Setup\n",
|
| 16 |
-
"Imports, config, and dataset loading."
|
| 17 |
]
|
| 18 |
},
|
| 19 |
{
|
|
@@ -27,8 +18,7 @@
|
|
| 27 |
"cell_type": "markdown",
|
| 28 |
"metadata": {},
|
| 29 |
"source": [
|
| 30 |
-
"#
|
| 31 |
-
"Load SemEval restaurants, laptops, and Amazon Hindi datasets."
|
| 32 |
]
|
| 33 |
},
|
| 34 |
{
|
|
@@ -42,8 +32,7 @@
|
|
| 42 |
"cell_type": "markdown",
|
| 43 |
"metadata": {},
|
| 44 |
"source": [
|
| 45 |
-
"#
|
| 46 |
-
"Plot language distribution across datasets."
|
| 47 |
]
|
| 48 |
},
|
| 49 |
{
|
|
@@ -57,8 +46,7 @@
|
|
| 57 |
"cell_type": "markdown",
|
| 58 |
"metadata": {},
|
| 59 |
"source": [
|
| 60 |
-
"#
|
| 61 |
-
"Display random samples from each dataset."
|
| 62 |
]
|
| 63 |
},
|
| 64 |
{
|
|
@@ -72,8 +60,7 @@
|
|
| 72 |
"cell_type": "markdown",
|
| 73 |
"metadata": {},
|
| 74 |
"source": [
|
| 75 |
-
"#
|
| 76 |
-
"Analyze aspect term frequency, sentiment distribution, and review lengths."
|
| 77 |
]
|
| 78 |
},
|
| 79 |
{
|
|
@@ -91,10 +78,18 @@
|
|
| 91 |
"name": "python3"
|
| 92 |
},
|
| 93 |
"language_info": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
"name": "python",
|
|
|
|
|
|
|
| 95 |
"version": "3.11.0"
|
| 96 |
}
|
| 97 |
},
|
| 98 |
"nbformat": 4,
|
| 99 |
-
"nbformat_minor":
|
| 100 |
}
|
|
|
|
| 4 |
"cell_type": "markdown",
|
| 5 |
"metadata": {},
|
| 6 |
"source": [
|
| 7 |
+
"# Setup"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
]
|
| 9 |
},
|
| 10 |
{
|
|
|
|
| 18 |
"cell_type": "markdown",
|
| 19 |
"metadata": {},
|
| 20 |
"source": [
|
| 21 |
+
"# Load Data"
|
|
|
|
| 22 |
]
|
| 23 |
},
|
| 24 |
{
|
|
|
|
| 32 |
"cell_type": "markdown",
|
| 33 |
"metadata": {},
|
| 34 |
"source": [
|
| 35 |
+
"# Language Distribution"
|
|
|
|
| 36 |
]
|
| 37 |
},
|
| 38 |
{
|
|
|
|
| 46 |
"cell_type": "markdown",
|
| 47 |
"metadata": {},
|
| 48 |
"source": [
|
| 49 |
+
"# Sample Reviews"
|
|
|
|
| 50 |
]
|
| 51 |
},
|
| 52 |
{
|
|
|
|
| 60 |
"cell_type": "markdown",
|
| 61 |
"metadata": {},
|
| 62 |
"source": [
|
| 63 |
+
"# Aspect Term Analysis"
|
|
|
|
| 64 |
]
|
| 65 |
},
|
| 66 |
{
|
|
|
|
| 78 |
"name": "python3"
|
| 79 |
},
|
| 80 |
"language_info": {
|
| 81 |
+
"codemirror_mode": {
|
| 82 |
+
"name": "ipython",
|
| 83 |
+
"version": 3
|
| 84 |
+
},
|
| 85 |
+
"file_extension": ".py",
|
| 86 |
+
"mimetype": "text/x-python",
|
| 87 |
"name": "python",
|
| 88 |
+
"nbconvert_exporter": "python",
|
| 89 |
+
"pygments_lexer": "ipython3",
|
| 90 |
"version": "3.11.0"
|
| 91 |
}
|
| 92 |
},
|
| 93 |
"nbformat": 4,
|
| 94 |
+
"nbformat_minor": 2
|
| 95 |
}
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
transformers==4.
|
| 2 |
datasets==2.19.0
|
| 3 |
torch==2.3.0
|
| 4 |
onnxruntime==1.18.0
|
|
@@ -12,7 +12,7 @@ pandas==2.2.2
|
|
| 12 |
numpy==1.26.4
|
| 13 |
seqeval==1.2.2
|
| 14 |
mlflow==2.13.0
|
| 15 |
-
dvc==3.
|
| 16 |
evidently==0.4.30
|
| 17 |
fastapi==0.111.0
|
| 18 |
uvicorn==0.29.0
|
|
|
|
| 1 |
+
transformers==4.40.0
|
| 2 |
datasets==2.19.0
|
| 3 |
torch==2.3.0
|
| 4 |
onnxruntime==1.18.0
|
|
|
|
| 12 |
numpy==1.26.4
|
| 13 |
seqeval==1.2.2
|
| 14 |
mlflow==2.13.0
|
| 15 |
+
dvc==3.51.1
|
| 16 |
evidently==0.4.30
|
| 17 |
fastapi==0.111.0
|
| 18 |
uvicorn==0.29.0
|
scripts/download_data.py
CHANGED
|
@@ -1,105 +1,45 @@
|
|
|
|
|
| 1 |
import urllib.request
|
| 2 |
-
from pathlib import Path
|
| 3 |
-
import json
|
| 4 |
-
import pandas as pd
|
| 5 |
from datasets import load_dataset
|
| 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 |
-
def download_semeval_restaurants() -> dict:
|
| 43 |
-
dest = RAW_DIR / "semeval_restaurants"
|
| 44 |
-
dest.mkdir(parents=True, exist_ok=True)
|
| 45 |
-
print(" Loading SemEval 2014 Restaurants...")
|
| 46 |
-
dataset = load_dataset("jakartaresearch/semeval-absa", "restaurant")
|
| 47 |
-
counts = {}
|
| 48 |
-
for split in dataset:
|
| 49 |
-
path = dest / f"{split}.jsonl"
|
| 50 |
-
dataset[split].to_json(path)
|
| 51 |
-
counts[split] = len(dataset[split])
|
| 52 |
-
print(f" ✓ {split}: {len(dataset[split])} samples -> {path}")
|
| 53 |
-
return counts
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def download_amazon_hindi() -> dict:
|
| 57 |
-
dest = RAW_DIR / "amazon_hindi"
|
| 58 |
-
dest.mkdir(parents=True, exist_ok=True)
|
| 59 |
-
print(" Loading Hindi Amazon reviews...")
|
| 60 |
-
|
| 61 |
-
base_url = "https://raw.githubusercontent.com/Udrasht/Hindi-Sentiment-Analysis-Corpus-from-Amazon-Reviews/main/data"
|
| 62 |
-
|
| 63 |
-
for split, fname in [("train", "train.xlsx"), ("test", "test.xlsx")]:
|
| 64 |
-
url = f"{base_url}/{fname}"
|
| 65 |
-
print(f" Downloading {split} from {url}...")
|
| 66 |
-
df = pd.read_excel(url)
|
| 67 |
-
records = []
|
| 68 |
-
for _, row in df.iterrows():
|
| 69 |
-
records.append({
|
| 70 |
-
"text": row["content_hindi"],
|
| 71 |
-
"title": row["title_hindi"],
|
| 72 |
-
"rating": int(row["rating"]),
|
| 73 |
-
"label": row["labels"],
|
| 74 |
-
})
|
| 75 |
-
path = dest / f"{split}.jsonl"
|
| 76 |
-
with open(path, "w", encoding="utf-8") as f:
|
| 77 |
-
for r in records:
|
| 78 |
-
f.write(json.dumps(r, ensure_ascii=False) + "\n")
|
| 79 |
-
print(f" ✓ {split}: {len(records)} samples -> {path}")
|
| 80 |
-
|
| 81 |
-
return {"train": 3527, "test": 884}
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def main():
|
| 85 |
-
results = {}
|
| 86 |
-
print("\n=== Downloading fasttext LID model ===")
|
| 87 |
-
results["fasttext"] = download_fasttext()
|
| 88 |
-
|
| 89 |
-
print("\n=== Downloading SemEval 2014 Laptops ===")
|
| 90 |
-
results["semeval_laptops"] = download_semeval_laptops()
|
| 91 |
-
|
| 92 |
-
print("\n=== Downloading SemEval 2014 Restaurants ===")
|
| 93 |
-
results["semeval_restaurants"] = download_semeval_restaurants()
|
| 94 |
-
|
| 95 |
-
print("\n=== Downloading Hindi Amazon reviews ===")
|
| 96 |
-
results["amazon_hindi"] = download_amazon_hindi()
|
| 97 |
-
|
| 98 |
-
print("\n" + "=" * 50)
|
| 99 |
-
print("DOWNLOAD SUMMARY")
|
| 100 |
-
print("=" * 50)
|
| 101 |
-
print(json.dumps(results, indent=2))
|
| 102 |
-
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import urllib.request
|
|
|
|
|
|
|
|
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
+
from src.config import DATA_DIR, RAW_DIR, FASTTEXT_MODEL_PATH
|
| 5 |
+
|
| 6 |
+
def download_fasttext():
|
| 7 |
+
url = "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.ftz"
|
| 8 |
+
os.makedirs(FASTTEXT_MODEL_PATH.parent, exist_ok=True)
|
| 9 |
+
if not FASTTEXT_MODEL_PATH.exists():
|
| 10 |
+
print("Downloading fastText LID model...")
|
| 11 |
+
urllib.request.urlretrieve(url, FASTTEXT_MODEL_PATH)
|
| 12 |
+
else:
|
| 13 |
+
print("fastText LID model already exists.")
|
| 14 |
+
print(f"fastText model size: {os.path.getsize(FASTTEXT_MODEL_PATH) / 1024 / 1024:.2f} MB")
|
| 15 |
+
|
| 16 |
+
def download_semeval():
|
| 17 |
+
print("Downloading SemEval datasets...")
|
| 18 |
+
restaurants = load_dataset("tomaarsen/absa-semeval-2014-restaurants")
|
| 19 |
+
laptops = load_dataset("tomaarsen/absa-semeval-2014-laptops")
|
| 20 |
+
|
| 21 |
+
rest_path = RAW_DIR / "semeval_restaurants"
|
| 22 |
+
lap_path = RAW_DIR / "semeval_laptops"
|
| 23 |
+
|
| 24 |
+
restaurants.save_to_disk(str(rest_path))
|
| 25 |
+
laptops.save_to_disk(str(lap_path))
|
| 26 |
+
|
| 27 |
+
print(f"SemEval Restaurants train samples: {len(restaurants['train'])}")
|
| 28 |
+
print(f"SemEval Laptops train samples: {len(laptops['train'])}")
|
| 29 |
+
|
| 30 |
+
def download_amazon_hindi():
|
| 31 |
+
print("Downloading Amazon Hindi dataset...")
|
| 32 |
+
# Load just 5000 from train
|
| 33 |
+
amz_hi = load_dataset("amazon_reviews_multi", "hi", split="train[:5000]")
|
| 34 |
+
|
| 35 |
+
amz_path = RAW_DIR / "amazon_hindi"
|
| 36 |
+
amz_hi.save_to_disk(str(amz_path))
|
| 37 |
+
|
| 38 |
+
print(f"Amazon Hindi train samples: {len(amz_hi)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
+
os.makedirs(RAW_DIR, exist_ok=True)
|
| 42 |
+
download_fasttext()
|
| 43 |
+
download_semeval()
|
| 44 |
+
download_amazon_hindi()
|
| 45 |
+
print("Download complete.")
|