Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,29 @@ import os
|
|
6 |
import firebase_admin
|
7 |
from firebase_admin import credentials, db
|
8 |
|
9 |
-
# Replace with your
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
aai.settings.api_key = "62acec891bb04c339ec059b738bedac6"
|
11 |
|
12 |
# Initialize question answering pipeline
|
@@ -30,12 +52,13 @@ questions = [
|
|
30 |
"Please mention if tooth decay is present with tooth number(s), else just say the word 'none' and nothing else",
|
31 |
"Have any teeth been fractured? If yes, please mention the tooth number(s), else just say 'none' and nothing else",
|
32 |
"Is there any pre-shedding mobility of teeth? If yes, please specify, else just say 'none' and nothing else",
|
33 |
-
"Does the child have malocclusion? If yes, please provide details, else just say the word 'none' and nothing
|
34 |
"Does the child experience pain, swelling, or abscess? If yes, please provide details, else just say 'none' and nothing else",
|
35 |
"Are there any other findings you would like to note?",
|
36 |
"What treatment plan do you recommend? Choose only from Options: (Scaling, Filling, Pulp therapy/RCT, Extraction, Medication, Referral) and nothing else"
|
37 |
]
|
38 |
|
|
|
39 |
oral_health_assessment_form = [
|
40 |
"Doctor’s Name",
|
41 |
"Child’s Name",
|
@@ -61,42 +84,12 @@ oral_health_assessment_form = [
|
|
61 |
"Treatment plan",
|
62 |
]
|
63 |
|
64 |
-
#
|
65 |
-
cred = credentials.Certificate('credentials.json')
|
66 |
-
firebase_admin.initialize_app(cred, {
|
67 |
-
'databaseURL': 'https://learning-5fd92-default-rtdb.asia-southeast1.firebasedatabase.app/'
|
68 |
-
})
|
69 |
-
|
70 |
-
ref = db.reference("/")
|
71 |
-
|
72 |
-
# Initialize Gradio Interface
|
73 |
-
def main(audio):
|
74 |
-
context = transcribe_audio(audio)
|
75 |
-
|
76 |
-
if "Error" in context:
|
77 |
-
return context
|
78 |
-
|
79 |
-
df = fill_dataframe(context)
|
80 |
-
|
81 |
-
# Add doctor's and patient's name to the beginning of the DataFrame
|
82 |
-
df = pd.concat([pd.DataFrame({"Question": ["Doctor’s Name", "Child’s Name"], "Answer": ["Dr. Charles Xavier", ""]}), df])
|
83 |
-
|
84 |
-
# Add a title to the DataFrame
|
85 |
-
df['Question'] = oral_health_assessment_form
|
86 |
-
|
87 |
-
# Convert DataFrame to HTML table with editable text boxes
|
88 |
-
table_html = df.to_html(index=False, escape=False, formatters={"Answer": lambda x: f'<input type="text" value="{x}" name="{x}" />'})
|
89 |
-
|
90 |
-
# Add a submit button
|
91 |
-
submit_button = gr.Button("Submit", onclick=submit_data)
|
92 |
-
table_html += submit_button
|
93 |
-
|
94 |
-
return table_html
|
95 |
-
|
96 |
def generate_answer(question, context):
|
97 |
result = question_answerer(question=question, context=context)
|
98 |
return result['answer']
|
99 |
|
|
|
100 |
def transcribe_audio(audio_path):
|
101 |
print(f"Received audio file at: {audio_path}")
|
102 |
|
@@ -128,6 +121,7 @@ def transcribe_audio(audio_path):
|
|
128 |
print(f"Exception occurred: {e}")
|
129 |
return str(e)
|
130 |
|
|
|
131 |
def fill_dataframe(context):
|
132 |
data = []
|
133 |
for question in questions:
|
@@ -135,19 +129,46 @@ def fill_dataframe(context):
|
|
135 |
data.append({"Question": question, "Answer": answer})
|
136 |
return pd.DataFrame(data)
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
146 |
|
147 |
-
#
|
148 |
-
gr.
|
149 |
-
|
150 |
-
gr.Audio(type="file", label="Record your audio"),
|
151 |
-
gr.HTML(label="Assessment Form"),
|
152 |
-
live=True
|
153 |
-
).launch()
|
|
|
6 |
import firebase_admin
|
7 |
from firebase_admin import credentials, db
|
8 |
|
9 |
+
# Replace with your Firebase credentials JSON
|
10 |
+
firebase_credentials = {
|
11 |
+
"type": "service_account",
|
12 |
+
"project_id": "learning-5fd92",
|
13 |
+
"private_key_id": "3d926218194aee3902d5f92b2973effbbf27b4f8",
|
14 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiZPrNvBtVqjM1\na+/MzyZ86s/Z7fHguR1FIuNM65nxZcG6tiOgFUD5RVq9RJKpHb1eB5NTCaLVVDGg\n1nmjX45Zt/3Wj6FQKFv20iruElaNpRfhhvxCjxb37XtgwAI4BwCi8nwCi3hjuPbN\ni2jd4cCQw0lR+bPDUq/1RF/rZC5OBCmV3KpM1XuZNfpB/0UQmOdhlenba9SYhkoG\nPom/ttJBcS2nfddCvPc4qRm/z97WV+FlBtU9P4ySGSbuU+SObKhBLVEZdD9xyHw/\nYGojIxsAj2C6pahsjD94sfPN+Pve4MnGeAWvp5An0H95inDz0ewgcQW1U9gYwKBF\nDpMsWhwFAgMBAAECggEAO0lOXwnLmOy4IUq0kzsVPAmltFQwN/CdBP6FqTFiVETo\nAzg3rVvVCqwNQofkWyVmWwu891XPQr6oF6X1ZnWeTJblbw2kwNTbLjLwRVYgjzm0\njDtxoQMISZ9DqcCvjrDPxK1uJKeyXgXK5KN+t3KRkMga3nig7jAj2RELS66ZURWT\nC5ul8I1wZtRwIerussw038xkmSfhPJ3OxXovt0cMULbR/TCzWOs7442TEEQ8OVop\ng/hqbJyALiN0Je4Ba8v7Vpf2CMa+m/0XeCvwevsziKrjzsfSl5D2J7uaeaSH3YCt\nfNsKCl4kVI6p1nPCmb7/607jBiHULdumGGnZrguMSQKBgQDPQDattsZoXugsa2kN\nrx1Y+gSdR7O5CWDk41zc7YRBOlV+yZVU0Ql+NStYSUhoZNGcAtvoOE/T1b53CmWf\n2u8R9MOi1+/bha6lWjD9UhFzSC9If6ks8/GLlrQRBLcBtL8D65eR0VNr8jYh4Prj\nnWlvQNlEBnrVQiWAMP3HL+OoIwKBgQDIl7NdtyuXFmMLFEp/vBlbux5LgwtC3UkC\nlCNLRHC26Dd+ofYdH1msK8U+hbVxzeE68G5Z0uUZXkPvwKRBTP6G+G2Rkq0pn2iz\noxbPEo3k4YAU387Hs7bIE4ijBjavAoQXlX6a+KtWD/NyXNxoPITuWcY1/69oUTRY\nSzDaHGCZtwKBgQCp50usJ94KLFwMJlYuWLQngHgN9H07IZwkqNw1ZiZ5TvhOA7Z/\nnQs6Qx5wt0s8AgLw8ksT2i5fTKhjcMuVPGengdBsjLhfb/2YIxNsaT5TEwdoWPf5\nGVeZBwhBt2rMZEAAV8CfU0sfgKauKYGk3iRUh4ZWRTWn5R1XNVKnW/Q3nwKBgHj5\nJC9MnHlOAwVXg2Ztpk1wF+EqcswscC+h73chG2jzuyQfwTHnCkmPTDFcqgSsST0m\naya4zXnzI72fzG+lY4s6n7m/X7dOb/KGVOclJXX3mSr9ntNJSt53CVxoDuqU9dzT\nR7tmZM5OaF98KWCjvveQJL3EKaEb+BmXnyqrGocZAoGBAIMZviUGb7KlmuctERej\ntVSpE7WZLbXLykMFNsgiswsGXANmv8VvBguRLS0C5wCp3Xd0Erz9jld0erPNGiaf\n/8rRN2ibnMhV0BKjRNQHLFKfA24Wm2vHX7scB0Vj9fGpzQlW2UOV66m/zfX5DWua\n7X1bfrWIpyN5dv/oXkMXBnvF\n-----END PRIVATE KEY-----\n",
|
15 |
+
"client_email": "[email protected]",
|
16 |
+
"client_id": "116952036581323046961",
|
17 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
18 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
19 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
20 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-twa5w%40learning-5fd92.iam.gserviceaccount.com",
|
21 |
+
"universe_domain": "googleapis.com"
|
22 |
+
}
|
23 |
+
|
24 |
+
# Initialize Firebase
|
25 |
+
cred = credentials.Certificate(firebase_credentials)
|
26 |
+
firebase_admin.initialize_app(cred, {
|
27 |
+
'databaseURL': 'https://learning-5fd92-default-rtdb.asia-southeast1.firebasedatabase.app/'
|
28 |
+
})
|
29 |
+
ref = db.reference("/")
|
30 |
+
|
31 |
+
# Initialize AssemblyAI
|
32 |
aai.settings.api_key = "62acec891bb04c339ec059b738bedac6"
|
33 |
|
34 |
# Initialize question answering pipeline
|
|
|
52 |
"Please mention if tooth decay is present with tooth number(s), else just say the word 'none' and nothing else",
|
53 |
"Have any teeth been fractured? If yes, please mention the tooth number(s), else just say 'none' and nothing else",
|
54 |
"Is there any pre-shedding mobility of teeth? If yes, please specify, else just say 'none' and nothing else",
|
55 |
+
"Does the child have malocclusion? If yes, please provide details, else just say the word 'none' and nothing
|
56 |
"Does the child experience pain, swelling, or abscess? If yes, please provide details, else just say 'none' and nothing else",
|
57 |
"Are there any other findings you would like to note?",
|
58 |
"What treatment plan do you recommend? Choose only from Options: (Scaling, Filling, Pulp therapy/RCT, Extraction, Medication, Referral) and nothing else"
|
59 |
]
|
60 |
|
61 |
+
# List for the oral health assessment form
|
62 |
oral_health_assessment_form = [
|
63 |
"Doctor’s Name",
|
64 |
"Child’s Name",
|
|
|
84 |
"Treatment plan",
|
85 |
]
|
86 |
|
87 |
+
# Function to generate answers for the questions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
def generate_answer(question, context):
|
89 |
result = question_answerer(question=question, context=context)
|
90 |
return result['answer']
|
91 |
|
92 |
+
# Function to handle audio recording and transcription
|
93 |
def transcribe_audio(audio_path):
|
94 |
print(f"Received audio file at: {audio_path}")
|
95 |
|
|
|
121 |
print(f"Exception occurred: {e}")
|
122 |
return str(e)
|
123 |
|
124 |
+
# Function to fill in the DataFrame with answers
|
125 |
def fill_dataframe(context):
|
126 |
data = []
|
127 |
for question in questions:
|
|
|
129 |
data.append({"Question": question, "Answer": answer})
|
130 |
return pd.DataFrame(data)
|
131 |
|
132 |
+
# Function to push data to Firebase
|
133 |
+
def push_to_firebase(data):
|
134 |
+
ref.push(data)
|
135 |
+
print("Data pushed to Firebase successfully.")
|
136 |
+
|
137 |
+
# Main Gradio app function
|
138 |
+
def main(audio):
|
139 |
+
context = transcribe_audio(audio)
|
140 |
+
|
141 |
+
if "Error" in context:
|
142 |
+
return context
|
143 |
+
|
144 |
+
df = fill_dataframe(context)
|
145 |
+
|
146 |
+
# Add doctor's and patient's name to the beginning of the DataFrame
|
147 |
+
df = pd.concat([pd.DataFrame({"Question": ["Doctor’s Name", "Child’s Name"], "Answer": ["Dr. Charles Xavier", ""]}), df])
|
148 |
+
|
149 |
+
# Add a title to the DataFrame
|
150 |
+
df['Question'] = oral_health_assessment_form
|
151 |
+
|
152 |
+
# Convert DataFrame to HTML table with editable text boxes
|
153 |
+
table_html = df.to_html(index=False, escape=False, formatters={"Answer": lambda x: f'<input type="text" value="{x}" />'})
|
154 |
+
|
155 |
+
# Create submit button and save data to Firebase
|
156 |
+
submit_button = gr.Button("Submit")
|
157 |
+
output_html = gr.HTML(label="Assessment Form")
|
158 |
+
|
159 |
+
def submit_data():
|
160 |
+
data = df.set_index('Question').to_dict()['Answer']
|
161 |
+
push_to_firebase(data)
|
162 |
+
|
163 |
+
submit_button.click(fn=submit_data)
|
164 |
|
165 |
+
return gr.Interface(
|
166 |
+
[audio],
|
167 |
+
[output_html, submit_button],
|
168 |
+
title="Audio Transcription and Question Answering App",
|
169 |
+
live=False
|
170 |
+
)
|
171 |
|
172 |
+
# Launch the app
|
173 |
+
main_app = main(gr.Audio(type="filepath", label="Record your audio"))
|
174 |
+
main_app.launch()
|
|
|
|
|
|
|
|