Spaces:
Running
Running
| import os | |
| import gradio as gr | |
| from transformers import pipeline | |
| import spacy | |
| import subprocess | |
| import nltk | |
| from nltk.corpus import wordnet | |
| from spellchecker import SpellChecker | |
| from flask import Flask, jsonify, request | |
| # Initialize Flask app | |
| app = Flask(__name__) | |
| # Initialize the English text classification pipeline for AI detection | |
| pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta") | |
| # Initialize the spell checker | |
| spell = SpellChecker() | |
| # Ensure necessary NLTK data is downloaded | |
| nltk.download('wordnet') | |
| nltk.download('omw-1.4') | |
| # Ensure the SpaCy model is installed | |
| try: | |
| nlp = spacy.load("en_core_web_sm") | |
| except OSError: | |
| subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"]) | |
| nlp = spacy.load("en_core_web_sm") | |
| # Function to predict the label and score for English text (AI Detection) | |
| def predict_en(text): | |
| res = pipeline_en(text)[0] | |
| return res['label'], res['score'] | |
| # Other processing functions (remove redundant words, capitalization, etc.) as previously defined | |
| # For brevity, I'm skipping them here since they're unchanged. Make sure to include all the defined functions from the original code. | |
| # Function to paraphrase and correct grammar with enhanced accuracy | |
| def paraphrase_and_correct(text): | |
| cleaned_text = remove_redundant_words(text) | |
| paraphrased_text = capitalize_sentences_and_nouns(cleaned_text) | |
| paraphrased_text = force_first_letter_capital(paraphrased_text) | |
| paraphrased_text = correct_article_errors(paraphrased_text) | |
| paraphrased_text = correct_singular_plural_errors(paraphrased_text) | |
| paraphrased_text = correct_tense_errors(paraphrased_text) | |
| paraphrased_text = correct_double_negatives(paraphrased_text) | |
| paraphrased_text = ensure_subject_verb_agreement(paraphrased_text) | |
| paraphrased_text = rephrase_with_synonyms(paraphrased_text) | |
| paraphrased_text = correct_spelling(paraphrased_text) | |
| return paraphrased_text | |
| # API Endpoint for AI Detection | |
| def ai_detection(): | |
| data = request.get_json() | |
| text = data.get('text', '') | |
| if text: | |
| label, score = predict_en(text) | |
| return jsonify({"label": label, "score": score}) | |
| else: | |
| return jsonify({"error": "No text provided"}), 400 | |
| # API Endpoint for Paraphrasing and Grammar Correction | |
| def paraphrase_and_correct_api(): | |
| data = request.get_json() | |
| text = data.get('text', '') | |
| if text: | |
| corrected_text = paraphrase_and_correct(text) | |
| return jsonify({"corrected_text": corrected_text}) | |
| else: | |
| return jsonify({"error": "No text provided"}), 400 | |
| # Gradio app setup with two tabs | |
| def launch_gradio(): | |
| with gr.Blocks() as demo: | |
| with gr.Tab("AI Detection"): | |
| t1 = gr.Textbox(lines=5, label='Text') | |
| button1 = gr.Button("π€ Predict!") | |
| label1 = gr.Textbox(lines=1, label='Predicted Label π') | |
| score1 = gr.Textbox(lines=1, label='Prob') | |
| # Connect the prediction function to the button | |
| button1.click(fn=predict_en, inputs=t1, outputs=[label1, score1]) | |
| with gr.Tab("Paraphrasing & Grammar Correction"): | |
| t2 = gr.Textbox(lines=5, label='Enter text for paraphrasing and grammar correction') | |
| button2 = gr.Button("π Paraphrase and Correct") | |
| result2 = gr.Textbox(lines=10, label='Corrected Text', placeholder="The corrected text will appear here...") | |
| # Connect the paraphrasing and correction function to the button | |
| button2.click(fn=paraphrase_and_correct, inputs=t2, outputs=result2) | |
| demo.launch(share=True) # Share=True to create a public link | |
| # Launch Gradio interface in a separate thread | |
| if __name__ == '__main__': | |
| # Run Flask app in one thread and Gradio in another | |
| from threading import Thread | |
| # Gradio interface | |
| gradio_thread = Thread(target=launch_gradio) | |
| gradio_thread.start() | |
| # Flask API | |
| app.run(debug=True, port=5000) | |