Download app.py from garima24/textgen: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/spaces/garima24/textgen/resolve/main/app.py
- Command line
-
hf download hf://spaces/garima24/textgen/app.py
-
curl -L -o app.py https://huggingface.co/spaces/garima24/textgen/resolve/main/app.py
4.39 kB
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import spacy | |
| from sumy.parsers.plaintext import PlaintextParser | |
| from sumy.nlp.tokenizers import Tokenizer | |
| from sumy.summarizers.text_rank import TextRankSummarizer | |
| from detoxify import Detoxify | |
| from textblob import TextBlob | |
| from gramformer import Gramformer # Grammar correction | |
| import torch | |
| # Replace with your Hugging Face token | |
| # Load GPT-NeoX model and tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B") | |
| model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-1.3B") | |
| # Initialize the sentiment analysis pipeline with DistilBERT | |
| sentiment_model = "distilbert-base-uncased" | |
| sentiment_analyzer = pipeline("sentiment-analysis", model=sentiment_model) | |
| nlp = spacy.load("en_core_web_sm") | |
| # Load Grammar Correction Model | |
| gf = Gramformer(models=1, use_gpu=torch.cuda.is_available()) | |
| # Bias Terms List (Can be expanded) | |
| BIAS_TERMS = ["he/she", "rich/poor", "race", "gender", "disabled", "poverty"] | |
| # Advanced Refinement - TextRank Summarization | |
| def summarize_text(text): | |
| parser = PlaintextParser.from_string(text, Tokenizer("english")) | |
| summarizer = TextRankSummarizer() | |
| summary = summarizer(parser.document, 3) # Extract top 3 sentences | |
| return " ".join(str(sentence) for sentence in summary) | |
| # Grammar Correction using Gramformer | |
| def correct_grammar(text): | |
| corrected = gf.correct(text, max_candidates=1) | |
| return list(corrected)[0] if corrected else text | |
| # Detect Toxic Content (Bias and Offensive Language) | |
| def detect_bias(text): | |
| toxic_score = Detoxify("original").predict(text) | |
| bias_flag = any(term in text.lower() for term in BIAS_TERMS) | |
| if toxic_score["toxicity"] > 0.3 or bias_flag: | |
| return "โ ๏ธ Potential bias/toxicity detected!" | |
| return "โ Content is neutral and inclusive." | |
| # Sentiment Analysis for Content Evaluation using DistilBERT for sentiment analysis | |
| def analyze_sentiment(text): | |
| sentiment = sentiment_analyzer(text) | |
| label = sentiment[0]['label'] | |
| if label == "POSITIVE": | |
| return "๐ Positive" | |
| elif label == "NEGATIVE": | |
| return "โน๏ธ Negative" | |
| else: | |
| return "๐ Neutral" | |
| # Content Generation Pipeline | |
| class TextGenerationPipeline: | |
| def __init__(self): | |
| self.tokenizer = tokenizer | |
| self.model = model | |
| self.nlp = nlp | |
| def generate(self, prompt): | |
| """Generates educational content based on user input.""" | |
| inputs = self.tokenizer(prompt, return_tensors="pt") | |
| outputs = self.model.generate(inputs.input_ids, max_length=250, num_return_sequences=1) | |
| generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return generated_text | |
| def refine(self, text): | |
| """Refines content using summarization, grammar correction, and readability fixes.""" | |
| summarized = summarize_text(text) | |
| corrected = correct_grammar(summarized) | |
| return corrected | |
| def detect_bias(self, text): | |
| """Detects and flags biased content.""" | |
| return detect_bias(text) | |
| def evaluate(self, text): | |
| """Evaluates the content sentiment and readability.""" | |
| sentiment = analyze_sentiment(text) | |
| return sentiment | |
| def run(self, prompt): | |
| """Runs the full pipeline.""" | |
| generated = self.generate(prompt) | |
| refined = self.refine(generated) | |
| bias_check = self.detect_bias(refined) | |
| sentiment = self.evaluate(refined) | |
| return generated, refined, bias_check, sentiment | |
| # Initialize Pipeline | |
| pipeline = TextGenerationPipeline() | |
| # Build Gradio UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# ๐ GPT-NeoX-20B Educational Content Generator") | |
| gr.Markdown("### AI-powered content generation with bias detection and advanced refinement.") | |
| prompt_input = gr.Textbox(label="Enter your prompt:") | |
| generate_btn = gr.Button("Generate Content") | |
| output_text = gr.Textbox(label="Generated Content", interactive=False) | |
| refined_text = gr.Textbox(label="Refined Content", interactive=False) | |
| bias_check = gr.Textbox(label="Bias Check Result", interactive=False) | |
| sentiment_analysis = gr.Textbox(label="Sentiment Analysis", interactive=False) | |
| generate_btn.click(pipeline.run, inputs=[prompt_input], outputs=[output_text, refined_text, bias_check, sentiment_analysis]) | |
| demo.launch() | |