# 1. Import the required packages import torch import gradio as gr from typing import Dict from transformers import pipeline # 2. Define function to use our model on given text def trait_classifier(text: str) -> Dict[str, float]: # Set up text classification pipeline agr_classifier = pipeline(task="text-classification", # Because our model is on Hugging Face already, we can pass in the model name directly model="kar0lina/petarda_xlm-roberta-base_pandora1000-agr", # link to model on HF Hub device="cuda" if torch.cuda.is_available() else "cpu", top_k=None) # return all possible scores (not just top-1) con_classifier = pipeline(task="text-classification", # Because our model is on Hugging Face already, we can pass in the model name directly model="kar0lina/petarda_xlm-roberta-base_pandora1000-con", # link to model on HF Hub device="cuda" if torch.cuda.is_available() else "cpu", top_k=None) # return all possible scores (not just top-1) ext_classifier = pipeline(task="text-classification", # Because our model is on Hugging Face already, we can pass in the model name directly model="kar0lina/petarda_xlm-roberta-base_pandora1000-ext", # link to model on HF Hub device="cuda" if torch.cuda.is_available() else "cpu", top_k=None) # return all possible scores (not just top-1) neu_classifier = pipeline(task="text-classification", # Because our model is on Hugging Face already, we can pass in the model name directly model="kar0lina/petarda-pandora1000-neu-xlm-roberta-base", # link to model on HF Hub device="cuda" if torch.cuda.is_available() else "cpu", top_k=None) # return all possible scores (not just top-1) ope_classifier = pipeline(task="text-classification", # Because our model is on Hugging Face already, we can pass in the model name directly model="kar0lina/petarda_xlm-roberta-base_pandora1000-ope", # link to model on HF Hub device="cuda" if torch.cuda.is_available() else "cpu", top_k=None) # return all possible scores (not just top-1) # Get outputs from pipeline (as a list of dicts) outputs_agr = agr_classifier(text)[0] outputs_con = con_classifier(text)[0] outputs_ext = ext_classifier(text)[0] outputs_neu = neu_classifier(text)[0] outputs_ope = ope_classifier(text)[0] print("outputs_agr: ", outputs_agr) print("outputs_con: ", outputs_con) print("outputs_ext: ", outputs_ext) print("outputs_neu: ", outputs_neu) print("outputs_ope: ", outputs_ope) # Format output for Gradio (e.g. {"label_1": probability_1, "label_2": probability_2}) output_dict = {} #agr if outputs_agr[0]['label'] == 'POSITIVE': output_dict['Agreeablenes'] = outputs_agr[0]["score"] else: output_dict['Agreeablenes'] = outputs_agr[1]["score"] #con if outputs_con[0]['label'] == 'POSITIVE': output_dict['Consiousness'] = outputs_con[0]["score"] else: output_dict['Consiousness'] = outputs_con[1]["score"] #ext if outputs_ext[0]['label'] == 'POSITIVE': output_dict['Extraversion'] = outputs_ext[0]["score"] else: output_dict['Extraversion'] = outputs_ext[1]["score"] #neu if outputs_neu[0]['label'] == 'POSITIVE': output_dict['Neuroticism'] = outputs_neu[0]["score"] else: output_dict['Neuroticism'] = outputs_neu[1]["score"] #ope if outputs_ope[0]['label'] == 'POSITIVE': output_dict['Openness'] = outputs_ope[0]["score"] else: output_dict['Openness'] = outputs_ope[1]["score"] # output_dict = {'neu': probability_1, 'ext': probability_2, 'ope': # probability_3} return output_dict # 3. Create a Gradio interface with details about our app description = """ 🥰😌🫣😨🤯 A text classifier for PErsonality Trait prediction using Ai model Roberta - Demo App. Fine-tuned from [xlm-roberta-base](https://huggingface.co/FacebookAI/xlm-roberta-base) on Pandora dataset""" demo = gr.Interface(fn=trait_classifier, inputs="text", outputs=gr.Label(num_top_classes=5, label='Personality traits', show_heading=False, show_label=True), title="🤯 Petarda", theme=gr.themes.Ocean(primary_hue="amber", secondary_hue="cyan"), description=description, examples=[["Well I don\'t know it going into it. I don\'t just assume people are dumb. I give them a chance. I make them prove if they are smart of dumb. In this case she had a biology degree and I was younger and thought degree = smart. That couldn't be more wrong lol. Plus her banter was kind of fun."], ["And now you\'re asking very hard questions that I\'ve struggled with quite a bit… Both of those can be a struggle. My memory is very hard to access because I just can\'t remember things in any level of detail... faces, conversations, events, my own actions, or unstructured facts. My senses can be hard to access as well because I keep pulling back into my own head. I don\t feel like I\'m grounded in myself or that I have a consistent behavior. I\'m stateless. It\'s as if everything is new to me all the time. I can barely maintain a routine. At the same time, empathizing and really knowing what to do naturally in social situations is really a problem for me as well. I seem to get by and people generally like me, but I\'m very careful and I can\'t always relate to how other people are acting. Sometimes it seems animalistic, simple-minded, and petty. Depends... if there\'s something that I\'m interested in being discussed or done, then I am very, very energetic, passionate, and reactive. My energy levels can swing from one extreme to another depending on my level of interest. If not, then I\'m extremely withdrawn and in my own head."], ["When I get really pissed, I go out for a drive by myself, blast the music, and vent to myself out loud, talking through whatever it is until I eventually calm down. If I want to relax, I go out for a walk somewhere. Usually I'll go to the lake, I like to be near bodies of water for some reason."], ["That high school wouldn't be as scary as I thought it would be. Take a deep breath, relax, and don't sweat the small stuff. This is your last time to be able to be as carefree as you are. Enjoy being a teenager, no matter how awkward it is. It'll be ok."], ["there's always these posts about customers wanting you to open early... but I went to a store this morning and had to wait 20mins before entering AFTER their posted \"open\" time. ;/ The employee didn't even show up till after I was there, and took her time getting everything set before I was allowed in ;/"], ["oh yeah... Summer Games! That one was great. I never owned one as a kid, so when I saw the C64 at an old folk's garage sale, I just had to snatch it up... that was like 10+ years ago, and I fired it up maybe half a dozen times. I never did get around to figuring out how to hook up the 1541 disk drive to a regular pc so I could load it up with a buncha games either."], ["oh yeah there's nothing wrong with that kinda look. Though i'm just saying a knife like that, you can get for $80. I don't buy knives to keep and display either. I've used $600 knives, $1200 swords, doesn't bother me any. not gonna sell 'em so what do I care about the price once i've bought them."], ["I love listening to her while I do the dishes. My hubby loves it because sometimes it devolves into a concert because I sing along haha"], ["This is exactly what my corgi does! If you get up before he's ready he grumps and goes back to sleep. If you wake up when he wakes up he's bright eyed and bushy nubbined and ready for cuddles. He'll hang out if we're lazing in bed but he gets *the look* like we're horrible if we don't pop right up to take him out. Also a notorious sock fighter. Corgis are awesome lol"], ["I think it's possible, especially if you're in a relationship where you see marriage potential and you talk to your SO about your feelings... I think with some planning and the support of a partner with the same want for a traditional family it's totally doable to live that dream! My mom told me once that one of her biggest regrets in life is that she didn't stay at home to take care of my brother and I when we were little."], ["I *love* going to Renaissance Festivals! I have a full costume and thoroughly enjoy myself every time I go :) I'm glad you had a great time! I recommend investing in some garb if you decide you'd like to go more regularly, it's so much fun to be a part of it all."], ["That was my yesterday. I woke up today thinking maybe ill be okay today, but nope. As I listen to everything over and over, I keep remembering moments in my life where I was listening to the song in the past. I'm 28 but it feels like they've truly been with me my whole life."], ["Dorms is good for a year or two when you\'re young. I didn\'t mind it at first but at some point I HAD to have my own place. There\'s a time and a place for dorms. I plan on going back to college eventually though, and I\'ll be in my mid 30s by then, and honestly, I\'d consider moving back into the dorms (if I don\'t have a dog by then) because you get the cafeteria and the closeness to classes"], ["amazing, absolutely amazing. don't worry about robbery too much, if you don't trust somebody don't show them interest. keep your head on, and if you start messin with drugs and booze, know your limit, talk with your body a lot. for me, if somebody's smokin bud, talk them up for a little, then accept if you want. for the most part, don't accept white powders. bring plenty good food, food that will nourish you. it's the shit man."], ["looking good buddy! this is the kind of body i'm aiming for, it's just a matter of lowering body fat. not too easy. don't forget your delts and pecs my good man."], ]) # 4. Launch the interface if __name__ == "__main__": demo.launch(share=True)