Spaces:
Sleeping
Sleeping
Download preprocess.py from akshat1409/python-qa-assistant: direct link, hf CLI and curl.
- Browser
- Download file 4.94 kB
-
https://huggingface.co/spaces/akshat1409/python-qa-assistant/resolve/main/preprocess.py
- Command line
-
hf download hf://spaces/akshat1409/python-qa-assistant/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/spaces/akshat1409/python-qa-assistant/resolve/main/preprocess.py
4.94 kB
| """ | |
| preprocess.py | |
| ------------- | |
| Step 1: Filter Python-tagged questions, join best answers, clean HTML. | |
| Output: data/clean_qa.parquet (~15K top-scored Python Q&A pairs) | |
| Run once locally before ingesting: | |
| python preprocess.py | |
| """ | |
| import pandas as pd | |
| from bs4 import BeautifulSoup | |
| import re | |
| import os | |
| DATASET_PATH = "dataset/archive" | |
| OUTPUT_PATH = "data/clean_qa.parquet" | |
| TOP_N = 15000 # Keep top N questions by score (keeps file sizes manageable) | |
| # βββ 1. Load Tags β get Python question IDs ββββββββββββββββββββββββββββββββββ | |
| print("π Loading Tags.csv...") | |
| tags = pd.read_csv(f"{DATASET_PATH}/Tags.csv", encoding="latin-1") | |
| python_ids = set(tags[tags["Tag"] == "python"]["Id"].tolist()) | |
| print(f" β {len(python_ids):,} Python-tagged question IDs found") | |
| # βββ 2. Load Questions in chunks (file is ~888MB) ββββββββββββββββββββββββββββ | |
| print("\nπ Loading Questions.csv (chunked, this may take a minute)...") | |
| q_chunks = [] | |
| for chunk in pd.read_csv( | |
| f"{DATASET_PATH}/Questions.csv", | |
| encoding="latin-1", | |
| chunksize=50_000, | |
| on_bad_lines="skip", | |
| ): | |
| filtered = chunk[chunk["Id"].isin(python_ids)] | |
| if not filtered.empty: | |
| q_chunks.append(filtered) | |
| questions = pd.concat(q_chunks, ignore_index=True) | |
| print(f" β {len(questions):,} Python questions loaded") | |
| # Keep only top N by vote score | |
| questions = questions.sort_values("Score", ascending=False).head(TOP_N) | |
| print(f" β Trimmed to top {len(questions):,} by score") | |
| # βββ 3. Load Answers for only those questions ββββββββββββββββββββββββββββββββ | |
| print("\nπ Loading Answers.csv (chunked, this may take a minute)...") | |
| target_ids = set(questions["Id"].tolist()) | |
| a_chunks = [] | |
| for chunk in pd.read_csv( | |
| f"{DATASET_PATH}/Answers.csv", | |
| encoding="latin-1", | |
| chunksize=50_000, | |
| on_bad_lines="skip", | |
| ): | |
| filtered = chunk[chunk["ParentId"].isin(target_ids)] | |
| if not filtered.empty: | |
| a_chunks.append(filtered) | |
| answers = pd.concat(a_chunks, ignore_index=True) | |
| print(f" β {len(answers):,} answers loaded") | |
| # βββ 4. Pick best answer per question (highest score) ββββββββββββββββββββββββ | |
| best_answers = ( | |
| answers.sort_values("Score", ascending=False) | |
| .groupby("ParentId", as_index=False) | |
| .first() | |
| ) | |
| best_answers = best_answers.rename( | |
| columns={"ParentId": "QuestionId", "Body": "AnswerBody", "Score": "AnswerScore"} | |
| ) | |
| print(f" β {len(best_answers):,} best answers selected") | |
| # βββ 5. Clean HTML ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def clean_html(html: str) -> str: | |
| """Strip HTML tags, preserve code blocks with backtick markers.""" | |
| if pd.isna(html): | |
| return "" | |
| soup = BeautifulSoup(str(html), "html.parser") | |
| # Mark code blocks so they stay readable | |
| for code in soup.find_all("code"): | |
| code.string = f"\n```\n{code.get_text()}\n```\n" | |
| text = soup.get_text(separator=" ") | |
| text = re.sub(r"\s+", " ", text).strip() | |
| return text | |
| print("\nπ§Ή Cleaning HTML bodies...") | |
| questions["CleanBody"] = questions["Body"].apply(clean_html) | |
| best_answers["CleanAnswer"] = best_answers["AnswerBody"].apply(clean_html) | |
| # βββ 6. Merge questions + best answers βββββββββββββββββββββββββββββββββββββββ | |
| merged = questions.merge( | |
| best_answers[["QuestionId", "CleanAnswer", "AnswerScore"]], | |
| left_on="Id", | |
| right_on="QuestionId", | |
| how="inner", | |
| ) | |
| merged = merged[[ | |
| "Id", "Title", "CleanBody", "Score", "CleanAnswer", "AnswerScore" | |
| ]].copy() | |
| merged.columns = [ | |
| "question_id", "title", "question_body", "question_score", | |
| "answer_body", "answer_score" | |
| ] | |
| # βββ 7. Basic quality filters βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| merged = merged[merged["answer_body"].str.len() > 50] | |
| merged = merged[merged["question_body"].str.len() > 20] | |
| merged = merged[merged["title"].str.len() > 5] | |
| merged = merged.drop_duplicates(subset="question_id") | |
| print(f"\nβ Final dataset: {len(merged):,} Q&A pairs") | |
| # βββ 8. Save ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| os.makedirs("data", exist_ok=True) | |
| merged.to_parquet(OUTPUT_PATH, index=False) | |
| print(f"πΎ Saved β {OUTPUT_PATH}") | |
| print("\nSample row:") | |
| print(merged.iloc[0][["title", "question_body", "answer_body"]].to_string()) | |