gpravin1308 commited on
Commit
66f9ea0
·
verified ·
1 Parent(s): 0d708d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -29
app.py CHANGED
@@ -1,7 +1,5 @@
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):
@@ -13,42 +11,47 @@ def load_excel_sheets(file_path):
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.Dropdown(choices=list(sheets.keys()), label="Select Sheet"), # Updated input syntax
44
- outputs=gr.Textbox(label="Generated Prompt"), # Updated output syntax
45
- title="📝 Witness Prompt Generator",
46
- description="Select a sheet to generate a random prompt from the provided data.",
47
- theme="default", # Apply a clean default theme
48
- live=False, # Ensure that generation only happens on button click
49
- css=".gradio-container {background-color: #f5f5f5; font-family: Arial; padding: 20px;} h1 {color: #333;}"
50
- )
51
-
 
 
 
 
 
 
 
 
 
52
  return interface
53
 
54
  # Create and launch the Gradio interface
 
 
1
  import pandas as pd
2
  import gradio as gr
 
3
 
4
  # Function to load Excel sheets and extract first column data from valid sheets
5
  def load_excel_sheets(file_path):
 
11
  df = pd.read_excel(xls, sheet_name=sheet_name)
12
  # Check if the sheet has at least one column and is not empty
13
  if not df.empty and df.shape[1] > 0:
14
+ # Extract the first column and keep the order intact
15
  sheets[sheet_name] = df.iloc[:, 0].dropna().tolist()
 
 
 
16
 
17
  return sheets
18
 
19
+ # Function to create the combined prompt from selected values
20
+ def combine_selected_items(selected_items):
21
+ # Combine all the selected items into a single prompt
22
+ combined_prompt = " | ".join([item for item in selected_items if item])
23
+ return f"Combined Prompt: {combined_prompt}"
 
24
 
25
+ # Function to create the Gradio interface
26
  def prompt_generator_interface(file_path):
27
  # Load all the sheets and extract data from the first column
28
  sheets = load_excel_sheets(file_path)
29
 
30
+ # Gradio interface
31
+ with gr.Blocks() as interface:
32
+ gr.Markdown("# 📝 Witness Prompt Generator\nSelect an item from each sheet to generate a combined prompt.")
33
+
34
+ # Initialize an empty list to store all the dropdowns and user selections
35
+ dropdowns = []
36
+
37
+ # Reverse the order of sheets to display them right to left
38
+ reversed_sheet_names = list(sheets.keys())[::-1]
39
+
40
+ # Display all sheets as dropdowns in a grid
41
+ with gr.Row():
42
+ for sheet_name in reversed_sheet_names:
43
+ dropdown = gr.Dropdown(choices=sheets[sheet_name], label=sheet_name, interactive=True)
44
+ dropdowns.append(dropdown)
45
+
46
+ # Button to submit the combined prompt
47
+ submit_button = gr.Button("Generate Combined Prompt")
48
+
49
+ # Textbox to display the combined prompt
50
+ combined_output = gr.Textbox(label="Combined Prompt", placeholder="Your combined prompt will appear here...")
51
+
52
+ # Action when the submit button is clicked
53
+ submit_button.click(combine_selected_items, inputs=dropdowns, outputs=combined_output)
54
+
55
  return interface
56
 
57
  # Create and launch the Gradio interface