AndrewNanu-app commited on
Commit
808736f
β€’
1 Parent(s): 68363f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -0
app.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import plotly.graph_objects as go
4
+ import gradio as gr
5
+ from datasets import Dataset, DatasetDict
6
+ from huggingface_hub import HfApi, HfFolder
7
+
8
+ # Generate a synthetic dataset
9
+ def synthesize_dataset(file_path):
10
+ np.random.seed(42)
11
+ data = {
12
+ 'title': [f'Place {i}' for i in range(100)],
13
+ 'latitude': np.random.uniform(low=32.0, high=36.0, size=100),
14
+ 'longitude': np.random.uniform(low=-114.0, high=-110.0, size=100),
15
+ 'info': [f'Info {i}' for i in range(100)]
16
+ }
17
+ df = pd.DataFrame(data)
18
+ df.to_csv(file_path, index=False)
19
+ return df
20
+
21
+ class DataLoader:
22
+ """
23
+ Class to handle loading of datasets.
24
+ """
25
+
26
+ def __init__(self, file_path: str):
27
+ """
28
+ Initialize with the path to the dataset file.
29
+
30
+ Args:
31
+ file_path (str): Path to the CSV dataset file.
32
+ """
33
+ self.file_path = file_path
34
+
35
+ def load(self) -> pd.DataFrame:
36
+ """
37
+ Load the dataset from the CSV file.
38
+
39
+ Returns:
40
+ pd.DataFrame: Loaded dataset as a pandas DataFrame.
41
+ """
42
+ return pd.read_csv(self.file_path)
43
+
44
+ class MapVisualizer:
45
+ """
46
+ Class to handle creation of map visualizations.
47
+ """
48
+
49
+ def __init__(self, df: pd.DataFrame):
50
+ """
51
+ Initialize with a DataFrame.
52
+
53
+ Args:
54
+ df (pd.DataFrame): DataFrame containing the data to visualize.
55
+ """
56
+ self.df = df
57
+
58
+ def create(self, filter_keyword: str) -> go.Figure:
59
+ """
60
+ Create a map visualization filtered by a keyword.
61
+
62
+ Args:
63
+ filter_keyword (str): Keyword to filter the dataset by 'title' column.
64
+
65
+ Returns:
66
+ go.Figure: Plotly figure object with the map visualization.
67
+ """
68
+ filtered_df = self.df[self.df['title'].str.contains(filter_keyword, case=False, na=False)]
69
+
70
+ fig = go.Figure(go.Scattermapbox(
71
+ lat=filtered_df['latitude'].tolist(),
72
+ lon=filtered_df['longitude'].tolist(),
73
+ mode='markers',
74
+ marker=go.scattermapbox.Marker(size=6),
75
+ text=filtered_df['info'].tolist(),
76
+ hoverinfo="text",
77
+ hovertemplate='<b>Info</b>: %{text}<br><b>Latitude</b>: %{lat}<br><b>Longitude</b>: %{lon}'
78
+ ))
79
+
80
+ fig.update_layout(
81
+ mapbox_style="open-street-map",
82
+ hovermode='closest',
83
+ mapbox=dict(
84
+ bearing=0,
85
+ center=go.layout.mapbox.Center(
86
+ lat=filtered_df['latitude'].mean(),
87
+ lon=filtered_df['longitude'].mean()
88
+ ),
89
+ pitch=0,
90
+ zoom=8
91
+ ),
92
+ )
93
+
94
+ return fig
95
+
96
+ class GradioApp:
97
+ """
98
+ Class to handle the Gradio application interface.
99
+ """
100
+
101
+ def __init__(self, dataset_path: str):
102
+ """
103
+ Initialize with the path to the dataset.
104
+
105
+ Args:
106
+ dataset_path (str): Path to the dataset CSV file.
107
+ """
108
+ self.data_loader = DataLoader(dataset_path)
109
+ self.df = self.data_loader.load()
110
+ self.map_visualizer = MapVisualizer(self.df)
111
+
112
+ def filter_map(self, keyword: str) -> go.Figure:
113
+ """
114
+ Filter the map visualization by a keyword.
115
+
116
+ Args:
117
+ keyword (str): Keyword to filter the dataset by 'title' column.
118
+
119
+ Returns:
120
+ go.Figure: Plotly figure object with the map visualization.
121
+ """
122
+ return self.map_visualizer.create(keyword)
123
+
124
+ def launch(self):
125
+ """
126
+ Launch the Gradio application.
127
+ """
128
+ with gr.Blocks() as demo:
129
+ gr.Markdown("# πŸ“ˆ Real-Time Map Plot")
130
+ keyword_input = gr.Textbox(label="Keyword to Filter", placeholder="Enter keyword")
131
+ map_output = gr.Plot()
132
+ filter_button = gr.Button("Update Map")
133
+
134
+ filter_button.click(self.filter_map, inputs=[keyword_input], outputs=map_output)
135
+
136
+ demo.launch(share=True)
137
+
138
+ class HuggingFaceIntegration:
139
+ """
140
+ Class to handle interaction with Hugging Face Hub.
141
+ """
142
+
143
+ def __init__(self):
144
+ """
145
+ Initialize Hugging Face API.
146
+ """
147
+ self.api = HfApi()
148
+
149
+ def upload_dataset(self, df: pd.DataFrame, repo_id: str):
150
+ """
151
+ Upload dataset to Hugging Face Hub.
152
+
153
+ Args:
154
+ df (pd.DataFrame): DataFrame containing the dataset.
155
+ repo_id (str): Hugging Face repository ID.
156
+ """
157
+ dataset = Dataset.from_pandas(df)
158
+ dataset_dict = DatasetDict({"train": dataset})
159
+ dataset_dict.push_to_hub(repo_id)
160
+
161
+ def load_dataset(self, repo_id: str) -> pd.DataFrame:
162
+ """
163
+ Load dataset from Hugging Face Hub.
164
+
165
+ Args:
166
+ repo_id (str): Hugging Face repository ID.
167
+
168
+ Returns:
169
+ pd.DataFrame: Loaded dataset as a pandas DataFrame.
170
+ """
171
+ dataset = Dataset.from_hub(repo_id, split="train")
172
+ return dataset.to_pandas()
173
+
174
+ # Synthesize a dataset
175
+ synthesize_dataset('/content/synthetic_dataset.csv')
176
+
177
+ # Create and launch the Gradio app
178
+ if __name__ == "__main__":
179
+ app = GradioApp('/content/synthetic_dataset.csv')
180
+ app.launch()