Anupam202224 commited on
Commit
b780c60
·
verified ·
1 Parent(s): 79243ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py CHANGED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import time
5
+
6
+ # Mock user database for authentication (in production, this should be more secure)
7
+ users_db = {"admin": "password"}
8
+
9
+ # In-memory pipelines and logs
10
+ data_pipelines = {}
11
+ logs = []
12
+
13
+ def log_event(event):
14
+ timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
15
+ logs.append(f"[{timestamp}] {event}")
16
+
17
+ # User Authentication function
18
+ def authenticate(username, password):
19
+ if username in users_db and users_db[username] == password:
20
+ log_event(f"User {username} logged in.")
21
+ return True, f"Welcome, {username}!"
22
+ else:
23
+ return False, "Invalid username or password."
24
+
25
+ # Data ingestion function
26
+ def data_ingestion(data_file):
27
+ try:
28
+ df = pd.read_csv(data_file) # Handle file uploads better in Hugging Face Spaces
29
+ data_pipelines["data_ingestion"] = df
30
+ log_event("Data ingestion completed successfully.")
31
+ return df.head()
32
+ except Exception as e:
33
+ log_event(f"Data ingestion failed: {str(e)}")
34
+ return f"Error: {str(e)}"
35
+
36
+ # Data transformation function (basic cleaning)
37
+ def data_transformation(clean_duplicates=True, fill_missing=None):
38
+ if "data_ingestion" not in data_pipelines:
39
+ return "No data ingested yet."
40
+
41
+ df = data_pipelines["data_ingestion"].copy()
42
+
43
+ if clean_duplicates:
44
+ df = df.drop_duplicates()
45
+ if fill_missing is not None:
46
+ df = df.fillna(fill_missing)
47
+
48
+ data_pipelines["data_transformation"] = df
49
+ log_event("Data transformation completed successfully.")
50
+
51
+ return df.head()
52
+
53
+ # Data orchestration: sequential task execution
54
+ def run_pipeline():
55
+ if "data_ingestion" in data_pipelines and "data_transformation" in data_pipelines:
56
+ log_event("Data pipeline executed successfully.")
57
+ return "Pipeline executed successfully."
58
+ else:
59
+ log_event("Pipeline execution failed.")
60
+ return "Pipeline execution failed. Please ensure all steps are completed."
61
+
62
+ # Data monitoring and logs
63
+ def monitor_pipeline():
64
+ return logs
65
+
66
+ # Data visualization (displaying the current state of data)
67
+ def visualize_data():
68
+ if "data_transformation" in data_pipelines:
69
+ return data_pipelines["data_transformation"].head()
70
+ else:
71
+ return "No transformed data available."
72
+
73
+ # Gradio interface setup
74
+ def main():
75
+ # Launch Gradio Interface
76
+ with gr.Blocks() as demo:
77
+ # Authentication
78
+ with gr.Row():
79
+ username_input = gr.Textbox(label="Username")
80
+ password_input = gr.Textbox(label="Password", type="password")
81
+ auth_button = gr.Button("Log In")
82
+ auth_output = gr.Textbox(label="Login Message")
83
+ auth_button.click(authenticate, inputs=[username_input, password_input], outputs=auth_output)
84
+
85
+ # Ingestion section
86
+ with gr.Row():
87
+ ingest_button = gr.File(label="Upload CSV Data")
88
+ ingest_submit_button = gr.Button("Submit Data")
89
+ ingest_output = gr.Dataframe(label="Ingested Data")
90
+ ingest_submit_button.click(data_ingestion, inputs=ingest_button, outputs=ingest_output)
91
+
92
+ # Transformation section
93
+ with gr.Row():
94
+ clean_duplicates = gr.Checkbox(label="Remove Duplicates", value=True)
95
+ fill_missing = gr.Textbox(label="Fill Missing Values (leave empty for no filling)")
96
+ transform_button = gr.Button("Transform Data")
97
+ transform_output = gr.Dataframe(label="Transformed Data")
98
+ transform_button.click(data_transformation, inputs=[clean_duplicates, fill_missing], outputs=transform_output)
99
+
100
+ # Orchestration section
101
+ with gr.Row():
102
+ pipeline_button = gr.Button("Run Pipeline")
103
+ pipeline_output = gr.Textbox(label="Pipeline Status")
104
+ pipeline_button.click(run_pipeline, outputs=pipeline_output)
105
+
106
+ # Monitoring section
107
+ with gr.Row():
108
+ monitor_button = gr.Button("Monitor Pipeline")
109
+ monitor_output = gr.Textbox(label="Logs")
110
+ monitor_button.click(monitor_pipeline, outputs=monitor_output)
111
+
112
+ # Visualization section
113
+ with gr.Row():
114
+ visualize_button = gr.Button("Visualize Data")
115
+ visualize_output = gr.Dataframe(label="Data Preview")
116
+ visualize_button.click(visualize_data, outputs=visualize_output)
117
+
118
+ demo.launch()
119
+
120
+ # Launch the app
121
+ if __name__ == "__main__":
122
+ main()