Spaces:
Runtime error
Runtime error
# 1. Import necessary libraries | |
import pandas as pd | |
import gradio as gr | |
import random | |
# Function to load Excel sheets and extract first column data from valid sheets | |
def load_excel_sheets(file_path): | |
# Load all sheets into a dictionary {sheet_name: DataFrame} | |
xls = pd.ExcelFile(file_path) | |
sheets = {sheet_name: pd.read_excel(xls, sheet_name=sheet_name).iloc[:, 0].dropna().tolist() | |
for sheet_name in xls.sheet_names} | |
return sheets | |
# Function to generate a random item from the selected sheet | |
def generate_random_item(sheet_name, sheets): | |
items = sheets.get(sheet_name, []) | |
if items: | |
return random.choice(items) | |
return "No items available." | |
# Gradio interface | |
def prompt_generator_interface(file_path): | |
# Load all the sheets and extract data from first column | |
sheets = load_excel_sheets(file_path) | |
# Define function for Gradio | |
def generate_prompt(sheet_name): | |
return generate_random_item(sheet_name, sheets) | |
# Create Gradio interface | |
interface = gr.Interface( | |
fn=generate_prompt, # Function to call for generating random item | |
inputs=gr.inputs.Dropdown(choices=list(sheets.keys()), label="Select Sheet"), # Dropdown to select sheet | |
outputs="text", # Output is a text | |
title="Excel Sheet Prompt Generator", | |
description="Select a sheet and generate a random item from that sheet" | |
) | |
return interface | |
# Create and launch the Gradio interface | |
if __name__ == "__main__": | |
# Path to your Excel file (adjust if needed) | |
file_path = 'Witness Prompt Generator.xlsm' | |
# Create and launch interface | |
interface = prompt_generator_interface(file_path) | |
interface.launch() | |