Spaces:
Runtime error
Runtime error
File size: 4,463 Bytes
28bb657 b20c168 28bb657 66f9ea0 28bb657 b137304 016b0ba 28bb657 66f9ea0 b137304 28bb657 b137304 016b0ba b137304 016b0ba b20c168 66f9ea0 016b0ba b137304 66f9ea0 b137304 016b0ba b20c168 016b0ba b20c168 016b0ba 66f9ea0 b20c168 66f9ea0 016b0ba 66f9ea0 28bb657 b137304 28bb657 b137304 28bb657 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import pandas as pd
import gradio as gr
from ui_components import CustomDropdown, CustomButton # Example: Replace with actual component names
# 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 = {}
for sheet_name in xls.sheet_names:
df = pd.read_excel(xls, sheet_name=sheet_name)
# Check if the sheet has at least one column and is not empty
if not df.empty and df.shape[1] > 0:
# Extract the first column and keep the order intact
sheets[sheet_name] = df.iloc[:, 0].dropna().tolist()
return sheets
# Function to get the order of sheets from the main sheet
def get_sheet_order(file_path, main_sheet_name):
# Read the main sheet which contains the sheet order
df_order = pd.read_excel(file_path, sheet_name=main_sheet_name)
# Assuming the sheet names are listed in the first column
return df_order.iloc[:, 0].dropna().tolist()
# Function to create the combined prompt from selected values and weights
def combine_selected_items(*selected_items_and_weights):
combined_prompt = []
for i in range(0, len(selected_items_and_weights), 2):
item = selected_items_and_weights[i]
weight = selected_items_and_weights[i+1]
if item:
combined_prompt.append(f"{item} (Weight: {weight})" if weight else item)
return f"Combined Prompt: {' | '.join(combined_prompt)}"
# Function to create the Gradio interface
def prompt_generator_interface(file_path, main_sheet_name):
# Load all the sheets and extract data from the first column
sheets = load_excel_sheets(file_path)
# Get the sheet order from the main sheet
sheet_order = get_sheet_order(file_path, main_sheet_name)
# Filter out sheets that don't exist in the main order
valid_sheets = {sheet_name: sheets[sheet_name] for sheet_name in sheet_order if sheet_name in sheets}
# Define the sheets that should NOT have weights
no_weight_sheets = ["Poetic", "Scenarios", "Camera Setup", "Combos", "PositiceNegative prompts", "LAZY Mode"]
# Move Resources tab to the last place
if 'Resources' in valid_sheets:
valid_sheets['Resources'] = valid_sheets.pop('Resources')
# Gradio interface using custom UI components
with gr.Blocks() as interface:
gr.Markdown("# 📝 Witness Prompt Generator\nSelect an item from each sheet to generate a combined prompt.")
# Initialize an empty list to store all the dropdowns and weight inputs
dropdowns_and_weights = []
# Display all sheets as dropdowns in the order specified by the main sheet
with gr.Row():
for sheet_name in valid_sheets:
with gr.Column():
# Using the custom dropdown from ui_components.py
dropdown = CustomDropdown(choices=valid_sheets[sheet_name], label=sheet_name, interactive=True)
dropdowns_and_weights.append(dropdown)
# If the sheet is not in the no_weight_sheets, add a weight input
if sheet_name not in no_weight_sheets:
# Custom weight input
weight_input = gr.Textbox(label=f"{sheet_name} Weight", placeholder="Enter weight (optional)", interactive=True)
dropdowns_and_weights.append(weight_input)
# Using a custom button for submission
submit_button = CustomButton("Generate Combined Prompt")
# Textbox to display the combined prompt
combined_output = gr.Textbox(label="Combined Prompt", placeholder="Your combined prompt will appear here...")
# Action when the submit button is clicked
submit_button.click(combine_selected_items, inputs=dropdowns_and_weights, outputs=combined_output)
return interface
# Create and launch the Gradio interface
if __name__ == "__main__":
# Path to your Excel file (adjust the path if necessary)
file_path = 'Witness Prompt Generator.xlsm'
# Define the name of the main sheet that contains the order of sheets
main_sheet_name = 'Main Order' # Change this to match the name of your control sheet
# Create and launch interface
interface = prompt_generator_interface(file_path, main_sheet_name)
interface.launch()
|