Team Ai
Apppublic

AlGe/PosterDemoSequenceClassificationBinary

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py199 linesDownload Raw Back to root
1from __future__ import annotations2from typing import Iterable, List, Dict, Tuple3 4import gradio as gr5from gradio.themes.base import Base6from gradio.themes.soft import Soft7from gradio.themes.monochrome import Monochrome8from gradio.themes.default import Default9from gradio.themes.utils import colors, fonts, sizes10 11import spaces12import torch13import os14import io15import re16import colorsys17 18import numpy as np19 20from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, pipeline21 22import matplotlib.pyplot as plt23import plotly.graph_objects as go24from wordcloud import WordCloud25from PIL import Image26 27bin_color_map = {28    "External": "#0080ff",29    "Internal": "#df2020"30}31 32def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:33    hex_color = hex_color.lstrip('#')34    return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))35 36def rgb_to_hex(rgb_color: tuple[int, int, int]) -> str:37    return "#{:02x}{:02x}{:02x}".format(*rgb_color)38 39def adjust_brightness(rgb_color: tuple[int, int, int], factor: float) -> tuple[int, int, int]:40    hsv_color = colorsys.rgb_to_hsv(*[v / 255.0 for v in rgb_color])41    new_v = max(0, min(hsv_color[2] * factor, 1))42    new_rgb = colorsys.hsv_to_rgb(hsv_color[0], hsv_color[1], new_v)43    return tuple(int(v * 255) for v in new_rgb)44 45monochrome = Monochrome()46 47auth_token = os.environ['HF_TOKEN']48 49tokenizer_bin = AutoTokenizer.from_pretrained("AlGe/deberta-v3-large_token", token=auth_token)50model_bin = AutoModelForTokenClassification.from_pretrained("AlGe/deberta-v3-large_token", token=auth_token)51tokenizer_bin.model_max_length = 51252pipe_bin = pipeline("ner", model=model_bin, tokenizer=tokenizer_bin)53 54def process_ner(text: str, pipeline) -> dict:55    output = pipeline(text)56    entities = []57    current_entity = None58 59    for token in output:60        entity_type = token['entity'][2:]61        entity_prefix = token['entity'][:1]62 63        if current_entity is None or entity_type != current_entity['entity'] or (entity_prefix == 'B' and entity_type == current_entity['entity']):64            if current_entity is not None:65                entities.append(current_entity)66            current_entity = {67                "entity": entity_type,68                "start": token['start'],69                "end": token['end'],70                "scores": [token['score']],71                "tokens": [token['word']]72            }73        else:74            current_entity['end'] = token['end']75            current_entity['scores'].append(token['score'])76            current_entity['tokens'].append(token['word'])77 78    if current_entity is not None:79        entities.append(current_entity)80 81    return {"text": text, "entities": entities}82 83import os84 85def generate_charts(ner_output_bin: dict) -> Tuple[go.Figure, np.ndarray]:86    entities_bin = [entity['entity'] for entity in ner_output_bin['entities']]87 88    # Counting entities for binary classification89    entity_counts_bin = {entity: entities_bin.count(entity) for entity in set(entities_bin)}90    bin_labels = list(entity_counts_bin.keys())91    bin_sizes = list(entity_counts_bin.values())92 93    bin_colors = [bin_color_map.get(label, "#FFFFFF") for label in bin_labels]94    95    # Create bar chart for binary classification96    fig2 = go.Figure(data=[go.Bar(x=bin_labels, y=bin_sizes, marker=dict(color=bin_colors))])97    fig2.update_layout(98        xaxis_title='Entity Type',99        yaxis_title='Count',100        template='plotly_dark',101        plot_bgcolor='rgba(0,0,0,0)',102        paper_bgcolor='rgba(0,0,0,0)'103    )104    105    # Generate word cloud106    wordcloud_image = generate_wordcloud(ner_output_bin['entities'], bin_color_map, "dh3.png")107    108    return fig2, wordcloud_image109 110def generate_wordcloud(entities: List[Dict], color_map: Dict[str, str], file_path: str) -> np.ndarray:111    # Construct the absolute path112    base_path = os.path.dirname(os.path.abspath(__file__))113    image_path = os.path.join(base_path, file_path)114    if not os.path.exists(image_path):115        raise FileNotFoundError(f"Mask image file not found: {image_path}")116 117    mask_image = np.array(Image.open(image_path))118    mask_height, mask_width = mask_image.shape[:2]119 120    word_details = []121 122    for entity in entities:123        for token in entity['tokens']:124            # Process each token125            token_text = token.replace("▁", " ").strip()126            if token_text:  # Ensure token is not empty127                word_details.append({128                    'text': token_text,129                    'score': entity.get('average_score', 0.5),130                    'entity': entity['entity']131                })132 133    # Calculate word frequency weighted by score134    word_freq = {}135    for detail in word_details:136        if detail['text'] in word_freq:137            word_freq[detail['text']]['score'] += detail['score']138            word_freq[detail['text']]['count'] += 1139        else:140            word_freq[detail['text']] = {'score': detail['score'], 'count': 1, 'entity': detail['entity']}141 142    # Average the scores and prepare final frequency dictionary143    final_word_freq = {word: details['score'] / details['count'] for word, details in word_freq.items()}144 145    # Prepare entity type mapping for color function146    word_to_entity = {word: details['entity'] for word, details in word_freq.items()}147 148    def color_func(word, font_size, position, orientation, random_state=None, **kwargs):149        entity_type = word_to_entity.get(word, None)150        return color_map.get(entity_type, "#FFFFFF")151 152    wordcloud = WordCloud(width=mask_width, height=mask_height, background_color='#121212', mask=mask_image, color_func=color_func).generate_from_frequencies(final_word_freq)153 154    plt.figure(figsize=(mask_width/100, mask_height/100))155    plt.imshow(wordcloud, interpolation='bilinear')156    plt.axis('off')157    plt.tight_layout(pad=0)158 159    plt_image = plt.gcf()160    plt_image.canvas.draw()161    image_array = np.frombuffer(plt_image.canvas.tostring_rgb(), dtype=np.uint8)162    image_array = image_array.reshape(plt_image.canvas.get_width_height()[::-1] + (3,))163    plt.close()164 165    return image_array166 167 168@spaces.GPU169def all(text: str):170    ner_output_bin = process_ner(text, pipe_bin)171 172    bar_chart, wordcloud_image = generate_charts(ner_output_bin)173    174    return (ner_output_bin, wordcloud_image)175 176examples = [177    ['Bevor ich meinen Hund kaufte bin ich immer alleine durch den Park gelaufen. Gestern war ich aber mit dem Hund losgelaufen. Das Wetter war sehr schön, nicht wie sonst im Winter. Ich weiß nicht genau. Mir fällt sonst nichts dazu ein. Wir trafen auf mehrere Spaziergänger. Ein Mann mit seinem Kind. Das Kind hat ein Eis gegessen.'],178    ['Also, ich kann mir total vorstellen, dass ich in so zwei Jahren eine mega Geburtstagsparty für meinen besten Freund organisieren werde. Also, das wird echt krass, ich schwöre es dir. Ich werde eine coole Location finden, wahrscheinlich so ein Haus am See oder so, und dann lade ich echt alle seine Freunde und Familie ein. Und dann, das wird der Hammer, ich werde eine Band organisieren, die so seine ganze Lieblingsmusik spielt, weißt du? Und dann, weil ich ja keine Lust habe, selbst zu kochen, hol ich mir so einen professionellen Catering-Service, die dann für alle Gäste kochen. Na ja, ich hoff mal, dass das Wetter mitspielt und wir alle draußen feiern können. Ich sag dir, das wird echt ne unvergessliche Feier, und mein Freund wird ausflippen vor Überraschung, echt jetzt.'],179    ["So, I really imagine that in two years, I'll finally be living my dream and writing a novel. I'll find a quiet place where I can fully concentrate on writing. I'll tell a story that really engages me and that I want to share with others. I'll draw inspiration from my experiences and the people around me, just like in real life. I'll spend many hours putting my thoughts on paper and bringing my characters to life. Well, I hope that readers also find my story fascinating; that would be really cool."],180    ['Oh mein Gott, ich muss dir diese total lustige Geschichte aus meiner Schulzeit erzählen! Du wirst es nicht glauben. Also, ich kam eines Tages zu spät zur Schule, richtig? Du weißt, wie das ist, man ist in Eile und achtet nicht wirklich darauf. Ich habe einfach ein Paar Schuhe gegriffen und bin aus dem Haus gerannt. Erst als ich in der Schule war und mich zum Mittagessen hinsetzte, bemerkte ich, dass ich zwei völlig unterschiedliche Schuhe anhatte! Ich hatte einen schwarzen und einen weißen Turnschuh an. Und ich mache keine Witze, die Leute haben es sofort bemerkt. Ein Typ namens Tommy hat mich Mismatch Mike genannt und bald haben alle mitgemacht. Oh Mann, ich war damals so peinlich berührt! Jetzt finde ich es einfach nur witzig und frage mich, wie mir das nicht aufgefallen ist. Das ist eine dieser Geschichten, die ich jetzt auf Partys erzähle, und die Leute finden es total lustig.'],181    ["You know, this conversation reminded me of an incredible experience I had at a music festival in college. I'll never forget it. It was a rainy day, but we didn't care, and the band that was playing was my absolute favorite. Even though we were all soaked, the crowd kept on dancing and singing along. The energy was incredible, and I remember feeling so connected to everyone around me. It was as if the rain made the whole experience even more magical. I was surrounded by friends, and we all shared this special moment together. It was one of the best moments of my life, and I still get goosebumps when I think about it. Sometimes, it's the unexpected things that create the most amazing memories, you know?"],182]183 184iface = gr.Interface(185    fn=all,186    inputs=gr.Textbox(lines=5, label="Input Text", placeholder="Write about how your breakfast went or anything else that happened or might happen to you ..."),187    outputs=[188        gr.HighlightedText(label="Binary Sequence Classification",189                           color_map = bin_color_map190                          ),191        gr.Image(label="Entity Word Cloud")192    ],193    title="Scoring Demo",194    description="Autobiographical Memory Analysis: This demo combines two text - and two sequence classification models to showcase our automated Autobiographical Interview scoring method. Submit a narrative to see the results.",195    examples=examples,196    theme=monochrome197) 198 199iface.launch()