Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,62 @@
|
|
| 1 |
-
# 1. Import necessary libraries
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import random
|
| 5 |
-
|
| 6 |
-
# Function to load Excel sheets and extract first column data from valid sheets
|
| 7 |
-
def load_excel_sheets(file_path):
|
| 8 |
-
# Load all sheets into a dictionary {sheet_name: DataFrame}
|
| 9 |
-
xls = pd.ExcelFile(file_path)
|
| 10 |
-
sheets = {
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1. Import necessary libraries
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
# Function to load Excel sheets and extract first column data from valid sheets
|
| 7 |
+
def load_excel_sheets(file_path):
|
| 8 |
+
# Load all sheets into a dictionary {sheet_name: DataFrame}
|
| 9 |
+
xls = pd.ExcelFile(file_path)
|
| 10 |
+
sheets = {}
|
| 11 |
+
|
| 12 |
+
for sheet_name in xls.sheet_names:
|
| 13 |
+
df = pd.read_excel(xls, sheet_name=sheet_name)
|
| 14 |
+
# Check if the sheet has at least one column and is not empty
|
| 15 |
+
if not df.empty and df.shape[1] > 0:
|
| 16 |
+
# Extract the first column and remove NaN values
|
| 17 |
+
sheets[sheet_name] = df.iloc[:, 0].dropna().tolist()
|
| 18 |
+
else:
|
| 19 |
+
# If the sheet is empty or has no valid data, skip it
|
| 20 |
+
sheets[sheet_name] = [] # Optional: You can skip or keep an empty list
|
| 21 |
+
|
| 22 |
+
return sheets
|
| 23 |
+
|
| 24 |
+
# Function to generate a random item from the selected sheet
|
| 25 |
+
def generate_random_item(sheet_name, sheets):
|
| 26 |
+
items = sheets.get(sheet_name, [])
|
| 27 |
+
if items:
|
| 28 |
+
return random.choice(items)
|
| 29 |
+
return "No items available."
|
| 30 |
+
|
| 31 |
+
# Gradio interface
|
| 32 |
+
def prompt_generator_interface(file_path):
|
| 33 |
+
# Load all the sheets and extract data from the first column
|
| 34 |
+
sheets = load_excel_sheets(file_path)
|
| 35 |
+
|
| 36 |
+
# Define function for Gradio
|
| 37 |
+
def generate_prompt(sheet_name):
|
| 38 |
+
return generate_random_item(sheet_name, sheets)
|
| 39 |
+
|
| 40 |
+
# Improved UI with better layout
|
| 41 |
+
interface = gr.Interface(
|
| 42 |
+
fn=generate_prompt, # Function to call for generating random item
|
| 43 |
+
inputs=gr.inputs.Dropdown(choices=list(sheets.keys()), label="Select Sheet"),
|
| 44 |
+
outputs=gr.outputs.Textbox(label="Generated Prompt"),
|
| 45 |
+
title="📝 Witness Prompt Generator",
|
| 46 |
+
description="Select a sheet to generate a random prompt from the provided data.",
|
| 47 |
+
layout="vertical", # Ensure vertical layout
|
| 48 |
+
theme="default", # Apply a clean default theme
|
| 49 |
+
live=False, # Ensure that generation only happens on button click
|
| 50 |
+
css=".gradio-container {background-color: #f5f5f5; font-family: Arial; padding: 20px;} h1 {color: #333;}"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return interface
|
| 54 |
+
|
| 55 |
+
# Create and launch the Gradio interface
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
# Path to your Excel file (adjust the path if necessary)
|
| 58 |
+
file_path = 'Witness Prompt Generator.xlsm'
|
| 59 |
+
|
| 60 |
+
# Create and launch interface
|
| 61 |
+
interface = prompt_generator_interface(file_path)
|
| 62 |
+
interface.launch()
|