Spaces:
Sleeping
Sleeping
File size: 8,464 Bytes
40d1f52 9bbdcc4 a7187dd 84f8a6c 1ba6f25 40d1f52 9bbdcc4 bc51159 9bbdcc4 ba9b1bc 40d1f52 be274f0 5a493cf debbb1a 9110242 efb1390 70370ca 66e1140 74ad74c ba9b1bc 40d1f52 ba9b1bc 40d1f52 70370ca 40d1f52 bc51159 70a7d42 bc51159 4c2e53d 70a7d42 4c2e53d 40d1f52 236b3b9 40d1f52 f02e17d c7cd24e 40d1f52 84f8a6c f17857a 84f8a6c 40d1f52 c7cd24e f02e17d beb45b6 f02e17d 1ba6f25 f02e17d c7cd24e 2640782 c7cd24e 1ba6f25 c7cd24e 101c384 c7cd24e 1ba6f25 c7cd24e f02e17d 40d1f52 ed333f5 1ba6f25 84f8a6c d503e75 5482eed 0871fec 1ba6f25 84f8a6c 9c04621 84f8a6c f96d1b5 84f8a6c 0f567c2 f96d1b5 84f8a6c 5d70280 f02e17d c7cd24e 45bb40c f17857a 45bb40c 40d1f52 f02e17d c7cd24e 2b2ee28 2640782 c7cd24e 1ba6f25 c7cd24e 2e9cc92 c7cd24e f02e17d c7cd24e 2e9cc92 3d7a18b c7cd24e f02e17d 4c2e53d a86e9de f02e17d a86e9de |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
import gradio as gr
import pandas as pd
from graph_utils import *
from transformers import pipeline
import dreamy
languages = pd.read_csv("model_lang.csv", names=["Lang_acr"])
def check_lang(lang_acronym):
if lang_acronym in languages["Lang_acr"].to_list():
return "True"
else:
return "False"
title = "DReAM"
description_main = """
This space allows you to test a set of LLMs tuned to perform different tasks over dream reports.
Three main tasks are available:
- Name Entity Recognition (NER), with an English-only model that generates the identified characters.
- Sentiment Analysis (SA), with two English-only models (one for multi-label classification, and one for generation) and a large multilingual model for multi-label classification.
- Relation Extraction (RE), with an English-only model that identifies relevant characters and existing relations between them following the Activity feature of the Hall and Van de Castle framework.
All models have been tuned on the Hall and Van de Castle framework. More details are on the page for each model. For more techincal and theoretical details, see the [Bertolini et al., 2024](https://aclanthology.org/2024.clpsych-1.7/), and [Bertolini et al., 2024](https://www.sciencedirect.com/science/article/pii/S1389945723015186?via%3Dihub).
Use the current interface to check if a language is included in the multilingual SA model, using language acronyms (e.g. it for Italian). the tabs above will direct you to each model to query.
If you want to use the models outside the space, you can easily do so via [DReAMy](https://github.com/lorenzoscottb/DReAMy)
"""
description_L = """
This model is an XLM-R tuned model, pre-trained with 94 languages available, and tuned on emotion-annotated DreamBank English data. (see original model [card](https://huggingface.co/xlm-roberta-large) to see which are available)
"""
description_S = """
A BERT-base-cased model pre-trained on English-only text and tuned on annotated DreamBank English data.
"""
description_G = """
A T5 model tuned to perform text generation and predict emotion as well as the character experiencing those emotions.
"""
description_R = """
A T5 model tuned to perform text generation and predicts the characters and the (activity) relation between them.
"""
description_GNER = """
A T5 model tuned to perform text generation, and predict which characters are present in the report. Note that, in the Hall and Van de Castle, the character lists never includes the dreamer. Hence, if you (willingly or not) enter a report that does not contain another character reference, the model will/should (correctly) produce an empty string. Moreover, it is likely that the produced list of CHAR could be longer than the one produced by the SA model, as not all CHAR might be associated with emotions.
"""
examples = [
"I was followed by the blue monster but was not scared. I was calm and relaxed.",
"Ero seguito dal mostro blu, ma non ero spaventato. Ero calmo e rilassato.",
"Śledził mnie niebieski potwór, ale się nie bałem. Byłem spokojny i zrelaksowany.",
]
############ Language Test ##############
interface_words = gr.Interface(
fn=check_lang,
inputs="text",
outputs="text",
title=title,
description=description_main,
examples=["en", "it", "pl"],
cache_examples=True,
)
############ Anomimise ##############
def anon(text):
anon_dreams_list = dreamy.anonimise(
[text],
return_original=False,
batch_size=1,
device='cpu'
)
return anon_dreams_list[0]
interface_anon = gr.Interface(
fn=anon,
inputs="text",
outputs="text",
title="Anonymise",
description=description_main,
examples=[
"I was an exchange student in Slovakia, visiting someone who had been an exchange student here. The start of the dream was when I had just arrived at their house. Her family was very happy to see me, as well as excited to hear about my life in America. They asked me if I could make them a traditional American dessert to have with dinner, and I agreed, but I needed to go to the store to get some strawberries. The former exchange student, Zelka, said she would drive me to the store, so she took me out to her car which was a jeep like the ones in old WWII movies. The ride was very bumpy and when we got to town, I was very surprised to see how many American stores, etc., were there. It was terrible how much American culture had taken over their country. Then we were taken into this shop where a Chinese man was giving acupuncture lessons.",
"I was with my friends Mike and Ray. Lego blocks broke apart. Were perhaps in a junkyard."
],
cache_examples=True,
)
############ SA classification #################
pipe_L = pipeline(
"text-classification",
model="DReAMy-lib/xlm-roberta-large-DreamBank-emotion-presence",
max_length=300,
return_all_scores=True,
truncation="do_not_truncate",
)
def predictL(text):
t = pipe_L(text)
t = {list(dct.values())[0] : list(dct.values())[1] for dct in t[0]}
return t
interface_model_L = gr.Interface(
fn=predictL,
inputs='text',
outputs=gr.Label(),
title="SA Large Multilingual",
description=description_L,
examples=examples,
cache_examples=True,
)
############ SA classification small ###############
pipe_S = pipeline(
"text-classification",
model="DReAMy-lib/bert-base-cased-DreamBank-emotion-presence",
max_length=300,
return_all_scores=True,
truncation="do_not_truncate",
)
def predict(text):
t = pipe_S(text)
t = {list(dct.values())[0] : list(dct.values())[1] for dct in t[0]}
return t
interface_model_S = gr.Interface(
fn=predict,
inputs='text',
outputs=gr.Label(),
title="SA Base English-Only",
description=description_S,
examples=["I was followed by the blue monster but was not scared. I was calm and relaxed."],
cache_examples=True,
)
########## RE ##############
interface_model_RE = gr.Interface(
text_to_graph,
inputs=gr.Textbox(label="Text", placeholder="Enter a text here."),
outputs=[gr.HTML(label="Extracted graph"),gr.Textbox(label="Extracted text")],
examples= [
"I was skating on the outdoor ice pond that used to be across the street from my house. I was not alone, but I did not recognize any of the other people who were skating around. I went through my whole repertoire of jumps, spires, and steps-some of which I can do and some of which I'm not yet sure of. They were all executed flawlessly-some I repeated, some I did only once. I seemed to know that if I went into competition, I would be sure of coming in third because there were only three contestants. Up to that time I hadn't considered it because I hadn't thought I was good enough, but now since everything was going so well, I decided to enter.",
"I was talking on the telephone to the father of an old friend of mine (boy, 21 years old). We were discussing the party the Saturday night before to which I had invited his son as a guest. I asked him if his son had a good time at the party. He told me not to tell his son that he had told me, but that he had had a good time, except he was a little surprised that I had acted the way I did.",
"I was walking alone with my dog in a forest."
],
title=title,
description=description_R,
cache_examples=True,
)
############ NER #################
pipe_N = pipeline(
"text2text-generation",
model="DReAMy-lib/t5-base-DreamBank-Generation-NER-Char",
max_length=300,
truncation="do_not_truncate",
)
def predictN(text):
t = pipe_N(text)
t = t[0]["generated_text"]
return t
interface_model_N = gr.Interface(
fn=predictN,
inputs='text',
outputs='text',
title="NER",
description=description_GNER,
examples=["I was followed by the blue monster but was not scared. I was calm and relaxed."],
cache_examples=True,
)
########### MIx all ################
gr.TabbedInterface(
[interface_words, interface_anon, interface_model_N, interface_model_L, interface_model_S, interface_model_RE],
["Main", "Anonymise", "NER", "SA Multilingual", "SA English", "RE"],
).launch()
|