Spaces:
Running
Running
import gradio as gr | |
import assemblyai as aai | |
from transformers import pipeline | |
import pandas as pd | |
import os | |
import firebase_admin | |
from firebase_admin import credentials, db | |
# Replace with your Firebase credentials JSON | |
firebase_credentials = { | |
"type": "service_account", | |
"project_id": "learning-5fd92", | |
"private_key_id": "3d926218194aee3902d5f92b2973effbbf27b4f8", | |
"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", | |
"client_email": "firebase-adminsdk-twa5w@learning-5fd92.iam.gserviceaccount.com", | |
"client_id": "116952036581323046961", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://oauth2.googleapis.com/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-twa5w%40learning-5fd92.iam.gserviceaccount.com", | |
"universe_domain": "googleapis.com" | |
} | |
# Initialize Firebase | |
cred = credentials.Certificate(firebase_credentials) | |
firebase_admin.initialize_app(cred, { | |
'databaseURL': 'https://learning-5fd92-default-rtdb.asia-southeast1.firebasedatabase.app/' | |
}) | |
ref = db.reference("/") | |
# Initialize AssemblyAI | |
aai.settings.api_key = "62acec891bb04c339ec059b738bedac6" | |
# Initialize question answering pipeline | |
question_answerer = pipeline("question-answering", model='distilbert-base-cased-distilled-squad') | |
# List of questions | |
questions = [ | |
"Which grade is the child studying?", | |
"How old is the child?", | |
"What is the gender?", | |
"Can you provide the name and location of the child's school?", | |
"What are the names of the child's guardians or parents?", | |
"What is the chief complaint regarding the child's oral health? If there is none, just say the word 'none', else elaborate only on medication history", | |
"Can you provide any relevant medical history for the child? If there is none, just say the word 'none', else elaborate", | |
"Does the child take any medications regularly? If there is none, just say the word 'none'. If yes, please specify.", | |
"When was the child's previous dental visit? If no visits before, just say the word 'first' or mention the visit number and nothing else", | |
"Does the child have any habits such as thumb sucking, tongue thrusting, nail biting, or lip biting? If yes, just list them and don't provide any further details", | |
"Does the patient brush their teeth? Just use the words 'once daily', 'twice daily', or 'thrice daily' to answer, nothing else", | |
"Does the child experience bleeding gums? Just say 'yes' or 'no' for this and nothing else", | |
"Has the child experienced early childhood caries? Just say 'yes' or 'no' and nothing else", | |
"Please mention if tooth decay is present with tooth number(s), else just say the word 'none' and nothing else", | |
"Have any teeth been fractured? If yes, please mention the tooth number(s), else just say 'none' and nothing else", | |
"Is there any pre-shedding mobility of teeth? If yes, please specify, else just say 'none' and nothing else", | |
"Does the child have malocclusion? If yes, please provide details, else just say the word 'none' and nothing | |
"Does the child experience pain, swelling, or abscess? If yes, please provide details, else just say 'none' and nothing else", | |
"Are there any other findings you would like to note?", | |
"What treatment plan do you recommend? Choose only from Options: (Scaling, Filling, Pulp therapy/RCT, Extraction, Medication, Referral) and nothing else" | |
] | |
# List for the oral health assessment form | |
oral_health_assessment_form = [ | |
"Doctor’s Name", | |
"Child’s Name", | |
"Grade", | |
"Age", | |
"Gender", | |
"School name and place", | |
"Guardian/Parents name", | |
"Chief complaint", | |
"Medical history", | |
"Medication history", | |
"Previous dental visit", | |
"Habits", | |
"Brushing habit", | |
"Bleeding gums", | |
"Early Childhood caries", | |
"Decayed", | |
"Fractured teeth", | |
"Preshedding mobility", | |
"Malocclusion", | |
"Does the child have pain, swelling or abscess? (Urgent care need)", | |
"Any other finding", | |
"Treatment plan", | |
] | |
# Function to generate answers for the questions | |
def generate_answer(question, context): | |
result = question_answerer(question=question, context=context) | |
return result['answer'] | |
# Function to handle audio recording and transcription | |
def transcribe_audio(audio_path): | |
print(f"Received audio file at: {audio_path}") | |
# Check if the file exists and is not empty | |
if not os.path.exists(audio_path): | |
return "Error: Audio file does not exist." | |
if os.path.getsize(audio_path) == 0: | |
return "Error: Audio file is empty." | |
try: | |
# Transcribe the audio file using AssemblyAI | |
transcriber = aai.Transcriber() | |
print("Starting transcription...") | |
transcript = transcriber.transcribe(audio_path) | |
print("Transcription process completed.") | |
# Handle the transcription result | |
if transcript.status == aai.TranscriptStatus.error: | |
print(f"Error during transcription: {transcript.error}") | |
return transcript.error | |
else: | |
context = transcript.text | |
print(f"Transcription text: {context}") | |
return context | |
except Exception as e: | |
print(f"Exception occurred: {e}") | |
return str(e) | |
# Function to fill in the DataFrame with answers | |
def fill_dataframe(context): | |
data = [] | |
for question in questions: | |
answer = generate_answer(question, context) | |
data.append({"Question": question, "Answer": answer}) | |
return pd.DataFrame(data) | |
# Function to push data to Firebase | |
def push_to_firebase(data): | |
ref.push(data) | |
print("Data pushed to Firebase successfully.") | |
# Main Gradio app function | |
def main(audio): | |
context = transcribe_audio(audio) | |
if "Error" in context: | |
return context | |
df = fill_dataframe(context) | |
# Add doctor's and patient's name to the beginning of the DataFrame | |
df = pd.concat([pd.DataFrame({"Question": ["Doctor’s Name", "Child’s Name"], "Answer": ["Dr. Charles Xavier", ""]}), df]) | |
# Add a title to the DataFrame | |
df['Question'] = oral_health_assessment_form | |
# Convert DataFrame to HTML table with editable text boxes | |
table_html = df.to_html(index=False, escape=False, formatters={"Answer": lambda x: f'<input type="text" value="{x}" />'}) | |
# Create submit button and save data to Firebase | |
submit_button = gr.Button("Submit") | |
output_html = gr.HTML(label="Assessment Form") | |
def submit_data(): | |
data = df.set_index('Question').to_dict()['Answer'] | |
push_to_firebase(data) | |
submit_button.click(fn=submit_data) | |
return gr.Interface( | |
[audio], | |
[output_html, submit_button], | |
title="Audio Transcription and Question Answering App", | |
live=False | |
) | |
# Launch the app | |
main_app = main(gr.Audio(type="filepath", label="Record your audio")) | |
main_app.launch() | |