espejelomar's picture
feat: add description
a74e652
import pandas as pd
import re
import gradio as gr
# Constants
CORE_STARS_CSV_PATH = "core_stars.csv"
OUTPUT_CORE_STARS_TXT_PATH = "core_stars.txt"
APP_DESCRIPTION = """
With love by @espejelomar!
Upload a CSV file, pinpointing the column with users' Telegram IDs. The program then reviews this data and produces a new CSV file. In this file, a `1` in the `is_in_core_stars` column confirms that the person is a member of Core Stars.
It is 100% certain that if an individual appears in your Excel file and is in the Core Stars group then they will have a 1 in the `is_in_core_stars` column, however there is a low probability (less than 5%) that they will have a 0 and I know part of Core Stars. This happens because the person incorrectly typed their Telegram ID in the form. For example, these are cases that have already happened:
1. They wrote O instead of 0.
2. They changed their Telegram ID after filling out the form.
"""
# Normalize Telegram IDs
def normalize_id(telegram_id):
telegram_id = str(telegram_id).lower().strip()
if telegram_id.startswith("https://t.me/"):
telegram_id = telegram_id.replace("https://t.me/", "")
telegram_id = telegram_id.lstrip("@")
telegram_id = re.sub(r"[^a-zA-Z0-9_]", "", telegram_id)
return telegram_id
# Create core_stars.txt
def create_core_stars_txt(csv_path, output_path, column_name="Telegram Handle"):
df = pd.read_csv(csv_path)
normalized_ids = df[column_name].apply(normalize_id).unique()
with open(output_path, "w") as file:
for id in normalized_ids:
file.write(id + "\n")
# Read core_stars_ids
def read_core_stars_ids(file_path):
with open(file_path, "r") as file:
return [normalize_id(line) for line in file]
# Process uploaded CSV and return updated CSV
def process_csv(uploaded_csv, column_name):
# create_core_stars_txt(CORE_STARS_CSV_PATH, OUTPUT_CORE_STARS_TXT_PATH)
core_stars_ids = read_core_stars_ids(OUTPUT_CORE_STARS_TXT_PATH)
df = pd.read_csv(uploaded_csv)
df[column_name] = df[column_name].apply(normalize_id)
df["is_in_core_stars"] = df[column_name].apply(
lambda id: 1 if id in core_stars_ids else 0
)
cols = ["is_in_core_stars"] + [
col for col in df.columns if col != "is_in_core_stars"
]
df = df[cols]
# Save to a temporary file and return
output_file = "updated_file.csv"
df.to_csv(output_file, index=False)
return output_file
# Set up Gradio interface
iface = gr.Interface(
fn=process_csv,
inputs=[
gr.File(label="Upload CSV File"),
gr.Textbox(label="Enter Column Name for Telegram ID")
],
outputs=gr.File(label="Download Updated CSV"),
title="Telegram Core Stars Membership Checker",
description=APP_DESCRIPTION
)
iface.launch()
if __name__ == "__main__":
iface.launch()