import gradio as gr from transformers import pipeline import re # Load the pre-trained AI text classification model detector = pipeline("text-classification", model="roberta-base-openai-detector") # Count words in the input text def count_words(text): return len(re.findall(r'\b\w+\b', text)) # Count characters (excluding spaces) def count_characters(text): return len(text.replace(" ", "")) # Detect if the text is AI-generated or human-written def detect_text(text): if not text.strip(): return "No text entered." result = detector(text)[0] label = result['label'] score = round(result['score'] * 100, 2) return f"Prediction: {label} ({score}%)" # Perform full analysis def full_analysis(text): prediction = detect_text(text) words = count_words(text) chars = count_characters(text) return f"{prediction}\n\nWord Count: {words}\nCharacter Count: {chars}" description = """ Detect whether a given text is AI-generated or human-written. Also view word and character count for basic analysis. """ examples = [ ["The sun sets beautifully behind the hills every evening."], ["As an AI language model developed by OpenAI, I am capable of many tasks."], ["She opened the book and smiled as the story unfolded."] ] with gr.Blocks(title="Text AI Detector") as interface: gr.Markdown("# Text AI Detector") gr.Markdown(description) with gr.Tab("Detector"): text_input = gr.Textbox(label="Input Text", lines=8, placeholder="Type or paste your text here...") analyze_btn = gr.Button("Analyze") output = gr.Textbox(label="Result", lines=6) analyze_btn.click(fn=full_analysis, inputs=text_input, outputs=output) with gr.Tab("Examples"): gr.Examples(examples=examples, inputs=[text_input], outputs=[output], fn=full_analysis) gr.Markdown("---") gr.Markdown("Final Year Project | Built with Hugging Face + Gradio") if __name__ == "__main__": interface.launch(share=True)