Spaces:
Running
Running
import gradio as gr | |
import logging | |
from transformers import pipeline | |
class TextSummarizer: | |
def __init__(self, model_name="facebook/bart-large-cnn"): | |
self.summarizer = pipeline("summarization", model=model_name) | |
def summarize(self, text): | |
if not text: | |
return "No text to summarize." | |
try: | |
summary = self.summarizer( | |
text, | |
max_length=150, | |
min_length=50, | |
do_sample=False | |
)[0]['summary_text'] | |
return summary | |
except Exception as e: | |
return f"Summarization error: {str(e)}" | |
def process_input(input_type, input_data): | |
try: | |
# Direct text handling | |
if input_type == "Text": | |
return summarizer.summarize(input_data) | |
# File input handling (simplified) | |
if input_type in ["Text File", "PDF", "DOCX"]: | |
with open(input_data.name, 'r', encoding='utf-8') as file: | |
text = file.read() | |
return summarizer.summarize(text) | |
# Audio input (placeholder - would require speech-to-text) | |
if input_type == "Audio": | |
return "Audio summarization not implemented" | |
return "Invalid input type" | |
except Exception as e: | |
return f"Processing error: {str(e)}" | |
def main(): | |
global summarizer | |
summarizer = TextSummarizer() | |
# Gradio Interface | |
interface = gr.Interface( | |
fn=process_input, | |
inputs=[ | |
gr.Radio(["Text", "Text File", "PDF", "DOCX", "Audio"], label="Input Type"), | |
gr.File(type="file", label="Input") | |
], | |
outputs=gr.Textbox(label="Summary"), | |
title="Text Summarization App", | |
description="Upload text or select input type for summarization" | |
) | |
interface.launch() | |
if __name__ == "__main__": | |
main() |