import gradio as gr import pandas as pd import plotly.express as px def plot_real_estate(state): # Read the CSV file df = pd.read_csv('https://files.zillowstatic.com/research/public_csvs/zhvi/Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv') # Filter for the given state df_state = df[df['State'] == state.upper()] # Extract unique ZIP codes for the state zip_codes = df_state['RegionName'].unique() # Prepare an empty figure for plotting fig = px.Figure() # Loop through each ZIP code and add a plot line for zip_code in zip_codes: df_zip = df_state[df_state['RegionName'] == zip_code] # Select the columns with dates and transpose the data df_zip = df_zip.loc[:, '2000-01-31':].T.reset_index() df_zip.columns = ['Date', 'Price'] # Convert 'Date' to datetime df_zip['Date'] = pd.to_datetime(df_zip['Date']) # Add a line to the plot for this ZIP code fig.add_scatter(x=df_zip['Date'], y=df_zip['Price'], mode='lines', name=str(zip_code)) # Update plot layout fig.update_layout(title=f'Housing Prices in {state}', xaxis_title='Date', yaxis_title='Price') return fig iface = gr.Interface(fn=plot_real_estate, inputs=[gr.components.Textbox(label="State (e.g., NJ for New Jersey)")], outputs=gr.Plot()) iface.launch(share=False, debug=True)