Update app.py
Browse files14 unmodified lines1516171819202122232428 unmodified lines5354555657585960616263646566676869707114 unmodified linesmodelgpt2 = GPT2LMHeadModel.from_pretrained(model_id).to(device)tokenizergpt2 = GPT2TokenizerFast.from_pretrained(model_id)def count_words(text):words = text.split() # Split the text into a list of wordsreturn len(words)def text_to_sentences(text):clean_text = text.replace('\n', ' ')return re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', clean_text)28 unmodified linesreturn ppl.item()def calculatePerplexities(text):if text is None or len(text) == 0:return jsonify({'error': 'No query provided'})if len(text) > 9400:return jsonify({'error': 'Cannot analyze more than 9400 characters!'})if count_words(text) > 1500:return jsonify({'error': 'Cannot analyze more than 1500 words'})sentences = text_to_sentences(text)perplexities = []for sentence in sentences:perplexity = calculatePerplexity(sentence)perplexities.append({"sentence": sentence, "perplexity": perplexity})return jsonify(perplexities)def input_api_info():return [gr.inputs.Textbox(placeholder="Copy and paste here...")]14 unmodified lines1516171819202128 unmodified lines505152535455565758596061626364656667686914 unmodified linesmodelgpt2 = GPT2LMHeadModel.from_pretrained(model_id).to(device)tokenizergpt2 = GPT2TokenizerFast.from_pretrained(model_id)def text_to_sentences(text):clean_text = text.replace('\n', ' ')return re.split(r'(?<=[^A-Z].[.?]) +(?=[A-Z])', clean_text)28 unmodified linesreturn ppl.item()def calculatePerplexities(text):with app.app_context():if text is None or len(text) == 0:return ({'error': 'No query provided'})if len(text) > 9400:return ({'error': 'Cannot analyze more than 9400 characters!'})if len(text.split) > 1500:return ({'error': 'Cannot analyze more than 1500 words'})sentences = text_to_sentences(text)perplexities = []for sentence in sentences:perplexity = calculatePerplexity(sentence)perplexities.append({"sentence": sentence, "perplexity": perplexity})return (perplexities)def input_api_info():return [gr.inputs.Textbox(placeholder="Copy and paste here...")]
app.py+15-17