Human Interactive Chatbot

I want to restructure my code for the output means what whatever is coming in canvas window I want to customize it.
For the Top Abnormalities Question the output should come in scripted and defined way such that
In the first it should gave the pop up window “Do You want the listen the top abnormalities for today”
With the two option Yes and No, If yes is clicked code should run further if no is clicked code should Stop and the history should delete.

When Yes is clicked code should move forward and the it should speak: “Dear (here whatever is given as an input from the SPADM column should come For Example: Top Abnormalities of Aashir Aslam, here Aashir Aslam Should come ) Your top 3 abnormalities are” :”Your First Abnormality is {top_3_abnormalities[0]}”(Abnormality function is a , After this new pop up window should came which should SPEAK: “Do you want to listen the details of this Priority” with the 2 buttons Yes and No, if user clicked the yes button then code should show the details of only {top_3_abnormalities[0]} from the excel all the relevant Columns “Number”, “Update”, “Name”, “Remarks”) ,

if No Button is clicked then only it should proceed further to the Second Abnormality {top_3_abnormalities[1]} ]}, by speaking, :”Your Second Abnormality is {top_3_abnormalities[1]}” , After this new pop up window should came which should SPEAK: “Do you want to listen the details of this Priority” with the 2 buttons Yes and No, if user clicked the yes button then code should show the details of only {top_3_abnormalities[1]} from the excel all the relevant Columns “Number”, “Update”, “Name”, “Remarks”) ,

if No Button is clicked then only it should proceed further to the Second Abnormality {top_3_abnormalities[2]} ]}, by speaking, :”Your Second Abnormality is {top_3_abnormalities[2]}” , After this new pop up window should came which should SPEAK: “Do you want to listen the details of this Priority” with the 2 buttons Yes and No, if user clicked the yes button then code should show the details of only {top_3_abnormalities[2]} from the excel all the relevant Columns “Number”, “Update”, “Name”, “Remarks”) ,

Do not Show any thing else except the above scripted output I want, Abnormalities codes already defined In the code below

Function to be executed when “Yes” button is clicked

def proceed_with_code():
# Your code here
print(“User chose ‘Yes’”)

Function to handle user input and display the details (including voice input)

import re

def handle_input():
input_value = input_entry.get()

if input_value:

    # Check if any specific words occur in the input
        specific_words = ["abnormalities", "top abnormalities", "top 3 abnormalities", "risks", "top 3 risks", "priorities", "top 3 priorities"]
        if any(word.lower() in input_value.lower() for word in specific_words):
            show_popup_window()
        else:
            # Process the input and display details
            Thread(target=handle_input_task, args=(input_value,)).start()

def proceed_with_code():

input_value = input_entry.get()

if input_value:
    # Display the input text on the canvas
    canvas2.delete("all")
    canvas2.create_text(490, 120, anchor=NE, justify=RIGHT, text=input_value, font=('fixedsys', -30), fill="white", width=350)
    global img3
    loading = Label(root, image=img3, bd=0)
    loading.place(x=1150, y=622)

    if "greet" in input_value.lower():
        # Greet and wish the user
        p1 = Thread(target=speak, args=("Hello! I am your GSK SPADM assistant.",))
        p1.start()
        p2 = Thread(target=wishme)
        p2.start()
    else:
        # Process the input and display details
        Thread(target=handle_input_task, args=(input_value,)).start()

def handle_input_task(input_value):

Rest of the handle_input() function without the canvas2.delete(“all”) and display details logic

