Spaces:
Running
Running
File size: 4,101 Bytes
b0503ee 84669bc b7577da 7feda08 90fff6b 7fc55d1 9ea0d50 236bb4b 6d0ac04 90fff6b 9ea0d50 7fc55d1 51568dc 6ba2176 9ea0d50 6ba2176 fbc26ed 6ba2176 7feda08 59b2b8e 236bb4b b6e297c 5da5cc3 b6e297c 5da5cc3 92af867 90fff6b e21ee90 5da5cc3 59b2b8e 5da5cc3 124e989 847e3e1 236bb4b aed9390 236bb4b |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
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
@app.route('/api/ai-detection', methods=['POST'])
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
@app.route('/api/paraphrase-correct', methods=['POST'])
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)
|