Team Ai
Apppublic

deep-learning-analytics/Title_Generation

sourceHugging Faceupdated 5y agoView on Hugging Face
2likes
app.py48 linesDownload Raw Back to root
1import torch2 3import streamlit as st4 5st.title("Title Generation with Transformers")6st.write("")7st.write("Input your text here!")8 9 10default_value = "Ukrainian counterattacks: Kharkiv's regional administrator said a number of villages around Malaya Rogan were retaken by Ukrainian forces. Video verified by CNN shows Ukrainian troops in control of Vilkhivka, one of the settlements roughly 20 miles from the Russian border. The success of Ukrainian forces around Kharkiv has been mirrored further north, near the city of Sumy, where Ukrainian troops have liberated a number of settlements, according to videos geolocated and verified by CNN. A separate counterattack in the south also led to the liberation of two villages from Russian forces northwest of Mariupol, according to the Zaporizhzhia regional military administration."11 12sent = st.text_area("Text", default_value, height = 50)13 14from transformers import AutoTokenizer, AutoModelForSeq2SeqLM15 16tokenizer = AutoTokenizer.from_pretrained("deep-learning-analytics/automatic-title-generation")17 18model = AutoModelForSeq2SeqLM.from_pretrained("deep-learning-analytics/automatic-title-generation")19  20 21def tokenize_data(text):22    # Tokenize the review body23    input_ =  str(text) + ' </s>'24    max_len = 12025    # tokenize inputs26    tokenized_inputs = tokenizer(input_, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='pt')27 28    inputs={"input_ids": tokenized_inputs['input_ids'],29        "attention_mask": tokenized_inputs['attention_mask']}30    return inputs31 32def generate_answers(text):33    inputs = tokenize_data(text)34    results= model.generate(input_ids= inputs['input_ids'], attention_mask=inputs['attention_mask'], do_sample=True,35                            max_length=120,36                            top_k=120,37                            top_p=0.98,38                            early_stopping=True,39                            num_return_sequences=1)40    answer = tokenizer.decode(results[0], skip_special_tokens=True)41    return answer42 43answer = generate_answers(sent)44 45st.write(answer)46 47#iface = gr.Interface(fn=generate_answers,inputs=[gr.inputs.Textbox(lines=20)], outputs=["text"])48#iface.launch(inline=False, share=True)