import gradio as gr import pandas as pd import os # Define the upload directory relative to the app's location BASE_DIR = os.path.dirname(os.path.abspath(__file__)) UPLOAD_DIR = BASE_DIR def upload_file(file): try: # Save the uploaded file to the base directory file_path = os.path.join(UPLOAD_DIR, file.name) with open(file_path, "wb") as f: f.write(file.read()) # Read the CSV file and return the first few rows as a preview df = pd.read_csv(file_path) return df.head().to_html() except Exception as e: return str(e) iface = gr.Interface( fn=upload_file, inputs=gr.inputs.File(label="Upload CSV File"), outputs="html", title="CSV File Uploader", description="Upload a CSV file and preview the first few rows." ) if __name__ == "__main__": iface.launch()