NikilDGr8 commited on
Commit
2d616b6
1 Parent(s): 55e766a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -20
app.py CHANGED
@@ -4,8 +4,7 @@ from transformers import pipeline
4
  import os
5
  from supabase import create_client, Client
6
  from datetime import datetime
7
- import pandas as pd
8
- import tempfile
9
 
10
  # Add your AssemblyAI API key as Environment Variable
11
  aai.settings.api_key = os.environ['Assembly']
@@ -140,28 +139,43 @@ def save_answers(*args):
140
  print(f"Error inserting data into Supabase: {e}")
141
  return f"Error saving answers: {e}"
142
 
143
- def download_csv():
144
- try:
145
- response = supabase.table('oral_health_assessments').select("*").execute()
146
- data = response.data
147
- if data:
148
- df = pd.DataFrame(data)
149
- with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as temp_file:
150
- df.to_csv(temp_file.name, index=False)
151
- temp_file_path = temp_file.name
152
- return temp_file_path
153
- else:
154
- return "No data available"
155
- except Exception as e:
156
- print(f"Error fetching data from Supabase: {e}")
157
- return f"Error: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  # Create the Gradio interface
160
  with gr.Blocks() as demo:
161
  gr.Markdown("# OHA Form Filler App")
162
- download_button = gr.Button("Download CSV", elem_id="download_button")
163
  audio_input = gr.Audio(type="filepath", label="Record your audio", elem_id="audio_input")
164
  transcribe_button = gr.Button("Transcribe and Generate Form", elem_id="transcribe_button")
 
165
 
166
  with gr.Row(elem_id="textboxes_row"):
167
  with gr.Column():
@@ -174,7 +188,7 @@ with gr.Blocks() as demo:
174
 
175
  transcribe_button.click(fn=main, inputs=audio_input, outputs=textboxes_left + textboxes_right)
176
  submit_button.click(fn=save_answers, inputs=textboxes_left + textboxes_right, outputs=output_html)
177
- download_button.click(fn=download_csv, inputs=None, outputs=gr.File(label="Download CSV"))
178
 
179
- # Launches the app
180
  demo.launch(share=True)
 
4
  import os
5
  from supabase import create_client, Client
6
  from datetime import datetime
7
+ import csv
 
8
 
9
  # Add your AssemblyAI API key as Environment Variable
10
  aai.settings.api_key = os.environ['Assembly']
 
139
  print(f"Error inserting data into Supabase: {e}")
140
  return f"Error saving answers: {e}"
141
 
142
+ # Function to download table as CSV
143
+ def download_table_to_csv():
144
+ # Fetch data from Supabase table
145
+ response = supabase.table("oral_health_assessments").select("*").execute()
146
+
147
+ # Check if data is available
148
+ if not response.data:
149
+ print("No data found in the table.")
150
+ return "No data found in the table."
151
+
152
+ data = response.data
153
+
154
+ # Prepare CSV data
155
+ csv_data = []
156
+
157
+ # Add header row
158
+ if len(data) > 0:
159
+ csv_data.append(data[0].keys())
160
+
161
+ # Add data rows
162
+ for row in data:
163
+ csv_data.append(row.values())
164
+
165
+ # Save CSV data to file
166
+ with open("oral_health_assessments.csv", "w", newline='') as f:
167
+ writer = csv.writer(f)
168
+ writer.writerows(csv_data)
169
+
170
+ print(f"Downloaded table 'oral_health_assessments' to 'oral_health_assessments.csv'")
171
+ return "Table downloaded successfully as 'oral_health_assessments.csv'"
172
 
173
  # Create the Gradio interface
174
  with gr.Blocks() as demo:
175
  gr.Markdown("# OHA Form Filler App")
 
176
  audio_input = gr.Audio(type="filepath", label="Record your audio", elem_id="audio_input")
177
  transcribe_button = gr.Button("Transcribe and Generate Form", elem_id="transcribe_button")
178
+ download_button = gr.Button("Download as CSV", elem_id="download_button")
179
 
180
  with gr.Row(elem_id="textboxes_row"):
181
  with gr.Column():
 
188
 
189
  transcribe_button.click(fn=main, inputs=audio_input, outputs=textboxes_left + textboxes_right)
190
  submit_button.click(fn=save_answers, inputs=textboxes_left + textboxes_right, outputs=output_html)
191
+ download_button.click(fn=download_table_to_csv, outputs=output_html)
192
 
193
+ # Launch the app
194
  demo.launch(share=True)