alexue4 commited on
Commit
1c903eb
·
verified ·
1 Parent(s): 5e13b07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -1,29 +1,38 @@
1
  import gradio as gr
 
2
 
3
- # Load the pre-built interface
4
- interface = gr.load("models/alexue4/text-normalization-ru-new")
 
 
5
 
6
- # Function to modify the behavior (example: limiting max tokens and setting examples)
7
  def custom_function(input_text):
8
- # Custom processing here (e.g., limit max tokens)
9
- max_tokens = 250 # Set your desired max token limit
10
- if len(input_text.split()) > max_tokens:
11
- input_text = ' '.join(input_text.split()[:max_tokens])
12
 
13
- # Call the original function from the interface
14
- original_output = interface.predict(input_text)
15
- return original_output
 
 
 
 
 
 
16
 
17
- # Customize the examples
18
  examples = [
19
  ["в 2006-2010 гг. Вася учился в МГУ"],
20
  ["я купил iphone 10X за 14990 руб без 3-x часов полдень и т.д."]
21
  ]
22
 
23
- # Define the new Gradio interface with custom function and examples
24
- gr.Interface(
25
  fn=custom_function,
26
  inputs="text",
27
  outputs="text",
28
  examples=examples
29
- ).launch()
 
 
 
 
 
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
+