File size: 6,350 Bytes
a743eab
8593387
7166f82
 
de062b2
43b6c17
8593387
43b6c17
 
 
ef60d1f
43b6c17
8593387
 
7166f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43b6c17
7166f82
 
 
 
 
ef60d1f
7166f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef60d1f
7166f82
 
 
 
ef60d1f
43b6c17
4e4575b
43b6c17
 
 
 
 
 
 
4e4575b
 
 
 
 
 
 
 
 
 
 
 
7166f82
 
 
8593387
4e4575b
 
 
8593387
ef60d1f
7166f82
 
 
 
 
 
 
ef60d1f
 
43b6c17
 
 
 
 
 
ef60d1f
 
43b6c17
 
ef60d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7166f82
ef60d1f
43b6c17
ef60d1f
 
 
 
7166f82
ef60d1f
43b6c17
ef60d1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gradio as gr
import assemblyai as aai
from transformers import pipeline
import pandas as pd
import os
from firebase_admin import credentials, db, initialize_app

# Initialize Firebase with your credentials
cred = credentials.Certificate('credentials.json')
initialize_app(cred, {'databaseURL': 'https://learning-5fd92-default-rtdb.asia-southeast1.firebasedatabase.app/'})

# 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"
]

# 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_data):
    try:
        # Save audio data to a temporary file
        audio_path = '/tmp/audio.wav'
        with open(audio_path, 'wb') as f:
            f.write(audio_data)
        
        print(f"Saved audio file at: {audio_path}")

        # 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):
    try:
        ref = db.reference("/")
        ref.push(data)
        print("Data pushed to Firebase successfully.")
    except Exception as e:
        print(f"Error pushing data to Firebase: {e}")

# Main Gradio app function
def main(audio_data):
    context = transcribe_audio(audio_data)
    
    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(
        [gr.Audio(type="bytes", label="Record your audio")],
        [output_html, submit_button],
        title="Audio Transcription and Question Answering App",
        live=False
    )

# Launch the app
main_app = main()
main_app.launch()