File size: 1,435 Bytes
844c179
 
32ff9af
844c179
32ff9af
 
844c179
 
 
32ff9af
07a8a78
32ff9af
 
844c179
32ff9af
 
 
844c179
 
 
0d55fe0
844c179
 
 
 
32ff9af
 
0d55fe0
7203932
844c179
 
32ff9af
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
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)