TaurusOffice/TOS
0
1import gradio as gr2from transformers import pipeline3from better_profanity import profanity4 5import logging6 7# Initialize the summarizer8summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")9 10def summarize_text(text, summary_type="normal"):11 try:12 # Check if text exceeds 1000 words13 text = profanity.censor(text)14 word_count = len(text.split())15 if word_count > 1000:16 return "Error: Maximum allowed text length is 1000 words."17 18 19 # Set summary length based on the selected type20 if summary_type == "normal":21 max_len = word_count // 2 # Approximately half the length22 min_len = max(word_count // 4, 30) # At least 30 words23 elif summary_type == "brief":24 max_len = word_count // 4 # 1/4 of the text length25 min_len = 30 # At least 30 words26 elif summary_type == "detailed":27 max_len = (word_count * 2) // 3 # 2/3 of the text length28 min_len = max(word_count // 4, 30) # At least 30 words29 else:30 return "Error: Invalid summary type selected."31 32 # Generate the summary33 summary = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)34 return summary[0]['summary_text']35 36 except Exception as e:37 logging.error(f"Error during summarization: {e}")38 return "Error during summarization"39 40# Define Gradio interface41iface = gr.Interface(42 fn=summarize_text,43 inputs=[44 gr.Textbox(label="Input Text", lines=10, placeholder="Enter your text here..."), # Bigger input textbox45 gr.Radio(["normal", "brief", "detailed"], label="Summary Type", value="normal") # Radio buttons for summary type46 ],47 outputs=gr.Textbox(label="Summarized Text", lines=10, placeholder="The summary will appear here..."), # Bigger output textbox48 title="Text Summarizer"49)50 51iface.launch(share=True)52 