File size: 6,672 Bytes
fdd6a5f
e5a54e8
 
fdd6a5f
 
 
 
c0ba4ab
fdd6a5f
 
c0ba4ab
 
fdd6a5f
 
 
 
 
 
e5a54e8
 
 
 
 
 
 
 
 
 
fdd6a5f
 
e5a54e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0ba4ab
e5a54e8
c0ba4ab
 
e5a54e8
 
 
 
 
c0ba4ab
e5a54e8
 
c0ba4ab
 
 
 
e5a54e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0ba4ab
e5a54e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0ba4ab
e5a54e8
 
 
20e57f9
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Set environment variables before any other imports
import os
import tempfile

# Create app-specific temp directories
temp_base = tempfile.gettempdir()
os.environ['HF_HOME'] = os.path.join(temp_base, 'huggingface_home')
os.environ['GRADIO_TEMP_DIR'] = os.path.join(temp_base, 'gradio_tmp')

# Create directories with appropriate permissions
for dir_path in [os.environ['HF_HOME'], 
                 os.environ['GRADIO_TEMP_DIR']]:
    try:
        os.makedirs(dir_path, mode=0o777, exist_ok=True)
    except Exception as e:
        print(f"Warning: Could not create directory {dir_path}: {e}")

# Now import the rest of the dependencies
import gradio as gr
from typing import Optional, Tuple, Union
from pathlib import Path

# Import summarizer components
from summarizer.summarizer import process_text
from summarizer.utils import extract_from_url

class SummaryMaker:
    def __init__(self):
        """Initialize the SummaryMaker application."""
        pass  # Environment setup is now handled at module level

    def summarize_text(
        self,
        choice: str,
        url: str,
        file_path: Optional[str],
        text: str,
        model_name: str,
        max_length: int
    ) -> str:
        """
        Summarize text based on the input type chosen (URL, File, or direct Text).
        
        Args:
            choice: Input type ("URL", "File", or "Text")
            url: URL to extract text from
            file_path: Path to uploaded file
            text: Direct input text
            model_name: Name of the summarization model to use
            max_length: Maximum length of the summary
            
        Returns:
            str: Generated summary or error message
        """
        try:
            input_text = self._get_input_text(choice, url, file_path, text)
            
            if not input_text or len(input_text.strip()) < 50:
                return "Error: Not enough text content to summarize (minimum 50 characters)"
                
            summary = process_text(
                input_text,
                model=model_name,
                max_length=max_length
            )
            return summary
            
        except Exception as e:
            return f"Summarization failed: {str(e)}"

    def _get_input_text(
        self,
        choice: str,
        url: str,
        file_path: Optional[str],
        text: str
    ) -> str:
        """
        Extract text content based on the chosen input method.
        
        Args:
            choice: Input type ("URL", "File", or "Text")
            url: URL to extract text from
            file_path: Path to uploaded file
            text: Direct input text
            
        Returns:
            str: Extracted text content
            
        Raises:
            ValueError: If text extraction fails
        """
        if choice == "URL":
            if not url:
                raise ValueError("No URL provided")
            return extract_from_url(url)
            
        elif choice == "File":
            if not file_path:
                raise ValueError("No file uploaded")
            return Path(file_path.name).read_text(encoding='utf-8')
            
        elif choice == "Text":
            return text
            
        raise ValueError(f"Invalid choice: {choice}")

    def update_visibility(
        self,
        choice: str
    ) -> Tuple[gr.update, gr.update, gr.update, gr.update]:
        """
        Update the visibility of input components based on the selected choice
        and clear the summary output.
        
        Args:
            choice: Selected input type
            
        Returns:
            Tuple of Gradio updates for URL, File, Text, and Summary components
        """
        return (
            gr.update(visible=(choice == "URL"), value=""),  # URL input
            gr.update(visible=(choice == "File"), value=None),  # File input
            gr.update(visible=(choice == "Text"), value=""),  # Text input
            gr.update(value="")  # Summary output
        )

    def create_interface(self):
        """Create and configure the Gradio interface."""
        with gr.Blocks(title="SummaryMaker") as demo:
            gr.Markdown("# SummaryMaker")
            gr.Markdown(
                """A simple tool to generate summaries from text, URLs, or files.
                Choose your input method and adjust the settings below."""
            )
            
            # Input method selection
            choice = gr.Dropdown(
                choices=["Text", "URL", "File"],
                label="Choose input type",
                value="Text"
            )
            
            # Input components
            url = gr.Textbox(
                label="URL to Summarize",
                placeholder="Enter URL here...",
                visible=False
            )
            file = gr.File(
                label="Upload File",
                file_types=[".txt", ".md", ".doc", ".docx"],
                visible=False
            )
            text = gr.Textbox(
                label="Text to Summarize",
                placeholder="Enter or paste your text here...",
                lines=10,
                visible=True
            )
            
            # Model settings
            with gr.Row():
                model = gr.Textbox(
                    label="Model Name",
                    value="t5-base",
                    placeholder="Enter model name..."
                )
                max_length = gr.Slider(
                    label="Maximum Summary Length",
                    minimum=50,
                    maximum=500,
                    value=180,
                    step=10
                )
            
            # Output
            summary = gr.Textbox(
                label="Generated Summary",
                lines=5
            )
            
            # Event handlers
            choice.change(
                fn=self.update_visibility,
                inputs=choice,
                outputs=[url, file, text, summary]  # Added summary to outputs
            )
            
            gr.Button("Generate Summary").click(
                fn=self.summarize_text,
                inputs=[choice, url, file, text, model, max_length],
                outputs=summary
            )
            
        return demo

def main():
    """Initialize and launch the application."""
    app = SummaryMaker()
    demo = app.create_interface()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False  # Set to True to create a public URL
    )

if __name__ == "__main__":
    main()