import gradio as gr from huggingface_hub import InferenceClient import numpy as np import pandas as pd import joblib import pickle """ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference """ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") cols = pd.read_csv('./Columns.csv') cols = cols['Cols'] #with open("./model.pkl", "rb") as f: # model = pickle.load(f) model = joblib.load("./model.pkl") severityDictionary=dict() description_list = dict() precautionDictionary=dict() symptoms_dict = {} x = cols for index, symptom in enumerate(x): symptoms_dict[symptom] = index def calc_condition(exp,days): sum=0 for item in exp: sum=sum+severityDictionary[item] if((sum*days)/(len(exp)+1)>13): print("You should take the consultation from doctor. ") else: print("It might not be that bad but you should take precautions.") def getDescription(): global description_list with open('/kaggle/input/dataset/symptom_Description.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: _description={row[0]:row[1]} description_list.update(_description) def getSeverityDict(): global severityDictionary with open('/kaggle/input/dataset/Symptom_severity.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 try: for row in csv_reader: _diction={row[0]:int(row[1])} severityDictionary.update(_diction) except: pass def getprecautionDict(): global precautionDictionary with open('/kaggle/input/dataset/symptom_precaution.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: _prec={row[0]:[row[1],row[2],row[3],row[4]]} precautionDictionary.update(_prec) def getInfo(): print("-----------------------------------HealthCare ChatBot-----------------------------------") print("\nYour Name? \t\t\t\t",end="->") name=input("") print("Hello", name) def check_pattern(dis_list,inp): pred_list=[] inp=inp.replace(' ','_') patt = f"{inp}" regexp = re.compile(patt) pred_list=[item for item in dis_list if regexp.search(item)] if(len(pred_list)>0): return 1,pred_list else: return 0,[] def sec_predict(symptoms_exp): df = pd.read_csv('/kaggle/input/dataset/Training.csv') X = df.iloc[:, :-1] y = df['prognosis'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=20) rf_clf = DecisionTreeClassifier() rf_clf.fit(X_train, y_train) symptoms_dict = {symptom: index for index, symptom in enumerate(X)} input_vector = np.zeros(len(symptoms_dict)) for item in symptoms_exp: input_vector[[symptoms_dict[item]]] = 1 return rf_clf.predict([input_vector]) def print_disease(node): node = node[0] val = node.nonzero() disease = le.inverse_transform(val[0]) return list(map(lambda x:x.strip(),list(disease))) def tree_to_code(tree, feature_names): getSeverityDict() getDescription() getprecautionDict() getInfo() tree_ = tree.tree_ feature_name = [ feature_names[i] if i != _tree.TREE_UNDEFINED else "undefined!" for i in tree_.feature ] chk_dis=",".join(feature_names).split(",") symptoms_present = [] while True: # engine.say("\n Enter the symptom you are experiencing \t\t\t",) #engine.runAndWait() print("\nEnter the symptom you are experiencing \t\t",end="->") disease_input = input("") conf,cnf_dis=check_pattern(chk_dis,disease_input) if conf==1: print("searches related to input: ") for num,it in enumerate(cnf_dis): print(num,")",it) if num!=0: print(f"Select the one you meant (0 - {num}): ", end="") conf_inp = int(input("")) else: conf_inp=0 disease_input=cnf_dis[conf_inp] break else: print("Enter valid symptom.") while True: try: num_days=int(input("Okay. From how many days ? : ")) break except: print("Enter valid input.") def recurse(node, depth): indent = " " * depth if tree_.feature[node] != _tree.TREE_UNDEFINED: name = feature_name[node] threshold = tree_.threshold[node] if name == disease_input: val = 1 else: val = 0 if val <= threshold: recurse(tree_.children_left[node], depth + 1) else: symptoms_present.append(name) recurse(tree_.children_right[node], depth + 1) else: present_disease = print_disease(tree_.value[node]) red_cols = reduced_data.columns symptoms_given = red_cols[reduced_data.loc[present_disease].values[0].nonzero()] #engine.say("Are you experiencing any") #engine.runAndWait() print("Are you experiencing any ") symptoms_exp=[] for syms in list(symptoms_given): inp="" # engine.say(f"{syms}, are you experiencing it?") #engine.runAndWait() print(syms,"? : ",end='') while True: inp=input("") if(inp=="yes" or inp=="no"): break else: print("provide proper answers i.e. (yes/no) : ",end="") if(inp=="yes"): symptoms_exp.append(syms) second_prediction=sec_predict(symptoms_exp) calc_condition(symptoms_exp,num_days) if(present_disease[0]==second_prediction[0]): # engine.say("You may have ", present_disease[0]) #engine.runAndWait() print("You may have ", present_disease[0]) print(description_list[present_disease[0]]) else: print("You may have ", present_disease[0], "or ", second_prediction[0]) print(description_list[present_disease[0]]) print(description_list[second_prediction[0]]) precution_list=precautionDictionary[present_disease[0]] print("Take following measures : ") for i,j in enumerate(precution_list): print(i+1,")",j) recurse(0, 1) print("----------------------------------------------------------------------------------------------------------------------------------") def respond( message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, ): messages = [{"role": "system", "content": system_message}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) messages.append({"role": "user", "content": message}) response = "" for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = message.choices[0].delta.content response += token yield response """ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface """ demo = gr.ChatInterface( tree_to_code(model,cols), ) if __name__ == "__main__": demo.launch()