Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,6 @@ import assemblyai as aai
|
|
3 |
from transformers import pipeline
|
4 |
import pandas as pd
|
5 |
import os
|
6 |
-
from firebase_admin import credentials, db, initialize_app
|
7 |
-
|
8 |
-
# Initialize Firebase with your credentials
|
9 |
-
cred = credentials.Certificate('credentials.json')
|
10 |
-
initialize_app(cred, {'databaseURL': 'https://learning-5fd92-default-rtdb.asia-southeast1.firebasedatabase.app/'})
|
11 |
|
12 |
# Replace with your AssemblyAI API key
|
13 |
aai.settings.api_key = "62acec891bb04c339ec059b738bedac6"
|
@@ -39,7 +34,6 @@ questions = [
|
|
39 |
"What treatment plan do you recommend? Choose only from Options: (Scaling, Filling, Pulp therapy/RCT, Extraction, Medication, Referral) and nothing else"
|
40 |
]
|
41 |
|
42 |
-
# List for the oral health assessment form
|
43 |
oral_health_assessment_form = [
|
44 |
"Doctor’s Name",
|
45 |
"Child’s Name",
|
@@ -71,15 +65,17 @@ def generate_answer(question, context):
|
|
71 |
return result['answer']
|
72 |
|
73 |
# Function to handle audio recording and transcription
|
74 |
-
def transcribe_audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
try:
|
76 |
-
# Save audio data to a temporary file
|
77 |
-
audio_path = '/tmp/audio.wav'
|
78 |
-
with open(audio_path, 'wb') as f:
|
79 |
-
f.write(audio_data)
|
80 |
-
|
81 |
-
print(f"Saved audio file at: {audio_path}")
|
82 |
-
|
83 |
# Transcribe the audio file using AssemblyAI
|
84 |
transcriber = aai.Transcriber()
|
85 |
print("Starting transcription...")
|
@@ -108,18 +104,9 @@ def fill_dataframe(context):
|
|
108 |
data.append({"Question": question, "Answer": answer})
|
109 |
return pd.DataFrame(data)
|
110 |
|
111 |
-
# Function to push data to Firebase
|
112 |
-
def push_to_firebase(data):
|
113 |
-
try:
|
114 |
-
ref = db.reference("/")
|
115 |
-
ref.push(data)
|
116 |
-
print("Data pushed to Firebase successfully.")
|
117 |
-
except Exception as e:
|
118 |
-
print(f"Error pushing data to Firebase: {e}")
|
119 |
-
|
120 |
# Main Gradio app function
|
121 |
-
def main(
|
122 |
-
context = transcribe_audio(
|
123 |
|
124 |
if "Error" in context:
|
125 |
return context
|
@@ -131,27 +118,23 @@ def main(audio_data):
|
|
131 |
|
132 |
# Add a title to the DataFrame
|
133 |
df['Question'] = oral_health_assessment_form
|
134 |
-
|
135 |
# Convert DataFrame to HTML table with editable text boxes
|
136 |
table_html = df.to_html(index=False, escape=False, formatters={"Answer": lambda x: f'<input type="text" value="{x}" />'})
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
title="Audio Transcription and Question Answering App",
|
152 |
-
live=False
|
153 |
-
)
|
154 |
|
155 |
# Launch the app
|
156 |
-
|
157 |
-
main_app.launch()
|
|
|
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"
|
|
|
34 |
"What treatment plan do you recommend? Choose only from Options: (Scaling, Filling, Pulp therapy/RCT, Extraction, Medication, Referral) and nothing else"
|
35 |
]
|
36 |
|
|
|
37 |
oral_health_assessment_form = [
|
38 |
"Doctor’s Name",
|
39 |
"Child’s Name",
|
|
|
65 |
return result['answer']
|
66 |
|
67 |
# Function to handle audio recording and transcription
|
68 |
+
def transcribe_audio(audio_path):
|
69 |
+
print(f"Received audio file at: {audio_path}")
|
70 |
+
|
71 |
+
# Check if the file exists and is not empty
|
72 |
+
if not os.path.exists(audio_path):
|
73 |
+
return "Error: Audio file does not exist."
|
74 |
+
|
75 |
+
if os.path.getsize(audio_path) == 0:
|
76 |
+
return "Error: Audio file is empty."
|
77 |
+
|
78 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Transcribe the audio file using AssemblyAI
|
80 |
transcriber = aai.Transcriber()
|
81 |
print("Starting transcription...")
|
|
|
104 |
data.append({"Question": question, "Answer": answer})
|
105 |
return pd.DataFrame(data)
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
# Main Gradio app function
|
108 |
+
def main(audio):
|
109 |
+
context = transcribe_audio(audio)
|
110 |
|
111 |
if "Error" in context:
|
112 |
return context
|
|
|
118 |
|
119 |
# Add a title to the DataFrame
|
120 |
df['Question'] = oral_health_assessment_form
|
121 |
+
|
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 |
+
return table_html
|
126 |
+
|
127 |
+
# Create the Gradio interface
|
128 |
+
with gr.Blocks() as demo:
|
129 |
+
gr.Markdown("# Audio Transcription and Question Answering App")
|
130 |
+
with gr.Row():
|
131 |
+
with gr.Column():
|
132 |
+
audio_input = gr.Audio(type="filepath", label="Record your audio")
|
133 |
+
output_html = gr.HTML(label="Assessment Form")
|
134 |
+
with gr.Column():
|
135 |
+
transcribe_button = gr.Button("Transcribe and Generate Form")
|
136 |
+
|
137 |
+
transcribe_button.click(fn=main, inputs=audio_input, outputs=output_html)
|
|
|
|
|
|
|
138 |
|
139 |
# Launch the app
|
140 |
+
demo.launch()
|
|