Spaces:
Sleeping
Sleeping
Download app.py from Vishali-M/Data_Generator: direct link, hf CLI and curl.
- Browser
- Download file 2.31 kB
-
https://huggingface.co/spaces/Vishali-M/Data_Generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/Vishali-M/Data_Generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Vishali-M/Data_Generator/resolve/main/app.py
2.31 kB
| import gradio as gr | |
| import pandas as pd | |
| from faker import Faker | |
| import random | |
| import re | |
| fake = Faker() | |
| # Main function | |
| def generate_data(schema, rows): | |
| columns = re.findall(r'(\w+)\s+(INT|VARCHAR|FLOAT)', schema) | |
| data = {} | |
| for col_name, col_type in columns: | |
| col_name = col_name.lower() | |
| # Student ID | |
| if "id" in col_name: | |
| data[col_name] = [ | |
| random.randint(100, 999) | |
| for _ in range(rows) | |
| ] | |
| # Name | |
| elif "name" in col_name: | |
| data[col_name] = [ | |
| fake.name() | |
| for _ in range(rows) | |
| ] | |
| # Age | |
| elif "age" in col_name: | |
| data[col_name] = [ | |
| random.randint(18, 30) | |
| for _ in range(rows) | |
| ] | |
| # Department | |
| elif "department" in col_name: | |
| departments = [ | |
| "CSE", | |
| "AI", | |
| "ECE", | |
| "IT", | |
| "MECH" | |
| ] | |
| data[col_name] = [ | |
| random.choice(departments) | |
| for _ in range(rows) | |
| ] | |
| # CGPA | |
| elif "cgpa" in col_name: | |
| data[col_name] = [ | |
| round(random.uniform(6.0, 9.9), 2) | |
| for _ in range(rows) | |
| ] | |
| else: | |
| # Generic INT | |
| if col_type == "INT": | |
| data[col_name] = [ | |
| random.randint(1, 100) | |
| for _ in range(rows) | |
| ] | |
| # Generic FLOAT | |
| elif col_type == "FLOAT": | |
| data[col_name] = [ | |
| round(random.uniform(1, 100), 2) | |
| for _ in range(rows) | |
| ] | |
| # Generic VARCHAR | |
| else: | |
| data[col_name] = [ | |
| fake.word() | |
| for _ in range(rows) | |
| ] | |
| df = pd.DataFrame(data) | |
| return df | |
| # Gradio App | |
| demo = gr.Interface( | |
| fn=generate_data, | |
| inputs=[ | |
| gr.Textbox( | |
| lines=10, | |
| label="Enter SQL Schema" | |
| ), | |
| gr.Number( | |
| label="Rows", | |
| value=10 | |
| ) | |
| ], | |
| outputs=gr.Dataframe(), | |
| title="Synthetic Data Generator" | |
| ) | |
| demo.launch() |