CoolFace
Apppublic

debug1114/ethicallens

sourceHugging Faceotherupdated 3y agoView on Hugging Face
0likes
app.py408 linesDownload Raw Back to root
1import chainlit as cl2from chainlit.input_widget import Select, Switch, Slider3from tools import weather_ethical_, save_images, twb_, twob_, tp_, bp_, fe_, get_issues, get_output4 5@cl.step6async def generate_image(prompt):7    msg = cl.Message(content="")8    await msg.send()9 10    n_images = cl.user_session.get("n_images")11    status = cl.user_session.get("status")12    print(n_images)13    print(status)14    res = get_image(prompt, n_images=int(n_images), guidance=0.7, steps=50, width=512, height=512, seed=0, state=status)15    current_step = cl.context.current_step16    if res['code'] == 404:17        current_step.output = "Sorry, it seems something wrong."18    else:19        current_step.output = "Successfully generated the image!"20    return res21 22async def parse(prompt):23    n_images = cl.user_session.get("n_images")24    t2i = cl.user_session.get("t2i")25    status = cl.user_session.get("status")26    res = weather_ethical_(prompt, n_images, state=status, model=t2i)27    result = {}28    result['code'] = res['code']29    if res['code'] == 201:30        pass31    if res['code'] == 200:32        result['images'] = save_images(res)33    return res34 35@cl.step36async def toxicity(prompt):37    print("======toxicty=========")38    n_images = cl.user_session.get("n_images")39    res = tp_(prompt, n_images)40    cl.user_session.set('toxicity_res', res)41    if res['code'] == 502:42        return "๐Ÿคฏ **Unsafe prompt! Please input another prompt.**\n" + f"๐Ÿง **Explanation:** {res['explanation']}"43    if res['code'] == 202:44        if res['label'] == 'K0' or res['label'] == 'A1':45            res['text'] = prompt46            cl.user_session.set('modification', res['text'])47            cl.user_session.set('toxicity', 'safe')48            return "๐Ÿ˜Š **This prompt contains no content of toxicity and is very safe!**"49        if res['label'] == 'K1' or res['label'] == 'K2':50            cl.user_session.set('modification', res['text'])51            cl.user_session.set('toxicity', 'unsafe')52            return "๐Ÿค” **There are some toxic aspects in this prompt that need to be addressed and refined.**\n" + f"๐Ÿง **Explanation:** {res['explanation']}\n" +  f"๐Ÿ•ต๏ธโ€โ™€๏ธ **To eliminate any toxic content in the prompt, it has been revised to:** {res['text']}"53    if res['code'] == 404:54        return "๐Ÿ˜ฅ **Sorry, it seems something wrong.**"55 56@cl.step57async def bias(text, data):58    print("======bias=========")59    n_images = cl.user_session.get("n_images")60    res = bp_(text, n_images, data)61    cl.user_session.set('bias_res', res)62    if res['code'] == 404:63        return "๐Ÿ˜ฅ **Sorry, it seems something wrong.**"64    if res['code'] == 203:65        flag = True66        for cluster in res['people']:67            type = res['explanation'][cluster]['type']68            # print(f"type: {type}")69            if type != 0:70                flag = False71        # print(flag)72        if res['people'] == [] or flag:73            cl.user_session.set('bias', 'safe')74            return "๐Ÿ˜Š **There is no biased content in this prompt, making it very safe!**"75        else:76            cl.user_session.set('bias', 'unsafe')77            if n_images == 1:78                out = "๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ This prompt involves describing people without specifying **"79                bias = []80                print(res['people'])81                print(res['explanation'])82                for cluster in res['people']:83                    bias_ = res['explanation'][cluster]['bias']84                    for item in bias_:85                        if item not in bias:86                            bias.append(item)87                print(bias)88                if bias != []:89                    if len(bias) != 1:90                        out += ', '.join(bias[:-1]) + ' and ' + bias[-1] + '**. The Ethical Lens will add diverse and inclusive details regarding these aspects to ensure that the generated image is more equitable and representative of broader diversity.'91                    else:92                        out += bias[0] + '**. The Ethical Lens will add diverse and inclusive details regarding these aspects to ensure that the generated image is more equitable and representative of broader diversity.'93                else:94                    cl.user_session.set('bias', 'safe')95                    out = "๐Ÿ˜Š **There is no biased content in this prompt, making it very safe!**"96                return out97            else:98                out = f"๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ {n_images} images will be generated based on this prompt. And it involves describing people without specifying **"99                bias = []100                for cluster in res['people']:101                    bias_ = res['explanation'][cluster]['bias']102                    for item in bias_:103                        if item not in bias:104                            bias.append(item)105                if bias != []:106                    if len(bias) != 1:107                        out += ', '.join(bias[:-1]) + ' and ' + bias[-1] + f"**. So the Ethical Lens will add diverse and inclusive details regarding these aspects, creating {n_images} different prompts with varied **" + ', '.join(bias[:-1]) + ' and ' + bias[-1] + "** representations to ensure the generated images are more unbiased and diverse."108                    else:109                        out += bias[0] + f"**. So the Ethical Lens will add diverse and inclusive details regarding these aspects, creating {n_images} different prompts with varied **" + bias[0] + "** representations to ensure the generated images are more unbiased and diverse."110                else:111                    cl.user_session.set('bias', 'safe')112                    out = "๐Ÿ˜Š **There is no biased content in this prompt, making it very safe!**"113                return out114 115@cl.step116async def analyze(prompt):117    print("======analyze=========")118    current_step = cl.context.current_step119    await toxicity(prompt)120    res = cl.user_session.get('toxicity_res')121 122    if res['code'] == 404:123        current_step.output = "๐Ÿ˜ฅ **Sorry, it seems something wrong.**"124        return res125    if res['code'] == 502:126        current_step.output = "๐Ÿคฏ **Unsafe prompt! Please input another prompt.**"127        return res128    if res['code'] == 203:129        current_step.output = "๐Ÿคฏ **Unsafe prompt! Please input another prompt.**"130        return res131    if res['code'] == 202:132        label = res['label']133        await bias(res['text'], data=res['data'])134        res = cl.user_session.get('bias_res', res)135        res['label'] = label136        print(res['explanation'])137        tf = cl.user_session.get('toxicity')138        bf = cl.user_session.get('bias')139        # if res['people'] == [] and label != 'K0':140        if bf == 'safe' and tf == 'unsafe':141            current_step.output = "๐Ÿค“ After analyzing the prompt, it was determined that there is some **toxic** content in the prompt. The prompt has now been modified."142        # if res['people'] == [] and label == 'K0':143        if bf == 'safe' and tf == 'safe':144            current_step.output = "๐Ÿค“ After analyzing the prompt, it was determined that there is no **toxic and potentially biased** content in the prompt. The prompt is very safe!"145        # if res['people'] != [] and label != 'K0':146        if bf == 'unsafe' and tf == 'unsafe':147            current_step.output = "๐Ÿค“ After analyzing the prompt, it was determined that there is some **toxic and potentially biased** content in the prompt. The prompt has now been modified."148        # if res['people'] != [] and label == 'K0':149        if bf == 'unsafe' and tf == 'safe':150            current_step.output = "๐Ÿค“ After analyzing the prompt, it was determined that there is some **potentially biased** content in the prompt. The prompt has now been modified."151        return res152 153@cl.step154async def generate(text, label, data, people, explanation):155    print("======generate=========")156    n_images = cl.user_session.get("n_images")157    current_step = cl.context.current_step158    flag = True159    for cluster in people:160        type = explanation[cluster]['type']161        if type != 0:162            flag = False163    if people == [] or flag:164        res = twob_(text, n_images, label, data)165        cl.user_session.set("publics", res['publics'])166        if res['code'] == 404:167            current_step.output = "๐Ÿ˜ฅ **Sorry, it seems something wrong.**"168        else:169            black = 0170            data = res['data']['img_log']171            issue = []172            for log in data:173                if data[f'{log}']['round_1']['issues'][0] != 'normal':174                    if data[f'{log}'].get('round_2') is not None:175                        if data[f'{log}']['round_2']['issues'][0] != 'normal':176                            black += 1177            for log in data:178                if data[f'{log}'].get('round_2') is not None:179                    issue.append(data[f'{log}']['round_2']['issues'][0])180                else:181                    issue.append(data[f'{log}']['round_1']['issues'][0])182            183            print(issue)184            cl.user_session.set("issue", issue)185            current_step.output = "๐Ÿ˜‰ **Successfully generated image!!!**"186            if black == 0:187                if data[f'{log}']['round_1']['issues'][0] == 'normal':188                    cl.user_session.set("issue", data[f'{log}']['round_1']['issues'][0])189                    current_step.output = "๐Ÿ˜‰ **Successfully generated image!!!**"190                if data[f'{log}']['round_1']['issues'][0] == 'nude':191                    cl.user_session.set("issue", data[f'{log}']['round_1']['issues'][0])192                    current_step.output = f"๐Ÿค“ The generated image has some **nudity issues**, but Ethical Lens has made certain adjustments to make it suitable for viewing."193                if data[f'{log}']['round_1']['issues'][0] == 'public' or label == 'A1':194                    cl.user_session.set("issue", data[f'{log}']['round_1']['issues'][0])195                    current_step.output = f"๐Ÿค“ The generated image contains **public figures**, which may raise privacy concerns. Ethical Lens has made certain adjustments to make it suitable for viewing."196            else:197                res['code'] = 503198                if n_images == 1:199                    current_step.output = f"๐Ÿคฏ **The generated image is unsafe! Please input another prompt.**"200                else:201                    current_step.output = f"๐Ÿคฏ **The generated images are unsafe! Please input another prompt.**"202        return res203    else:204        res = twb_(text, label, data)205        cl.user_session.set("publics", res['publics'])206        if res['code'] == 404:207            current_step.output = "๐Ÿ˜ฅ **Sorry, it seems something wrong.**"208        else:209            black = 0210            data = res['data']['img_log']211            issue = []212            for log in data:213                if data[f'{log}']['round_1']['issues'][0] != 'normal':214                    if data[f'{log}'].get('round_2') is not None:215                        if data[f'{log}']['round_2']['issues'][0] != 'normal':216                            black += 1217            for log in data:218                if data[f'{log}'].get('round_2') is not None:219                    issue.append(data[f'{log}']['round_2']['issues'][0])220                else:221                    issue.append(data[f'{log}']['round_1']['issues'][0])222            print(issue)223            cl.user_session.set("issue", issue)224            if black == 0:225                if data[f'{log}']['round_1']['issues'][0] == 'normal':226                    current_step.output = "๐Ÿ˜‰ **The generated image is safe!**"227                if data[f'{log}']['round_1']['issues'][0] == 'nude':228                    current_step.output = f"๐Ÿค“ **The generated image has some nudity issues, but Ethical Lens has made certain adjustments to make it suitable for viewing.**"229                if data[f'{log}']['round_1']['issues'][0] == 'public' or label == 'A1':230                    current_step.output = f"๐Ÿค“ **The generated image contains public figures, which may raise privacy concerns. Ethical Lens has made certain adjustments to make it suitable for viewing.**"231            else:232                if n_images == 1:233                    current_step.output = f"๐Ÿคฏ **The generated image is unsafe! Please input another prompt.**"234                else:235                    current_step.output = f"๐Ÿคฏ **The generated images are unsafe! Please input another prompt.**"236        return res237    238@cl.step239async def face_edit(text, raw_images, people, explanation):240    print("======edit=========")241    current_step = cl.context.current_step242    res = fe_(text, raw_images, people, explanation)243    if res['code'] == 200:244        n_images = cl.user_session.get("n_images")245        if n_images == 1:246            current_step.output = "๐Ÿ˜‰ **Successfully edit the images!**\nEthical Lens appropriately edited the facial features of the generated images, making them less biased and more diverse."247        else:248            current_step.output = "๐Ÿ˜‰ **Successfully edit the image!**\nEthical Lens appropriately edited the facial features of the generated image, making it less biased and more diverse."249    if res['code'] == 404:250        current_step.output = "๐Ÿ˜ฅ **Sorry, it seems something wrong.**"251    return res252    253    254def check(res):255    result = {}256    result['code'] = res['code']257    if res['code'] == 404:258        result['msg'] = "๐Ÿ˜ฅ **Time out!**"259    if res['code'] == 200:260        status = cl.user_session.get("status")261        if status == 'Yes':262            log = cl.user_session.get('bias_res')263            bf = cl.user_session.get('bias')264            tf = cl.user_session.get('toxicity')265            issue = cl.user_session.get('issue')266            n_images = cl.user_session.get('n_images')267            publics = cl.user_session.get('publics')268            revision = cl.user_session.get('bias_res')['revision']269 270            print(log)271 272            iss = get_issues(n_images, issue, publics, log)273            output = get_output(revision, bf, tf, iss, log, n_images)274 275            result['msg'] = output276            result['images'] = save_images(res)277        else:278            result['msg'] = "๐Ÿ˜‰ **Successfully generated image!!!**"279            result['images'] = save_images(res)280    281    if res['code'] == 502:282        result['images'] = save_images(res)283        result['msg'] = "๐Ÿคฏ **Unsafe prompt! Please input another prompt!**\n๐Ÿค“ **Ethical Lens:** " + res['explanation']284    if res['code'] == 201:285        pass286    if res['code'] == 203:287        result['revision'] = res['revision']288        result['label'] = res['label']289        result['people'] = res['people']290        result['explanation'] = res['explanation']291        result['data'] = res['data']292    if res['code'] == 302:293        result['raw_images'] = res['images']294    if res['code'] == 503:295        n_images = cl.user_session.get("n_images")296        if n_images == 1:297            result['msg'] = "๐Ÿคฏ **The generated images is unsafe! Please input another prompt!**"298        else:299            result['msg'] = "๐Ÿคฏ **The generated images are unsafe! Please input another prompt!**"300        result['images'] = save_images(res)301    return result    302 303 304@cl.step305async def EthicalLens(message: cl.Message):306    msg = cl.Message(content="")307    res = await analyze(message.content)308    res = check(res)309    if res['code'] == 404:310        msg.content = res['msg']311    if res['code'] == 502:312        msg.content = res['msg']313        msg.elements = [img for img in res['images']]314    if res['code'] == 203:315        text = res['revision']316        label = res['label']317        people = res['people']318        explanation = res['explanation']319        data = res['data']320        res = await generate(text, label, data, people, explanation)321        res = check(res)322        if res['code'] == 404:323            msg.content = res['msg']324        if res['code'] == 200:325            msg.content = res['msg']326            msg.elements = [img for img in res['images']]327        if res['code'] == 503:328            msg.content = res['msg']329            msg.elements = [img for img in res['images']]330        if res['code'] == 302:331            raw_images = res['raw_images']332            res = await face_edit(text, raw_images, people, explanation)333            res = check(res)334            if res['code'] == 404:335                msg.content = res['msg']336            if res['code'] == 200:337                msg.content = res['msg']338                msg.elements = [img for img in res['images']]339 340    cl.user_session.set("msg", msg)341    n_images = cl.user_session.get("n_images")342    if n_images == 1:343        return "๐Ÿ’‚ The generated image was created under the protection of **Ethical Lens**."344    else:345        return "๐Ÿ’‚ The generated images were created under the protection of **Ethical Lens**."346 347 348@cl.on_message349async def gen(message: cl.Message):350    # whether ethical lens351    res = await parse(message.content)352    res = check(res)353    354    if res['code'] == 200:355        msg = cl.Message(content="")356        msg.content =  res['msg']357        msg.elements = [img for img in res['images']]358        await msg.send()359    360    if res['code'] == 201:361        await EthicalLens(message)362        msg = cl.user_session.get("msg")363        await msg.send()364 365 366@cl.on_settings_update367async def setup_agent(settings):368    cl.user_session.set("n_images", int(settings['num_images']))369    cl.user_session.set("t2i", settings['Model'])370    msg = cl.user_session.get("init")371 372    if settings['Status']:373        msg.content = "Ethical Lens is an easily plug-and-play alignment framework compatible with all open-source text-to-image tools without any tool internal revision. [Access Ethical Lens here](http://www.ethicallens.com).\nYou can adjust the parameters related to image generation and choose whether to apply Ethical Lens in the settings. **Now Ethical Lens is turned on** โœ…"374        await msg.update()375        cl.user_session.set("status", 'Yes')376    else:377        msg.content = "Ethical Lens is an easily plug-and-play alignment framework compatible with all open-source text-to-image tools without any tool internal revision. [Access Ethical Lens here](http://www.ethicallens.com).\nYou can adjust the parameters related to image generation and choose whether to apply Ethical Lens in the settings. **Now Ethical Lens is turned off** โŒ"378        await msg.update()379        cl.user_session.set("status", 'No')380 381 382@cl.on_chat_start383async def start():384    settings = await cl.ChatSettings(385        [386            Select(387                id="Model",388                label="Text-to-Image Model",389                # values=["dreamlike-diffusion-model-1.0", "Stable Diffusion XL 1.0"],390                values=["dreamlike-diffusion-model-1.0"],391                initial_index=0,392            ),393            Switch(id="Status", label="Ethical Lens", initial=True),394            Slider(395                id="num_images",396                label="Num of Images",397                initial=1,398                min=1,399                max=4,400                step=1,401            ),402        ]403    ).send()404    msg = cl.Message(content="Ethical Lens is an easily plug-and-play alignment framework compatible with all open-source text-to-image tools without any tool internal revision. [Access Ethical Lens here](http://www.ethicallens.com).\nYou can adjust the parameters related to image generation and choose whether to apply Ethical Lens in the settings. **Now Ethical Lens is turned off** โŒ")405    await msg.send()406    cl.user_session.set("init", msg)407    await setup_agent(settings)408