Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from faker import Faker
|
| 4 |
+
import random
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
fake = Faker()
|
| 8 |
+
|
| 9 |
+
# Main function
|
| 10 |
+
def generate_data(schema, rows):
|
| 11 |
+
|
| 12 |
+
columns = re.findall(r'(\w+)\s+(INT|VARCHAR|FLOAT)', schema)
|
| 13 |
+
|
| 14 |
+
data = {}
|
| 15 |
+
|
| 16 |
+
for col_name, col_type in columns:
|
| 17 |
+
|
| 18 |
+
col_name = col_name.lower()
|
| 19 |
+
|
| 20 |
+
# Student ID
|
| 21 |
+
if "id" in col_name:
|
| 22 |
+
data[col_name] = [
|
| 23 |
+
random.randint(100, 999)
|
| 24 |
+
for _ in range(rows)
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
# Name
|
| 28 |
+
elif "name" in col_name:
|
| 29 |
+
data[col_name] = [
|
| 30 |
+
fake.name()
|
| 31 |
+
for _ in range(rows)
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Age
|
| 35 |
+
elif "age" in col_name:
|
| 36 |
+
data[col_name] = [
|
| 37 |
+
random.randint(18, 30)
|
| 38 |
+
for _ in range(rows)
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# Department
|
| 42 |
+
elif "department" in col_name:
|
| 43 |
+
departments = [
|
| 44 |
+
"CSE",
|
| 45 |
+
"AI",
|
| 46 |
+
"ECE",
|
| 47 |
+
"IT",
|
| 48 |
+
"MECH"
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
data[col_name] = [
|
| 52 |
+
random.choice(departments)
|
| 53 |
+
for _ in range(rows)
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
# CGPA
|
| 57 |
+
elif "cgpa" in col_name:
|
| 58 |
+
data[col_name] = [
|
| 59 |
+
round(random.uniform(6.0, 9.9), 2)
|
| 60 |
+
for _ in range(rows)
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
else:
|
| 64 |
+
|
| 65 |
+
# Generic INT
|
| 66 |
+
if col_type == "INT":
|
| 67 |
+
data[col_name] = [
|
| 68 |
+
random.randint(1, 100)
|
| 69 |
+
for _ in range(rows)
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
# Generic FLOAT
|
| 73 |
+
elif col_type == "FLOAT":
|
| 74 |
+
data[col_name] = [
|
| 75 |
+
round(random.uniform(1, 100), 2)
|
| 76 |
+
for _ in range(rows)
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
# Generic VARCHAR
|
| 80 |
+
else:
|
| 81 |
+
data[col_name] = [
|
| 82 |
+
fake.word()
|
| 83 |
+
for _ in range(rows)
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
df = pd.DataFrame(data)
|
| 87 |
+
|
| 88 |
+
return df
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# Gradio App
|
| 92 |
+
demo = gr.Interface(
|
| 93 |
+
fn=generate_data,
|
| 94 |
+
inputs=[
|
| 95 |
+
gr.Textbox(
|
| 96 |
+
lines=10,
|
| 97 |
+
label="Enter SQL Schema"
|
| 98 |
+
),
|
| 99 |
+
gr.Number(
|
| 100 |
+
label="Rows",
|
| 101 |
+
value=10
|
| 102 |
+
)
|
| 103 |
+
],
|
| 104 |
+
outputs=gr.Dataframe(),
|
| 105 |
+
title="Synthetic Data Generator"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
demo.launch()
|