File size: 5,181 Bytes
a917110
 
8f29b1d
3f6f474
b91a6bd
cf575f8
 
 
ec1d54e
cf575f8
d654474
b91a6bd
 
 
 
 
 
cf575f8
 
a917110
 
 
 
 
 
 
 
 
 
cf575f8
a917110
cf575f8
e32f803
cf575f8
 
 
 
 
 
 
b91a6bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
613e689
 
 
dd2409d
613e689
 
b91a6bd
8f29b1d
b91a6bd
e691ea0
8f29b1d
 
a917110
8f29b1d
 
 
 
 
 
 
ec1d54e
 
 
 
b91a6bd
 
 
 
 
8f29b1d
 
 
 
 
 
 
 
 
a917110
 
8f29b1d
b91a6bd
8f29b1d
a917110
e691ea0
 
 
 
 
 
 
 
 
b91a6bd
d654474
cf575f8
0e97d35
d654474
 
 
 
 
 
3f6f474
d654474
 
cf575f8
dd2409d
 
b91a6bd
 
dd2409d
3f6f474
8920952
cf575f8
3f6f474
613e689
3f6f474
0e97d35
613e689
 
 
cf575f8
b91a6bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a917110
 
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
import os
import tempfile
import traceback
from io import StringIO
from typing import Generator, Optional

import gradio as gr
import pandas as pd
from loguru import logger

from utils import pipeline
from utils.models import QWEN3_EMBEDDING_MODEL, get_embedding_model, list_models

LEGACY_POOLING_CHOICES = ['mean', 'cls']
QWEN3_POOLING_CHOICES = ['last-token']
DEFAULT_POOLING = 'mean'
QWEN3_POOLING = 'last-token'


def resolve_file_path(file) -> str:
    if isinstance(file, dict) and file.get('path'):
        return os.fspath(file['path'])
    if isinstance(file, (str, os.PathLike)):
        return os.fspath(file)
    if hasattr(file, 'name'):
        return os.fspath(file.name)
    raise TypeError(f'Unsupported file input: {type(file)!r}')


def read_data(filepath: str) -> Optional[pd.DataFrame]:
    filepath = os.fspath(filepath)
    if filepath.endswith('.xlsx'):
        df = pd.read_excel(filepath)
    elif filepath.endswith('.csv'):
        df = pd.read_csv(filepath)
    else:
        raise Exception('File type not supported')
    return df


def effective_pooling(model_name: str, pooling: str) -> str:
    if model_name == QWEN3_EMBEDDING_MODEL:
        return QWEN3_POOLING
    return pooling


def update_pooling_for_model(model_name: str):
    if model_name == QWEN3_EMBEDDING_MODEL:
        return gr.update(
            choices=QWEN3_POOLING_CHOICES,
            value=QWEN3_POOLING,
            interactive=False,
        )
    return gr.update(
        choices=LEGACY_POOLING_CHOICES,
        value=DEFAULT_POOLING,
        interactive=True,
    )


def process(
        task_name: str,
        model_name: str,
        pooling: str,
        text: str,
        file=None,
) -> Generator[tuple[str, Optional[pd.DataFrame], Optional[str]], None, None]:
    try:
        pooling = effective_pooling(model_name, pooling)
        logger.info(f'Processing {task_name} with {model_name} and {pooling}')
        # load file
        if file:
            df = read_data(resolve_file_path(file))
        elif text:
            string_io = StringIO(text)
            df = pd.read_csv(string_io)
            assert len(df) >= 1, 'No input data'
        else:
            raise Exception('No input data')

        # check
        if len(df) > 10000:
            raise Exception('Data exceeds 10,000 rows')

        yield f'模型加载中:{model_name}', None, None
        get_embedding_model(model_name)

        yield '计算中...', None, None

        # process
        if task_name == 'Originality':
            df = pipeline.p0_originality(df, model_name, pooling)
        elif task_name == 'Flexibility':
            df = pipeline.p1_flexibility(df, model_name, pooling)
        else:
            raise Exception('Task not supported')

        # save
        fd, path = tempfile.mkstemp(prefix='transdis_', suffix='.csv')
        os.close(fd)
        df.to_csv(path, index=False, encoding='utf-8-sig')
        yield '完成', df.iloc[:10], path

    except Exception:
        error = traceback.format_exc()
        logger.warning({
            'error': error,
            'task_name': task_name,
            'model_name': model_name,
            'pooling': pooling,
            'text': text,
            'file': file,
        })
        yield f'Something wrong\n\n{error}', None, None


# input
task_name_dropdown = gr.components.Dropdown(
    label='Task Name',
    value='Originality',
    choices=['Originality', 'Flexibility']
)
model_name_dropdown = gr.components.Dropdown(
    label='Model Name',
    value=list_models[0],
    choices=list_models
)
pooling_dropdown = gr.components.Dropdown(
    label='Pooling',
    value=DEFAULT_POOLING,
    choices=LEGACY_POOLING_CHOICES
)
text_input = gr.components.Textbox(
    value=open('data/example_xlm.csv', 'r').read(),
    lines=10,
)
file_input = gr.components.File(label='Input File', file_types=['.csv', '.xlsx'])

# output
text_output = gr.components.Textbox(label='Output')
dataframe_output = gr.components.Dataframe(label='DataFrame')
file_output = gr.components.File(label='Output File', file_types=['.csv', '.xlsx'])

with gr.Blocks(title='TransDis-CreativityAutoAssessment') as app:
    gr.Markdown('# TransDis-CreativityAutoAssessment')
    gr.Markdown(open('data/description.txt', 'r').read())
    with gr.Row():
        with gr.Column():
            task_name_dropdown.render()
            model_name_dropdown.render()
            pooling_dropdown.render()
            text_input.render()
            file_input.render()
            submit_button = gr.Button('Submit', variant='primary')
        with gr.Column():
            text_output.render()
            dataframe_output.render()
            file_output.render()

    model_name_dropdown.change(
        fn=update_pooling_for_model,
        inputs=model_name_dropdown,
        outputs=pooling_dropdown,
    )
    submit_button.click(
        fn=process,
        inputs=[task_name_dropdown, model_name_dropdown, pooling_dropdown, text_input, file_input],
        outputs=[text_output, dataframe_output, file_output],
        api_name='predict',
        concurrency_limit=1,
    )

if __name__ == '__main__':
    app.launch(max_threads=1)