File size: 1,683 Bytes
3bdcf00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from summarizer import TextSummarizer

# Initialize the summarizer globally to load the model once
print("Initializing Summarizer...")
global_summarizer = TextSummarizer()

def summarize_text(text, target_words):
    try:
        # Ensure summarizer is initialized
        if global_summarizer.llm is None:
            return "Error: Model not loaded.", ""
            
        summary, stats = global_summarizer.summarize(text, int(target_words))
        return summary, stats
    except Exception as e:
        return f"An error occurred: {str(e)}", ""

# Create the Gradio interface
with gr.Blocks() as iface:
    gr.Markdown("# AI Text Summarizer (Local Mistral-7B)")
    gr.Markdown("Enter a long text to get a concise summary using the **Mistral-7B** model (running locally).")
    gr.Markdown("> **Note:** The first run might take a moment to load the model. Subsequent runs will be faster.")
    
    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(lines=10, label="Input Text", placeholder="Enter text to summarize here...")
            # Changed from Max Tokens to Target Words
            length_slider = gr.Slider(minimum=10, maximum=500, value=100, step=10, label="Target Summary Length (Words)")
            submit_btn = gr.Button("Summarize", variant="primary")
        
        with gr.Column():
            output_text = gr.Textbox(label="Summary", lines=10)
            stats_output = gr.Textbox(label="Statistics", lines=2)

    submit_btn.click(
        fn=summarize_text,
        inputs=[text_input, length_slider],
        outputs=[output_text, stats_output]
    )

if __name__ == "__main__":
    iface.launch()