gpravin1308's picture
Update app.py
28bb657 verified
raw
history blame
2.42 kB
# 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 = {}
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 remove NaN values
sheets[sheet_name] = df.iloc[:, 0].dropna().tolist()
else:
# If the sheet is empty or has no valid data, skip it
sheets[sheet_name] = [] # Optional: You can skip or keep an empty list
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 the first column
sheets = load_excel_sheets(file_path)
# Define function for Gradio
def generate_prompt(sheet_name):
return generate_random_item(sheet_name, sheets)
# Improved UI with better layout
interface = gr.Interface(
fn=generate_prompt, # Function to call for generating random item
inputs=gr.inputs.Dropdown(choices=list(sheets.keys()), label="Select Sheet"),
outputs=gr.outputs.Textbox(label="Generated Prompt"),
title="πŸ“ Witness Prompt Generator",
description="Select a sheet to generate a random prompt from the provided data.",
layout="vertical", # Ensure vertical layout
theme="default", # Apply a clean default theme
live=False, # Ensure that generation only happens on button click
css=".gradio-container {background-color: #f5f5f5; font-family: Arial; padding: 20px;} h1 {color: #333;}"
)
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'
# Create and launch interface
interface = prompt_generator_interface(file_path)
interface.launch()