File size: 3,416 Bytes
53bf50a
a0ade0a
 
2a3a970
a0ade0a
 
460a080
a0ade0a
 
 
e8a0f17
53bf50a
2a3a970
036e563
bf036e1
e8a0f17
036e563
 
 
b1dcc65
036e563
 
b1dcc65
036e563
b1dcc65
bf6da96
 
 
6b0ab1a
 
 
 
 
 
 
 
bf6da96
 
e9d9124
a0ade0a
8cb52d1
 
 
 
a0ade0a
8cb52d1
2a3a970
 
a0ade0a
2a3a970
 
 
6b0ab1a
 
 
 
2a3a970
 
 
a0ade0a
886193f
5e6d454
 
 
1b2076c
 
d79b3e8
3a9b2f3
 
 
 
9352ec1
 
a0ade0a
9352ec1
a0ade0a
 
 
9352ec1
a0ade0a
 
 
 
9352ec1
5a5168a
b1dcc65
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
from __future__ import annotations
from typing import Iterable, List, Dict, Tuple

import gradio as gr
from gradio.themes.base import Base
from gradio.themes.soft import Soft
from gradio.themes.monochrome import Monochrome
from gradio.themes.default import Default
from gradio.themes.utils import colors, fonts, sizes

import spaces
import torch
import os
import io
import colorsys

import numpy as np

from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, pipeline

import matplotlib.pyplot as plt
import plotly.graph_objects as go
from wordcloud import WordCloud


def hex_to_rgb(hex_color: str) -> tuple[int, int, int]:
    hex_color = hex_color.lstrip('#')
    return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))

def rgb_to_hex(rgb_color: tuple[int, int, int]) -> str:
    return "#{:02x}{:02x}{:02x}".format(*rgb_color)

def adjust_brightness(rgb_color: tuple[int, int, int], factor: float) -> tuple[int, int, int]:
    hsv_color = colorsys.rgb_to_hsv(*[v / 255.0 for v in rgb_color])
    new_v = max(0, min(hsv_color[2] * factor, 1))
    new_rgb = colorsys.hsv_to_rgb(hsv_color[0], hsv_color[1], new_v)
    return tuple(int(v * 255) for v in new_rgb)

monochrome = Monochrome()

auth_token = os.environ['HF_TOKEN']

model1 = AutoModelForSequenceClassification.from_pretrained("AlGe/deberta-v3-large_Int_segment", num_labels=1, token=auth_token)
tokenizer1 = AutoTokenizer.from_pretrained("AlGe/deberta-v3-large_Int_segment", token=auth_token)

model2 = AutoModelForSequenceClassification.from_pretrained("AlGe/deberta-v3-large_seq_ext", num_labels=1, token=auth_token)


def process_classification(text: str, model1, model2, tokenizer1) -> Tuple[str, str, str]:
    inputs1 = tokenizer1(text, max_length=512, return_tensors='pt', truncation=True, padding=True)
    
    with torch.no_grad():
        outputs1 = model1(**inputs1)
        outputs2 = model2(**inputs1)
    
    prediction1 = outputs1[0].item()
    prediction2 = outputs2[0].item()
    score = prediction1 / (prediction2 + prediction1)

    return f"{round(prediction1, 1)}", f"{round(prediction2, 1)}", f"{round(score, 2)}"

@spaces.GPU
def all(text: str):
    classification_output = process_classification(text, model1, model2, tokenizer1)

    return (classification_output[0], classification_output[1], classification_output[2])

examples = [
    ['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.'],
]

iface = gr.Interface(
    fn=all,
    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 ..."),
    outputs=[
        gr.Label(label="Internal Detail Count"),
        gr.Label(label="External Detail Count"),
        gr.Label(label="Approximated Internal Detail Ratio"),
    ],
    title="Scoring Demo",
    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.",
    examples=examples,
    theme=monochrome
)

iface.launch()