karthiksagarn's picture
update app file
a512d51
import os
import gradio as gr
from transformers import pipeline, AutoTokenizer
# --- Optional: For private models, login with token ---
# Hugging Face token should be set in Space secrets as HF_TOKEN
# Using 'token' which is the current recommended argument.
hf_token = os.getenv("HF_TOKEN")
model_name = "karthiksagarn/bart-samsum-finetuned"
# Load summarizer pipeline and tokenizer
# Updated 'use_auth_token' to 'token'
summarizer = pipeline("summarization", model=model_name, token=hf_token)
tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
def generate_summary(text, max_input_tokens, max_output_tokens):
"""Generates a summary for the given text, respecting the max token limits."""
# Truncate input text to user-specified token limit
tokens = tokenizer.encode(text, truncation=True, max_length=max_input_tokens)
truncated_text = tokenizer.decode(tokens, skip_special_tokens=True)
# Generate summary using the user-defined max_length for the output
result = summarizer(
truncated_text,
max_length=max_output_tokens,
min_length=30,
do_sample=False
)
return result[0]['summary_text']
# --- FIX: Provide example values for ALL inputs ---
# Each example list must contain a value for the Textbox and both Sliders.
examples_list = [
[
"Artificial intelligence (AI) is transforming industries worldwide. From healthcare diagnostics to autonomous vehicles, AI systems process vast amounts of data to make predictions and decisions. Machine learning, a subset of AI, enables computers to learn from experience without explicit programming. Deep learning, using neural networks with many layers, has driven recent breakthroughs in image recognition and natural language processing.",
512,
150
],
[
"Climate change poses significant risks to global ecosystems. Rising temperatures lead to melting polar ice caps, sea-level rise, and extreme weather events. Mitigation strategies include reducing greenhouse gas emissions through renewable energy adoption and carbon capture technologies. Adaptation measures involve building resilient infrastructure and protecting vulnerable communities.",
512,
150
]
]
# Gradio Interface
iface = gr.Interface(
fn=generate_summary,
inputs=[
gr.Textbox(
lines=10,
placeholder="Paste your long text here...",
label="Input Text"
),
gr.Slider(
minimum=128,
maximum=1024,
value=512,
step=64,
label="Max Input Tokens"
),
gr.Slider(
minimum=30,
maximum=200,
value=150,
step=10,
label="Max Summary Length (Tokens)"
)
],
outputs=gr.Textbox(label="Summary"),
title="📝 BART Fine-Tuned Summarizer",
description="Summarize text using your fine-tuned BART model. Control the input and output token length with the sliders.",
examples=examples_list
)
# Launch for Hugging Face Spaces
iface.launch()