import pandas as pd import numpy as np import plotly.graph_objects as go import gradio as gr from datasets import Dataset, DatasetDict from huggingface_hub import HfApi, HfFolder # Generate a synthetic dataset def synthesize_dataset(file_path): np.random.seed(42) data = { 'title': [f'Place {i}' for i in range(100)], 'latitude': np.random.uniform(low=32.0, high=36.0, size=100), 'longitude': np.random.uniform(low=-114.0, high=-110.0, size=100), 'info': [f'Info {i}' for i in range(100)] } df = pd.DataFrame(data) df.to_csv(file_path, index=False) return df class DataLoader: """ Class to handle loading of datasets. """ def __init__(self, file_path: str): """ Initialize with the path to the dataset file. Args: file_path (str): Path to the CSV dataset file. """ self.file_path = file_path def load(self) -> pd.DataFrame: """ Load the dataset from the CSV file. Returns: pd.DataFrame: Loaded dataset as a pandas DataFrame. """ return pd.read_csv(self.file_path) class MapVisualizer: """ Class to handle creation of map visualizations. """ def __init__(self, df: pd.DataFrame): """ Initialize with a DataFrame. Args: df (pd.DataFrame): DataFrame containing the data to visualize. """ self.df = df def create(self, filter_keyword: str) -> go.Figure: """ Create a map visualization filtered by a keyword. Args: filter_keyword (str): Keyword to filter the dataset by 'title' column. Returns: go.Figure: Plotly figure object with the map visualization. """ filtered_df = self.df[self.df['title'].str.contains(filter_keyword, case=False, na=False)] fig = go.Figure(go.Scattermapbox( lat=filtered_df['latitude'].tolist(), lon=filtered_df['longitude'].tolist(), mode='markers', marker=go.scattermapbox.Marker(size=6), text=filtered_df['info'].tolist(), hoverinfo="text", hovertemplate='Info: %{text}
Latitude: %{lat}
Longitude: %{lon}' )) fig.update_layout( mapbox_style="open-street-map", hovermode='closest', mapbox=dict( bearing=0, center=go.layout.mapbox.Center( lat=filtered_df['latitude'].mean(), lon=filtered_df['longitude'].mean() ), pitch=0, zoom=8 ), ) return fig class GradioApp: """ Class to handle the Gradio application interface. """ def __init__(self, dataset_path: str): """ Initialize with the path to the dataset. Args: dataset_path (str): Path to the dataset CSV file. """ self.data_loader = DataLoader(dataset_path) self.df = self.data_loader.load() self.map_visualizer = MapVisualizer(self.df) def filter_map(self, keyword: str) -> go.Figure: """ Filter the map visualization by a keyword. Args: keyword (str): Keyword to filter the dataset by 'title' column. Returns: go.Figure: Plotly figure object with the map visualization. """ return self.map_visualizer.create(keyword) def launch(self): """ Launch the Gradio application. """ with gr.Blocks() as demo: gr.Markdown("# 📈 Real-Time Map Plot") keyword_input = gr.Textbox(label="Keyword to Filter", placeholder="Enter keyword") map_output = gr.Plot() filter_button = gr.Button("Update Map") filter_button.click(self.filter_map, inputs=[keyword_input], outputs=map_output) demo.launch(share=True) class HuggingFaceIntegration: """ Class to handle interaction with Hugging Face Hub. """ def __init__(self): """ Initialize Hugging Face API. """ self.api = HfApi() def upload_dataset(self, df: pd.DataFrame, repo_id: str): """ Upload dataset to Hugging Face Hub. Args: df (pd.DataFrame): DataFrame containing the dataset. repo_id (str): Hugging Face repository ID. """ dataset = Dataset.from_pandas(df) dataset_dict = DatasetDict({"train": dataset}) dataset_dict.push_to_hub(repo_id) def load_dataset(self, repo_id: str) -> pd.DataFrame: """ Load dataset from Hugging Face Hub. Args: repo_id (str): Hugging Face repository ID. Returns: pd.DataFrame: Loaded dataset as a pandas DataFrame. """ dataset = Dataset.from_hub(repo_id, split="train") return dataset.to_pandas() # Synthesize a dataset synthesize_dataset('/content/synthetic_dataset.csv') # Create and launch the Gradio app if __name__ == "__main__": app = GradioApp('/content/synthetic_dataset.csv') app.launch()