File size: 1,395 Bytes
d901185
dc1cf62
d901185
 
4940531
dc1cf62
0c28d49
4940531
0c28d49
 
d901185
 
 
d0cf39e
d901185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch

import gradio as gr

from transformers import (PegasusForConditionalGeneration, PegasusTokenizer)
  
best_model_path = "aditi2222/paragus_models"
model = PegasusForConditionalGeneration.from_pretrained(best_model_path)
#tokenizer = PegasusTokenizer.from_pretrained('google/pegasus-xsum')
tokenizer = PegasusTokenizer.from_pretrained('aditi2222/paragus_models')

def tokenize_data(text):
    # Tokenize the review body
    input_ =  str(text) + ' </s>'
    max_len = 64
    # tokenize inputs
    tokenized_inputs = tokenizer(input_, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='pt')

    inputs={"input_ids": tokenized_inputs['input_ids'],
        "attention_mask": tokenized_inputs['attention_mask']}
    return inputs

def generate_answers(text):
    inputs = tokenize_data(text)
    results= model.generate(input_ids= inputs['input_ids'], attention_mask=inputs['attention_mask'], do_sample=True,
                            max_length=64,
                            top_k=120,
                            top_p=0.98,
                            early_stopping=True,
                            num_return_sequences=1)
    answer = tokenizer.decode(results[0], skip_special_tokens=True)
    return answer

iface = gr.Interface(fn=generate_answers, inputs=['text'], outputs=["text"])
iface.launch(inline=False, share=True)