if input_value:
    # Check if the input statement contains any of the SPADM values
    spadm_matches = re.findall(r"\b(?:Muhammad Ameer Hamza|Aashir Aslam|Ali Jaffer|Muhammad Nadeem|Muhammad Arsalan|Ovais Ali Khan|Rehma Akram|Rizwan Haider)\b", input_value)
    if spadm_matches:
        # Iterate over the matched SPADM values
        for spadm_value in spadm_matches:
            # Check if the SPADM value exists in the DataFrame
            if spadm_value in df["SPADM"].values:
                # Sort by Amount column when input is an SPADM value
                if "Amount" in df.columns:
                    details = df[df["SPADM"] == spadm_value].sort_values("Amount", ascending=False).head(3)
                else:
                    raise KeyError("Column 'Amount' not found in the DataFrame.")
                
                # Extract the relevant columns (Number, Update, Name, Remarks, SPADM, Type, Packs, Loss)
                extracted_details = details[["Number", "Update", "Name", "Remarks", "SPADM", "Type", "Packs", "Loss"]]

                # Clear previous data in the canvas2
                canvas2.delete("all")

                # Insert the details into the canvas2 using create_text
                y_offset = 10
                for index, row in extracted_details.iterrows():
                    details_text = f"Number: {row['Number']}"
                    canvas2.create_text(10, y_offset, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

                    details_text = f"Name: {row['Name']}"
                    canvas2.create_text(10, y_offset + 30, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

                    details_text = f"Remarks: {row['Remarks']}"
                    canvas2.create_text(10, y_offset + 60, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

                    details_text = f"SPADM: {row['SPADM']}"
                    canvas2.create_text(10, y_offset + 90, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

                    y_offset += 150

                # Save the details to a file for future reference
                with open("response_history.txt", "a") as file:
                    file.write(f"Input: {input_value}\n")
                    file.write(f"Details: {extracted_details.to_string(index=False)}\n\n")

                # Reset the chatbot response label
                chatbot_response = ""

                # Get the top 3 abnormalities for the SPADM value
                top_3_abnormalities = details["Type"].to_list()[:3]

                # Create a string representation of the abnormalities with commas
                abnormalities_text = ", ".join(top_3_abnormalities)

                # Add the abnormalities to the canvas2
                abnormality_text = f"Your top abnormalities are: {abnormalities_text}"
                canvas2.create_text(10, 530, anchor="nw", text=abnormality_text, font=('Candara Light', -25, 'bold italic'), fill="white")

                # Speak out the top 3 abnormalities in a scripted way
                speak(f"Dear Officer, your Top Abnormalities for today are: Your first abnormality is {top_3_abnormalities[0]}. Your second abnormality is {top_3_abnormalities[1]}. Your third abnormality is {top_3_abnormalities[2]}.")
            else:
                # Display a message if no details found
                canvas2.delete("all")  # Clear previous data in the canvas2
                no_details_text = "No details found for the Input value"
                canvas2.create_text(10, 10, anchor="nw", text=no_details_text, font=('Arial', 12), fill="red")

                # Call the chatbot API and get the response
                chatbot_response = chatbot_query(input_value)
                canvas2.create_text(10, 200, anchor="nw", text=chatbot_response, font=('Candara Light', -25,'bold italic'), fill="white")

                # Save the chatbot response to a file for future reference
                with open("response_history.txt", "a") as file:
                    file.write(f"Input: {input_value}\n")
                    file.write(f"Chatbot Response: {chatbot_response}\n\n")

                # Speak the chatbot response
                engine.say(chatbot_response)
                engine.runAndWait()
    else:
        try:
            input_value = int(input_value)  # Convert input value to int
            if input_value in df["Number"].values:
                details = df[df["Number"] == input_value]
            elif input_value in df["SPADM"].values:
                # Sort by Amount column when input is an SPADM value
                if "Amount" in df.columns:
                    details = df[df["SPADM"] == input_value].sort_values("Amount", ascending=False).head(3)
                else:
                    raise KeyError("Column 'Amount' not found in the DataFrame.")
            else:
                details = None
        except ValueError:
            if input_value in df["Name"].values:
                details = df[df["Name"] == input_value]
            elif input_value in df["SPADM"].values:
                # Sort by Amount column when input is an SPADM value
                if "Amount" in df.columns:
                    details = df[df["SPADM"] == input_value].sort_values("Amount", ascending=False).head(3)
                else:
                    raise KeyError("Column 'Amount' not found in the DataFrame.")
            else:
                details = None


    if details is not None and not details.empty:
        # Extract the relevant columns (Number, Update, Name, Remarks, SPADM, Type, Packs, Loss)
        extracted_details = details[["Number", "Update", "Name", "Remarks", "SPADM", "Type", "Packs", "Loss"]]

        # Clear previous data in the canvas2
        canvas2.delete("all")

        # Insert the details into the canvas2 using create_text
        y_offset = 10
        for index, row in extracted_details.iterrows():
            details_text = f"Number: {row['Number']}"
            canvas2.create_text(10, y_offset, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

            details_text = f"Name: {row['Name']}"
            canvas2.create_text(10, y_offset + 30, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

            details_text = f"Remarks: {row['Remarks']}"
            canvas2.create_text(10, y_offset + 60, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

            details_text = f"SPADM: {row['SPADM']}"
            canvas2.create_text(10, y_offset + 90, anchor="nw", text=details_text, font=('Candara Light', -25,'bold italic'), fill="white")

            y_offset += 150


        # Save the details to a file for future reference
        with open("response_history.txt", "a") as file:
            file.write(f"Input: {input_value}\n")
            file.write(f"Details: {extracted_details.to_string(index=False)}\n\n")

        # Reset the chatbot response label
        chatbot_response = ""

        # Get the top 3 abnormalities for the SPADM value
        top_3_abnormalities = details["Type"].to_list()[:3]

        # Create a string representation of the abnormalities with commas
        abnormalities_text = ", ".join(top_3_abnormalities)

        # Add the abnormalities to the canvas2
        abnormality_text = f"Your top abnormality is: {abnormalities_text}"
        canvas2.create_text(10, 500, anchor="nw", text=abnormality_text, font=('Candara Light', -25, 'bold italic'), fill="white")
    
        # Speak out the details only if it's not an SPADM value
        if input_value not in df["SPADM"].values:
            speak("Here are the details:")
            for index, row in extracted_details.iterrows():
                speak(row["Name"])
                speak(row["Remarks"])
                speak(row["SPADM"])
            speak("Here are the details from SPADM:")

            # Speak out the headings only if it's not an SPADM value
            if "Type" in extracted_details.columns:
                speak(risk_heading)
                speak(impact_heading)
                speak(financial_impact_heading) 
                speak(result_heading)

        # Speak out the top 3 abnormalities in a scripted way
        speak(f"Dear Officer , your Top Priority for today is {abnormalities_text}.")

       
    else:
        # Display a message if no details found
        canvas2.delete("all")  # Clear previous data in the canvas2
        no_details_text = "No details found for the Input value"
        canvas2.create_text(10, 10, anchor="nw", text=no_details_text, font=('Arial', 12), fill="red")

        # Call the chatbot API and get the response
        chatbot_response = chatbot_query(input_value)
        canvas2.create_text(10, 200, anchor="nw", text=chatbot_response, font=('Candara Light', -25,'bold italic'), fill="white")

        # Save the chatbot response to a file for future reference
        with open("response_history.txt", "a") as file:
            file.write(f"Input: {input_value}\n")
            file.write(f"Chatbot Response: {chatbot_response}\n\n")

        # Speak the chatbot response
        engine.say(chatbot_response)
        engine.runAndWait()
else:
    # Handle voice input
    query = takecommand()
    if query != "None":
        input_entry.delete(0, tk.END)
        input_entry.insert(tk.END, query)
        handle_input()

Please read the pinned thread and fix the formatting of the code so that we can consider it properly.

It is also helpful to explain exactly what the issue is with the code. Nobody will want to write code to a specification for you, or do homework. It looks like you have already made a serious attempt at the task. The next step is to identify a problem to work on.