File size: 3,029 Bytes
89ab2dd
 
 
 
 
 
 
 
 
 
 
 
 
cb91214
89ab2dd
 
 
 
 
cb91214
89ab2dd
 
 
 
 
 
 
 
 
 
 
b57153a
cb91214
89ab2dd
8f530ca
2f42cb7
cb91214
2f42cb7
8f530ca
2f42cb7
 
 
cb91214
2f42cb7
 
8f530ca
2f42cb7
 
 
b57153a
2f42cb7
89ab2dd
 
 
 
 
b57153a
8f530ca
89ab2dd
b57153a
89ab2dd
 
cb91214
89ab2dd
2f42cb7
89ab2dd
 
 
 
 
 
2f42cb7
 
 
 
 
 
 
89ab2dd
8f530ca
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
import gradio as gr
import os
from transformers import AutoModelForCausalLM, AutoTokenizer

# Set an environment variable
HF_TOKEN = os.environ.get("HF_TOKEN", None)

# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", device_map="auto")

def generate_summary(text: str, temperature: float, max_new_tokens: int) -> str:
    """
    Generate a single-line summary from the input text using the llama3-8b model.
    Args:
        text (str): The input text to summarize.
        temperature (float): The temperature for generating the response.
        max_new_tokens (int): The maximum number of new tokens to generate.
    Returns:
        str: The generated summary in a single line.
    """
    input_ids = tokenizer.encode(text, return_tensors="pt").to(model.device)

    output_ids = model.generate(
        input_ids=input_ids,
        max_new_tokens=max_new_tokens,
        do_sample=True,
        temperature=temperature,
    )

    summary = tokenizer.decode(output_ids[0], skip_special_tokens=True)
    # Convert to a single line
    return " ".join(summary.split())

def summarize_file(file_path, temperature: float, max_new_tokens: int) -> str:
    """
    Summarize the content of an uploaded file into a single line.
    Args:
        file_path (str): The path of the uploaded file.
        temperature (float): The temperature for generating the response.
        max_new_tokens (int): The maximum number of new tokens to generate.
    Returns:
        str: The generated summary of the file's content in a single line.
    """
    # Read file content
    with open(file_path, 'r') as f:
        text = f.read()
        
    # Generate summary
    return generate_summary(text, temperature, max_new_tokens)

# Gradio block for text summarization
with gr.Blocks() as demo:
    gr.Markdown("<h1>Text Summarization Application</h1>")
    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(lines=10, label="Input Text", placeholder="Enter text here...")
            file_input = gr.File(label="Upload Text File", file_count="single", type="filepath")
            temperature = gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label="Temperature")
            max_tokens = gr.Slider(minimum=10, maximum=512, step=1, value=150, label="Max New Tokens")
            submit_button = gr.Button("Generate Summary")
        with gr.Column():
            summary_output = gr.Textbox(lines=1, label="Summary", interactive=False)

    # Link button to generate summary from text input
    submit_button.click(
        fn=generate_summary,
        inputs=[text_input, temperature, max_tokens],
        outputs=summary_output
    )

    # Link file upload to summary generation
    file_input.change(
        fn=summarize_file,
        inputs=[file_input, temperature, max_tokens],
        outputs=summary_output
    )

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