NikilDGr8 commited on
Commit
c059d59
1 Parent(s): 2169527

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -41
app.py CHANGED
@@ -3,6 +3,7 @@ import assemblyai as aai
3
  from transformers import pipeline
4
  import pandas as pd
5
  import os
 
6
 
7
  # Replace with your AssemblyAI API key
8
  aai.settings.api_key = "62acec891bb04c339ec059b738bedac6"
@@ -122,47 +123,23 @@ def main(audio):
122
  # Convert DataFrame to HTML table with editable text boxes
123
  table_html = df.to_html(index=False, escape=False, formatters={"Answer": lambda x: f'<input type="text" value="{x}" />'})
124
 
125
- # Add JavaScript to capture changes and submit
126
- custom_js = """
127
- <script>
128
- function getTableData() {
129
- var table = document.querySelector('table');
130
- var data = [];
131
- for (var i = 1, row; row = table.rows[i]; i++) {
132
- var question = row.cells[0].innerText;
133
- var answer = row.cells[1].querySelector('input').value;
134
- data.push({question: question, answer: answer});
135
- }
136
- return data;
137
- }
138
 
139
- function submitTableData() {
140
- var data = getTableData();
141
- fetch('/submit_table_data', {
142
- method: 'POST',
143
- headers: {
144
- 'Content-Type': 'application/json',
145
- },
146
- body: JSON.stringify(data),
147
- })
148
- .then(response => response.json())
149
- .then(data => {
150
- document.querySelector('#submit_status').innerText = 'Data submitted successfully!';
151
- })
152
- .catch((error) => {
153
- console.error('Error:', error);
154
- });
155
- }
156
 
157
- document.querySelector('#submit_button').addEventListener('click', submitTableData);
158
- </script>
159
- """
 
 
 
 
 
160
 
161
- return table_html + custom_js
162
-
163
- # Function to handle submit button and store table data in dictionary
164
- def submit_table_data(data):
165
- tabledata = {item['question']: item['answer'] for item in data}
166
  print("Submitted Table Data: ", tabledata)
167
  return f"Data submitted successfully! {tabledata}"
168
 
@@ -175,11 +152,10 @@ with gr.Blocks() as demo:
175
  output_html = gr.HTML(label="Assessment Form")
176
  with gr.Column():
177
  transcribe_button = gr.Button("Transcribe and Generate Form")
178
- submit_button = gr.Button("Submit", elem_id="submit_button")
179
- submit_status = gr.Textbox(label="Submit Status", elem_id="submit_status")
180
 
181
  transcribe_button.click(fn=main, inputs=audio_input, outputs=output_html)
182
- demo.add_event_listener('submit_table_data', fn=submit_table_data, inputs=None, outputs=submit_status)
183
 
184
  # Launch the app
185
  demo.launch()
 
3
  from transformers import pipeline
4
  import pandas as pd
5
  import os
6
+ from bs4 import BeautifulSoup
7
 
8
  # Replace with your AssemblyAI API key
9
  aai.settings.api_key = "62acec891bb04c339ec059b738bedac6"
 
123
  # Convert DataFrame to HTML table with editable text boxes
124
  table_html = df.to_html(index=False, escape=False, formatters={"Answer": lambda x: f'<input type="text" value="{x}" />'})
125
 
126
+ return table_html
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
+ # Function to handle submit button and store table data in dictionary
129
+ def submit_table_data(html_table):
130
+ # Parse the HTML table using BeautifulSoup
131
+ soup = BeautifulSoup(html_table, 'html.parser')
132
+ tabledata = {}
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
+ # Iterate through the rows of the table
135
+ rows = soup.find_all('tr')
136
+ for row in rows:
137
+ cells = row.find_all('td')
138
+ if len(cells) == 2:
139
+ question = cells[0].text.strip()
140
+ answer = cells[1].find('input')['value']
141
+ tabledata[question] = answer
142
 
 
 
 
 
 
143
  print("Submitted Table Data: ", tabledata)
144
  return f"Data submitted successfully! {tabledata}"
145
 
 
152
  output_html = gr.HTML(label="Assessment Form")
153
  with gr.Column():
154
  transcribe_button = gr.Button("Transcribe and Generate Form")
155
+ submit_button = gr.Button("Submit")
 
156
 
157
  transcribe_button.click(fn=main, inputs=audio_input, outputs=output_html)
158
+ submit_button.click(fn=submit_table_data, inputs=output_html, outputs=gr.Textbox(label="Submit Status"))
159
 
160
  # Launch the app
161
  demo.launch()