gloignon commited on
Commit
aafdbf2
·
verified ·
1 Parent(s): 250aae7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -9,15 +9,18 @@ from sentence_transformers import SentenceTransformer
9
  model = SentenceTransformer('all-MiniLM-L6-v2')
10
 
11
  # Function to compute document embeddings and apply PCA
12
- def compute_pca(dataframe):
13
- # Drop empty rows and extract identifiers and texts
14
- dataframe = dataframe.dropna()
15
- identifiers = dataframe['Identifier'].tolist()
16
- texts = dataframe['Text'].tolist()
17
 
18
- # Check if there's data to process
19
- if len(texts) == 0:
20
- return gr.Plot.update(value=None, label="No data to process. Please fill in the table.")
 
 
 
 
21
 
22
  # Generate embeddings
23
  embeddings = model.encode(texts)
@@ -40,30 +43,28 @@ def compute_pca(dataframe):
40
  # Gradio interface
41
  def text_editor_app():
42
  with gr.Blocks() as demo:
43
- # Editable DataFrame with 4 initial rows
44
- data_table = gr.Dataframe(headers=["Identifier", "Text"], value=[["", ""] for _ in range(4)],
45
- interactive=True, wrap=True, label="Enter Identifiers and Texts")
46
-
 
 
 
 
 
 
 
 
 
 
47
  # Button to run the analysis
48
  analyze_button = gr.Button("Run Analysis")
49
 
50
  # Output plot
51
  output_plot = gr.Plot(label="PCA Visualization")
52
 
53
- # Add rows dynamically as the table is filled
54
- def add_rows(data):
55
- # Check if all current rows are filled
56
- if data["Identifier"].isnull().sum() == 0 and data["Text"].isnull().sum() == 0:
57
- # Add an empty row
58
- new_row = pd.DataFrame({"Identifier": [""], "Text": [""]})
59
- return pd.concat([data, new_row], ignore_index=True)
60
- return data
61
-
62
- # Update table as rows are filled
63
- data_table.change(fn=add_rows, inputs=data_table, outputs=data_table)
64
-
65
  # Run analysis when the button is clicked
66
- analyze_button.click(fn=compute_pca, inputs=data_table, outputs=output_plot)
67
 
68
  return demo
69
 
 
9
  model = SentenceTransformer('all-MiniLM-L6-v2')
10
 
11
  # Function to compute document embeddings and apply PCA
12
+ def compute_pca(id1, text1, id2, text2, id3, text3, id4, text4):
13
+ # Collect identifiers and texts into lists
14
+ identifiers = [id1, id2, id3, id4]
15
+ texts = [text1, text2, text3, text4]
 
16
 
17
+ # Filter out any empty inputs
18
+ valid_entries = [(id, text) for id, text in zip(identifiers, texts) if id and text]
19
+ if not valid_entries:
20
+ return gr.Plot.update(value=None, label="No data to process. Please fill in the boxes.")
21
+
22
+ # Unzip identifiers and texts
23
+ identifiers, texts = zip(*valid_entries)
24
 
25
  # Generate embeddings
26
  embeddings = model.encode(texts)
 
43
  # Gradio interface
44
  def text_editor_app():
45
  with gr.Blocks() as demo:
46
+ # Input boxes for four identifier-text pairs
47
+ with gr.Row():
48
+ id1 = gr.Textbox(label="Identifier 1")
49
+ text1 = gr.Textbox(label="Text 1")
50
+ with gr.Row():
51
+ id2 = gr.Textbox(label="Identifier 2")
52
+ text2 = gr.Textbox(label="Text 2")
53
+ with gr.Row():
54
+ id3 = gr.Textbox(label="Identifier 3")
55
+ text3 = gr.Textbox(label="Text 3")
56
+ with gr.Row():
57
+ id4 = gr.Textbox(label="Identifier 4")
58
+ text4 = gr.Textbox(label="Text 4")
59
+
60
  # Button to run the analysis
61
  analyze_button = gr.Button("Run Analysis")
62
 
63
  # Output plot
64
  output_plot = gr.Plot(label="PCA Visualization")
65
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  # Run analysis when the button is clicked
67
+ analyze_button.click(fn=compute_pca, inputs=[id1, text1, id2, text2, id3, text3, id4, text4], outputs=output_plot)
68
 
69
  return demo
70