gloignon commited on
Commit
1014197
·
verified ·
1 Parent(s): 936a5e6

Back to minimal working version

Browse files
Files changed (1) hide show
  1. app.py +20 -57
app.py CHANGED
@@ -9,10 +9,10 @@ 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(*inputs):
13
- # Separate identifiers and texts
14
- identifiers = inputs[::2]
15
- texts = inputs[1::2]
16
 
17
  # Filter out any empty inputs
18
  valid_entries = [(id, text) for id, text in zip(identifiers, texts) if id and text]
@@ -40,46 +40,23 @@ def compute_pca(*inputs):
40
  fig = px.scatter(result_df, x='PC1', y='PC2', text='Identifier', title='PCA of Text Embeddings')
41
  return fig
42
 
43
- # Function to dynamically add new input pairs
44
- def add_new_input_boxes(identifiers, texts, n_boxes):
45
- # Add new identifier and text input pairs
46
- for i in range(n_boxes):
47
- index = len(identifiers) + 1
48
- identifiers.append(gr.Textbox(label=f"Identifier {index}", placeholder=f"Identifier {index}"))
49
- texts.append(gr.Textbox(label=f"Text {index}", placeholder=f"Text {index}"))
50
- return identifiers + texts
51
-
52
  # Gradio interface
53
  def text_editor_app():
54
  with gr.Blocks() as demo:
55
- # Lists to hold identifier and text boxes separately
56
- identifier_boxes = []
57
- text_boxes = []
58
-
59
- # Function to initialize default input boxes
60
- def initialize_default_boxes():
61
- initial_values = [("Animal 1", "Cats"), ("Animal 2", "Dogs"),
62
- ("Vehicle 1", "Planes"), ("Vehicle 2", "Boats")]
63
- for index, (identifier, text) in enumerate(initial_values, 1):
64
- identifier_boxes.append(gr.Textbox(label=f"Identifier {index}", value="", placeholder=identifier))
65
- text_boxes.append(gr.Textbox(label=f"Text {index}", value=text, placeholder=f"Text {index}"))
66
-
67
- # Initialize with default boxes
68
- initialize_default_boxes()
69
 
70
- # Counter to keep track of the number of components rendered so far
71
- counter = 0
72
-
73
- # Column to hold all input boxes
74
- with gr.Column() as input_container:
75
- for identifier, text in zip(identifier_boxes, text_boxes):
76
- identifier.render()
77
- text.render()
78
- counter += 1
79
-
80
- # Button to add more text input pairs
81
- add_button = gr.Button("Add Another Text")
82
-
83
  # Button to run the analysis
84
  analyze_button = gr.Button("Run Analysis")
85
 
@@ -87,23 +64,9 @@ def text_editor_app():
87
  output_plot = gr.Plot(label="PCA Visualization")
88
 
89
  # Run analysis when the button is clicked
90
- analyze_button.click(fn=compute_pca, inputs=identifier_boxes + text_boxes, outputs=output_plot)
91
-
92
- # Add new input boxes dynamically
93
- def update_input_container():
94
- # Create new input boxes and add them to the container
95
- new_boxes = add_new_input_boxes(identifier_boxes, text_boxes, n_boxes=1)
96
- return gr.Column(components=new_boxes)
97
-
98
- add_button.click(fn=update_input_container, inputs=[], outputs=input_container)
99
-
100
- # Display the input fields and buttons
101
- demo.append(input_container)
102
- demo.append(add_button)
103
- demo.append(analyze_button)
104
- demo.append(output_plot)
105
-
106
  return demo
107
 
108
  # Launch the app
109
- text_editor_app().launch()
 
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]
 
40
  fig = px.scatter(result_df, x='PC1', y='PC2', text='Identifier', title='PCA of Text Embeddings')
41
  return fig
42
 
 
 
 
 
 
 
 
 
 
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
 
 
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
 
71
  # Launch the app
72
+ text_editor_app().launch()