Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,71 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
from torch.utils.data import Dataset, DataLoader
|
| 6 |
-
import torch.nn as nn
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# π§ Intelligent dataset loader
|
| 10 |
-
# ============================================================
|
| 11 |
-
from datasets import load_dataset, DatasetDict
|
| 12 |
-
|
| 13 |
-
def load_dataset_intelligent(source: str, subset: str = None):
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
- Local CSV/parquet file
|
| 18 |
-
- Local folder containing CSVs
|
| 19 |
-
- Hugging Face Hub dataset repo
|
| 20 |
-
Returns dict of {split: DataFrame}
|
| 21 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
print(f"
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
csv_files = [f for f in os.listdir(path) if f.endswith((".csv", ".parquet"))]
|
| 32 |
-
if csv_files:
|
| 33 |
-
print(f"π Found folder with {len(csv_files)} data files in: {path}")
|
| 34 |
-
dataframes = {}
|
| 35 |
-
for file in csv_files:
|
| 36 |
-
split_name = "train" if "train" in file else os.path.splitext(file)[0]
|
| 37 |
-
fpath = os.path.join(path, file)
|
| 38 |
-
df = pd.read_parquet(fpath) if fpath.endswith(".parquet") else pd.read_csv(fpath)
|
| 39 |
-
dataframes[split_name] = df
|
| 40 |
-
return dataframes
|
| 41 |
-
return None
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return {"train": df}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if isinstance(ds, DatasetDict):
|
| 58 |
-
print(f"β
Loaded HF dataset with splits: {list(ds.keys())}")
|
| 59 |
-
return {split: ds[split].to_pandas() for split in ds.keys()}
|
| 60 |
-
else:
|
| 61 |
-
print("β
Loaded single-split HF dataset")
|
| 62 |
-
return {"train": ds.to_pandas()}
|
| 63 |
-
except Exception as e:
|
| 64 |
-
raise FileNotFoundError(f"β Could not load dataset: {source}\nError: {str(e)}")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
self.max_frames = max_frames
|
| 73 |
-
self.data_splits = load_dataset_intelligent(source)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
self.df = self.data_splits.get("train") or list(self.data_splits.values())[0]
|
| 77 |
-
self.root = Path(source) if os.path.isdir(source) else None
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
T.ToPILImage(), T.Resize((512,512)),
|
| 82 |
-
T.ToTensor(), T.Normalize([0.5]*3, [0.5]*3)
|
| 83 |
-
])
|
| 84 |
-
self.video_tf = T.Compose([
|
| 85 |
-
T.ToPILImage(), T.Resize((128,256)),
|
| 86 |
-
T.ToTensor(), T.Normalize([0.5]*3, [0.5]*3)
|
| 87 |
-
])
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
import torchvision
|
| 93 |
-
rec = self.df.iloc[i]
|
| 94 |
-
fname = rec.get("file_name") or rec.get("image") or rec.get("path")
|
| 95 |
-
text = rec.get("text") or rec.get("caption") or rec.get("prompt")
|
| 96 |
-
p = Path(self.root / fname) if self.root else Path(fname)
|
| 97 |
-
if not p.exists():
|
| 98 |
-
raise FileNotFoundError(f"Missing file: {p}")
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
elif p.suffix.lower() in {".mp4",".mov",".avi",".mkv"}:
|
| 105 |
-
vid,_,_ = torchvision.io.read_video(str(p))
|
| 106 |
-
total = len(vid)
|
| 107 |
-
if total == 0:
|
| 108 |
-
return {"type":"video","frames":torch.zeros((self.max_frames,3,128,256))}
|
| 109 |
-
idxs = np.linspace(0,total-1,self.max_frames).round().astype(int)
|
| 110 |
-
frames = torch.stack([self.video_tf(vid[j].numpy()) for j in idxs])
|
| 111 |
-
return {"type":"video","frames":frames,"caption":text}
|
| 112 |
-
else:
|
| 113 |
-
raise RuntimeError(f"Unsupported media: {p}")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def load_data(source_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
+
Load dataset from either a local CSV file or a Hugging Face dataset path.
|
| 9 |
+
Automatically detects which type of source to use.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
+
try:
|
| 12 |
+
# --- Case 1: Local CSV file ---
|
| 13 |
+
if os.path.exists(source_path):
|
| 14 |
+
print(f"π Loading local dataset from: {source_path}")
|
| 15 |
+
df = pd.read_csv(source_path)
|
| 16 |
+
print(f"β
Loaded {len(df)} rows from local CSV.")
|
| 17 |
+
return df
|
| 18 |
|
| 19 |
+
# --- Case 2: Hugging Face dataset ---
|
| 20 |
+
elif "/" in source_path:
|
| 21 |
+
print(f"π Loading Hugging Face dataset: {source_path}")
|
| 22 |
+
dataset = load_dataset(source_path, split="train")
|
| 23 |
+
df = dataset.to_pandas()
|
| 24 |
+
print(f"β
Loaded {len(df)} rows from Hugging Face dataset.")
|
| 25 |
+
return df
|
| 26 |
|
| 27 |
+
else:
|
| 28 |
+
raise FileNotFoundError("Invalid path: not a local file or HF dataset.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"β Error loading data: {e}")
|
| 32 |
+
return pd.DataFrame()
|
|
|
|
| 33 |
|
| 34 |
+
def summarize_dataset(df):
|
| 35 |
+
"""
|
| 36 |
+
Return a brief summary of the dataset for display in Gradio.
|
| 37 |
+
"""
|
| 38 |
+
if df.empty:
|
| 39 |
+
return "β No data loaded.", ""
|
| 40 |
+
|
| 41 |
+
preview = df.head().to_markdown(index=False)
|
| 42 |
+
info = f"β
Loaded {len(df)} rows and {len(df.columns)} columns.\n\n**Columns:** {', '.join(df.columns)}"
|
| 43 |
+
return info, preview
|
| 44 |
|
| 45 |
+
def gradio_ui():
|
| 46 |
+
with gr.Blocks(title="Prompt Enhancer Data Loader") as demo:
|
| 47 |
+
gr.Markdown("## π§ Intelligent Dataset Loader")
|
| 48 |
+
gr.Markdown("Automatically loads from a local CSV file **or** a Hugging Face dataset repo.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
with gr.Row():
|
| 51 |
+
dataset_path = gr.Textbox(
|
| 52 |
+
label="Enter dataset path (local or HF repo)",
|
| 53 |
+
value="rahul7star/prompt-enhancer-dataset-01",
|
| 54 |
+
placeholder="e.g., /path/to/local.csv or username/dataset-name",
|
| 55 |
+
)
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
load_btn = gr.Button("π Load Dataset")
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
output_info = gr.Markdown()
|
| 60 |
+
output_preview = gr.Markdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
def handle_load(path):
|
| 63 |
+
df = load_data(path)
|
| 64 |
+
return summarize_dataset(df)
|
| 65 |
|
| 66 |
+
load_btn.click(handle_load, inputs=[dataset_path], outputs=[output_info, output_preview])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
return demo
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
gradio_ui().launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|