Dental_LLM / app.py
NikilDGr8's picture
Update app.py
2169527 verified
raw
history blame
No virus
7.46 kB
import gradio as gr
import assemblyai as aai
from transformers import pipeline
import pandas as pd
import os
# Replace with your AssemblyAI API key
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 else",
"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"
]
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)
# 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}" />'})
# Add JavaScript to capture changes and submit
custom_js = """
<script>
function getTableData() {
var table = document.querySelector('table');
var data = [];
for (var i = 1, row; row = table.rows[i]; i++) {
var question = row.cells[0].innerText;
var answer = row.cells[1].querySelector('input').value;
data.push({question: question, answer: answer});
}
return data;
}
function submitTableData() {
var data = getTableData();
fetch('/submit_table_data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
document.querySelector('#submit_status').innerText = 'Data submitted successfully!';
})
.catch((error) => {
console.error('Error:', error);
});
}
document.querySelector('#submit_button').addEventListener('click', submitTableData);
</script>
"""
return table_html + custom_js
# Function to handle submit button and store table data in dictionary
def submit_table_data(data):
tabledata = {item['question']: item['answer'] for item in data}
print("Submitted Table Data: ", tabledata)
return f"Data submitted successfully! {tabledata}"
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Audio Transcription and Question Answering App")
with gr.Row():
with gr.Column():
audio_input = gr.Audio(type="filepath", label="Record your audio")
output_html = gr.HTML(label="Assessment Form")
with gr.Column():
transcribe_button = gr.Button("Transcribe and Generate Form")
submit_button = gr.Button("Submit", elem_id="submit_button")
submit_status = gr.Textbox(label="Submit Status", elem_id="submit_status")
transcribe_button.click(fn=main, inputs=audio_input, outputs=output_html)
demo.add_event_listener('submit_table_data', fn=submit_table_data, inputs=None, outputs=submit_status)
# Launch the app
demo.launch()