File size: 1,904 Bytes
8e4890d
 
 
 
 
7426b1c
8e4890d
7426b1c
8e4890d
 
 
7426b1c
 
 
 
 
 
 
 
 
 
8e4890d
 
 
7426b1c
8e4890d
7426b1c
 
 
 
 
 
 
 
 
 
 
 
 
 
8e4890d
7426b1c
8e4890d
 
 
 
7426b1c
 
8e4890d
7426b1c
 
 
 
 
 
 
 
8e4890d
7426b1c
8e4890d
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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()