File size: 1,769 Bytes
aae823c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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()