import gradio as gr import math import numpy as np import os import matplotlib.pyplot as plt import random from skimage import io as skio def evolution_plot(current_age): n = 10000 maxi = 100 x = np.linspace(0, maxi, 10000) max_state = 7 final_state = 5 y = 1 / (1 + np.exp(-(x / 8) + 5)) y = (y - np.min(y)) / (np.max(y - np.min(y))) * (final_state - 1) + 1 plt.title("Hair Loss Evolution Prediction") plt.xlabel("Age") plt.ylabel("Nordwood State") plt.ylim(1, max_state) actual = np.where(x < int(current_age))[0][-1] plt.plot(x[:actual], y[:actual]) plt.plot(x[actual:], y[actual:], '--') im_save_path = f'tmp/{random.randint(0, 10000)}.png' plt.savefig(im_save_path) plt.clf() plot = skio.imread(im_save_path) return plot def pad(arr): arr = list(map(str, arr)) max_len = 0 for i in arr: if len(i)>max_len: max_len = len(i) for i in range(len(arr)): for j in range(max_len-len(arr[i])): arr[i] = arr[i] + " " return arr def predict(file, age, parent, gp, shampoo): product = ['Computer', 'Monitor ', 'Laptop ', 'Printer ', 'Tablet '] quantity = pad(np.array([320, 450, 300, 120, 280]) / 500) min_normal = pad(np.array([250, 200, 210, 100, 250]) / 500) max_normal = pad(np.array([400, 300, 450, 150, 300]) / 500) variants = ["Y2TTB9", "9KKGH7", "ML0JH9"] risks = ['Seborrheic eczema', 'Folliculitis', "Pityriasis amiantacea"] percents = ['30', "21", '9'] geneticRisks = "" for variant, risk, percent in zip(variants, risks, percents): geneticRisks += f"Variant {variant} detected, risk of {risk} is {percent}% higher.\n" useful_products = ("Hydrating Shampoo : https://www.thisisthebestshampooforyou.fr/fr-fr/\n" "Light Hat : https://www.amazon.com/light-therapy-hat/s?k=light+therapy+hat") return skio.imread("results.png"), 'Dermatitis\nDryness\nDandruff', geneticRisks, evolution_plot(age), useful_products # GUI title = 'Hair loss prediction' description = 'Metagenomics Scalp Analysis for Hair Loss Prediction' # examples = [[f'examples/{name}', 3] for name in sorted(os.listdir('examples'))] iface = gr.Interface( fn=predict, inputs=[ gr.File(value="tmp/metagenome.txt", type='file', label='Scalp sample'), gr.Textbox(label='Age'), gr.CheckboxGroup(choices=["Yes", "No", "Do not know"], label="Has the father experienced hair loss ?"), gr.CheckboxGroup(choices=["0", "1", "2", "Do not know"], label="How many grand-parents have experienced hair loss ?"), gr.Textbox(label='How many times a week do you wash your hair ?'), ], outputs=[ gr.Image(label='Scalp Metagenomics Analysis Results'), gr.Text(label='Current issues :'), gr.Text(label='Genetic Risks :'), gr.Image(label='Future Evolution'), gr.Text(label='Useful Care Products') ], allow_flagging='never', cache_examples=False, title=title, description=description ) iface.launch()