import gradio as gr from transformers import AutoModelForSeq2SeqLM, AutoTokenizer from preprocessing import NegativeWordReplacer # Import our preprocessing pipeline # Load model from Hugging Face model_path = "asritha22bce/bart-positive-tone" # Ensure this model exists and is public model = AutoModelForSeq2SeqLM.from_pretrained(model_path) tokenizer = AutoTokenizer.from_pretrained(model_path) # Initialize the preprocessing pipeline excel_path = "replacement_words.xlsx" # Ensure this file is in the same folder pipeline = NegativeWordReplacer(excel_path) def generate_positive_headline(text): """Pre-process text using the pipeline, then generate a positive-tone headline.""" preprocessed_text = pipeline.replace_negative_words(text) inputs = tokenizer(preprocessed_text, return_tensors="pt", truncation=True, padding=True) outputs = model.generate(**inputs, max_length=50) return tokenizer.decode(outputs[0], skip_special_tokens=True) # ✅ Enable API access by adding `api=True` interface = gr.Interface( fn=generate_positive_headline, inputs=gr.Textbox(label="Enter a Headline"), outputs=gr.Textbox(label="Positive Headline"), title="Positive Headline Generator", description="This app converts headlines into a more positive tone by first replacing negative/exaggerated words, then using a fine-tuned BART model.", allow_flagging="never", ) interface.launch(share=True)