Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from functools import partial | |
| import csv | |
| import pandas as pd | |
| import ast | |
| from datasets import load_dataset | |
| from huggingface_hub import HfApi, HfFolder | |
| data = pd.read_csv("./KB_gpt_v1.csv",index_col=0) | |
| all_diseases = list(data['diseases']) | |
| js = "(x) => {return alert('File Saved!!')}" | |
| logged_in_users = {} | |
| def login(username, password): | |
| if (username, password) in [("admin", "pass1234"), ("admin2", "pass1234")]: | |
| logged_in_users[username] = True | |
| print(logged_in_users) | |
| return True | |
| else: | |
| return False | |
| def get_symp(symptoms,investigations,drugs,advices): | |
| drug_dict = [] | |
| data_dict = eval(drugs) | |
| print("DType of drugs is ",type(drugs)) | |
| print("Drugs :",drugs) | |
| print("Data dict : ",data_dict) | |
| for cat,med in data_dict.items(): | |
| for i in med: | |
| drug_dict.append(i) | |
| return (gr.update(choices=eval(symptoms),visible=True,interactive=True)),(gr.update(choices=eval(investigations),visible=True,interactive=True)),(gr.update(choices=all,visible=True,interactive=True)),(gr.update(choices=eval(advices),visible=True,interactive=True)) | |
| def get_list(disease): | |
| print(disease) | |
| sympto = ast.literal_eval((data[data['diseases']==str(disease)]).iloc[0][1]) | |
| investigations = ast.literal_eval((data[data['diseases']==str(disease)]['investigations']).iloc[0]) | |
| drugs = ast.literal_eval((data[data['diseases']==str(disease)]["Drug type: Drugs"]).iloc[0]) | |
| advices = ast.literal_eval((data[data['diseases']==str(disease)]["Advices"]).iloc[0]) | |
| return sympto,investigations,drugs,advices | |
| #------------------------------------------------ | |
| # api = HfApi() | |
| # # Replace with your Hugging Face Spaces username and repository name | |
| # username = "Add-Vishnu" | |
| # repo_name = "cdss_kb_generation" | |
| # # Replace with the CSV file name | |
| filename = "demo1.csv" | |
| # # Load the repository | |
| # repo = HfFolder.(repo_name, api=api, username=username) | |
| #--------------------------------------------------------- | |
| def save_data(disease,symp_data,invest_data,drug_data,advice_data): | |
| user = "" | |
| for username, logged_in in logged_in_users.items(): | |
| if logged_in: | |
| user = username | |
| print("In save:",user) | |
| # Read the existing content of the CSV file | |
| # file_path = repo.filename_to_path(filename) | |
| existing_rows = [] | |
| with open(filename, "r") as csv_file: | |
| print("Reading the data") | |
| csv_reader = csv.reader(csv_file) | |
| for row in csv_reader: | |
| existing_rows.append(row) | |
| print(existing_rows) | |
| try: | |
| print("In try") | |
| with open("./demo1.csv", mode="a") as csvfile: | |
| print("In open csv file",csvfile) | |
| csv_writer = csv.writer(csvfile) | |
| csv_writer.writerow([disease,symp_data,invest_data,drug_data,advice_data,user]) | |
| print("Done writing!!") | |
| except: | |
| print("An Exception occured") | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| disease = gr.Dropdown(all_diseases,label="Diseases") | |
| with gr.Row(): | |
| fetch = gr.Button(value="Fetch") | |
| symptoms=gr.Textbox(visible=False) | |
| investigations = gr.Textbox(visible=False) | |
| drugs = gr.Textbox(visible=False) | |
| advices = gr.Textbox(visible=False) | |
| disease.change(get_list,disease,[symptoms,investigations,drugs,advices]) | |
| with gr.Box(): | |
| symptoms_box = gr.CheckboxGroup(label="Symptoms",visible=False) | |
| investigations_box = gr.CheckboxGroup(label="Investigations",visible=False) | |
| # drugs_box = gr.Box(visible=False) | |
| drugs_box = gr.CheckboxGroup(label="Drugs",visible=False) | |
| advices_box = gr.CheckboxGroup(label="Advices",visible=False) | |
| save = gr.Button(value="Save") | |
| fetch.click(get_symp, inputs=[symptoms,investigations,drugs,advices],outputs=[symptoms_box,investigations_box,drugs_box,advices_box]) | |
| save.click(save_data,inputs=[disease,symptoms_box,investigations_box,drugs_box,advices_box],outputs=[]).then(fn=None,_js=js) | |
| if __name__ == "__main__": | |
| demo.launch(auth=login) | |