Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,38 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
# Load the
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
def custom_function(input_text):
|
8 |
-
|
9 |
-
|
10 |
-
if len(input_text.split()) > max_tokens:
|
11 |
-
input_text = ' '.join(input_text.split()[:max_tokens])
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
# Customize the examples
|
18 |
examples = [
|
19 |
["в 2006-2010 гг. Вася учился в МГУ"],
|
20 |
["я купил iphone 10X за 14990 руб без 3-x часов полдень и т.д."]
|
21 |
]
|
22 |
|
23 |
-
|
24 |
-
gr.Interface(
|
25 |
fn=custom_function,
|
26 |
inputs="text",
|
27 |
outputs="text",
|
28 |
examples=examples
|
29 |
-
)
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import transformers
|
3 |
|
4 |
+
# Load the model
|
5 |
+
model_name = "alexue4/text-normalization-ru-new"
|
6 |
+
model = transformers.AutoModelForSequenceClassification.from_pretrained(model_name)
|
7 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
+
# Define the custom function
|
10 |
def custom_function(input_text):
|
11 |
+
max_tokens = 100 # Set your desired max token limit
|
12 |
+
tokens = tokenizer.tokenize(input_text)
|
|
|
|
|
13 |
|
14 |
+
if len(tokens) > max_tokens:
|
15 |
+
tokens = tokens[:max_tokens]
|
16 |
+
input_text = tokenizer.convert_tokens_to_string(tokens)
|
17 |
+
|
18 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
19 |
+
outputs = model(**inputs)
|
20 |
+
prediction = outputs.logits.argmax(dim=-1).item()
|
21 |
+
|
22 |
+
return prediction
|
23 |
|
|
|
24 |
examples = [
|
25 |
["в 2006-2010 гг. Вася учился в МГУ"],
|
26 |
["я купил iphone 10X за 14990 руб без 3-x часов полдень и т.д."]
|
27 |
]
|
28 |
|
29 |
+
interface = gr.Interface(
|
|
|
30 |
fn=custom_function,
|
31 |
inputs="text",
|
32 |
outputs="text",
|
33 |
examples=examples
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the interface
|
37 |
+
interface.launch()
|
38 |
+
|