Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
from transformers import pipeline
|
| 4 |
import spacy
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
| 9 |
-
from flask import Flask, jsonify, request
|
| 10 |
|
| 11 |
-
# Initialize
|
| 12 |
-
app =
|
| 13 |
|
| 14 |
# Initialize the English text classification pipeline for AI detection
|
| 15 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
@@ -28,83 +27,97 @@ except OSError:
|
|
| 28 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 29 |
nlp = spacy.load("en_core_web_sm")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Function to predict the label and score for English text (AI Detection)
|
| 32 |
-
def predict_en(text):
|
| 33 |
res = pipeline_en(text)[0]
|
| 34 |
-
return res['label'], res['score']
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
paraphrased_text = correct_double_negatives(paraphrased_text)
|
| 48 |
-
paraphrased_text = ensure_subject_verb_agreement(paraphrased_text)
|
| 49 |
-
paraphrased_text = rephrase_with_synonyms(paraphrased_text)
|
| 50 |
-
paraphrased_text = correct_spelling(paraphrased_text)
|
| 51 |
-
|
| 52 |
-
return paraphrased_text
|
| 53 |
-
|
| 54 |
-
# API Endpoint for AI Detection
|
| 55 |
-
@app.route('/api/ai-detection', methods=['POST'])
|
| 56 |
-
def ai_detection():
|
| 57 |
-
data = request.get_json()
|
| 58 |
-
text = data.get('text', '')
|
| 59 |
-
|
| 60 |
-
if text:
|
| 61 |
-
label, score = predict_en(text)
|
| 62 |
-
return jsonify({"label": label, "score": score})
|
| 63 |
-
else:
|
| 64 |
-
return jsonify({"error": "No text provided"}), 400
|
| 65 |
-
|
| 66 |
-
# API Endpoint for Paraphrasing and Grammar Correction
|
| 67 |
-
@app.route('/api/paraphrase-correct', methods=['POST'])
|
| 68 |
-
def paraphrase_and_correct_api():
|
| 69 |
-
data = request.get_json()
|
| 70 |
-
text = data.get('text', '')
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
#
|
| 79 |
-
def
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
import spacy
|
| 5 |
import subprocess
|
| 6 |
import nltk
|
| 7 |
from nltk.corpus import wordnet
|
| 8 |
from spellchecker import SpellChecker
|
|
|
|
| 9 |
|
| 10 |
+
# Initialize FastAPI app
|
| 11 |
+
app = FastAPI()
|
| 12 |
|
| 13 |
# Initialize the English text classification pipeline for AI detection
|
| 14 |
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
|
|
|
|
| 27 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 28 |
nlp = spacy.load("en_core_web_sm")
|
| 29 |
|
| 30 |
+
# Request body models
|
| 31 |
+
class TextRequest(BaseModel):
|
| 32 |
+
text: str
|
| 33 |
+
|
| 34 |
+
class TextResponse(BaseModel):
|
| 35 |
+
result: str
|
| 36 |
+
|
| 37 |
# Function to predict the label and score for English text (AI Detection)
|
| 38 |
+
def predict_en(text: str):
|
| 39 |
res = pipeline_en(text)[0]
|
| 40 |
+
return {"label": res['label'], "score": res['score']}
|
| 41 |
+
|
| 42 |
+
# Function to get synonyms using NLTK WordNet
|
| 43 |
+
def get_synonyms_nltk(word: str, pos: str):
|
| 44 |
+
pos_tag = None
|
| 45 |
+
if pos == "VERB":
|
| 46 |
+
pos_tag = wordnet.VERB
|
| 47 |
+
elif pos == "NOUN":
|
| 48 |
+
pos_tag = wordnet.NOUN
|
| 49 |
+
elif pos == "ADJ":
|
| 50 |
+
pos_tag = wordnet.ADJ
|
| 51 |
+
elif pos == "ADV":
|
| 52 |
+
pos_tag = wordnet.ADV
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
synsets = wordnet.synsets(word, pos=pos_tag)
|
| 55 |
+
if synsets:
|
| 56 |
+
lemmas = synsets[0].lemmas()
|
| 57 |
+
return [lemma.name() for lemma in lemmas]
|
| 58 |
+
return []
|
| 59 |
+
|
| 60 |
+
# Function to correct spelling errors
|
| 61 |
+
def correct_spelling(text: str):
|
| 62 |
+
words = text.split()
|
| 63 |
+
corrected_words = []
|
| 64 |
+
for word in words:
|
| 65 |
+
corrected_word = spell.correction(word)
|
| 66 |
+
corrected_words.append(corrected_word)
|
| 67 |
+
return ' '.join(corrected_words)
|
| 68 |
+
|
| 69 |
+
# Function to rephrase text and replace words with their synonyms while maintaining form
|
| 70 |
+
def rephrase_with_synonyms(text: str):
|
| 71 |
+
doc = nlp(text)
|
| 72 |
+
rephrased_text = []
|
| 73 |
+
|
| 74 |
+
for token in doc:
|
| 75 |
+
pos_tag = None
|
| 76 |
+
if token.pos_ == "NOUN":
|
| 77 |
+
pos_tag = "NOUN"
|
| 78 |
+
elif token.pos_ == "VERB":
|
| 79 |
+
pos_tag = "VERB"
|
| 80 |
+
elif token.pos_ == "ADJ":
|
| 81 |
+
pos_tag = "ADJ"
|
| 82 |
+
elif token.pos_ == "ADV":
|
| 83 |
+
pos_tag = "ADV"
|
| 84 |
+
|
| 85 |
+
if pos_tag:
|
| 86 |
+
synonyms = get_synonyms_nltk(token.text, pos_tag)
|
| 87 |
+
if synonyms:
|
| 88 |
+
synonym = synonyms[0] # Just using the first synonym for simplicity
|
| 89 |
+
if token.pos_ == "VERB":
|
| 90 |
+
if token.tag_ == "VBG": # Present participle (e.g., running)
|
| 91 |
+
synonym = synonym + 'ing'
|
| 92 |
+
elif token.tag_ == "VBD" or token.tag_ == "VBN": # Past tense or past participle
|
| 93 |
+
synonym = synonym + 'ed'
|
| 94 |
+
elif token.tag_ == "VBZ": # Third-person singular present
|
| 95 |
+
synonym = synonym + 's'
|
| 96 |
+
elif token.pos_ == "NOUN" and token.tag_ == "NNS": # Plural nouns
|
| 97 |
+
synonym += 's' if not synonym.endswith('s') else ""
|
| 98 |
+
rephrased_text.append(synonym)
|
| 99 |
+
else:
|
| 100 |
+
rephrased_text.append(token.text)
|
| 101 |
+
else:
|
| 102 |
+
rephrased_text.append(token.text)
|
| 103 |
+
|
| 104 |
+
return ' '.join(rephrased_text)
|
| 105 |
+
|
| 106 |
+
# FastAPI endpoints
|
| 107 |
+
@app.post("/predict/")
|
| 108 |
+
def predict(text_request: TextRequest):
|
| 109 |
+
return predict_en(text_request.text)
|
| 110 |
+
|
| 111 |
+
@app.post("/rephrase/")
|
| 112 |
+
def rephrase(text_request: TextRequest):
|
| 113 |
+
return {"result": rephrase_with_synonyms(text_request.text)}
|
| 114 |
+
|
| 115 |
+
@app.post("/correct-spelling/")
|
| 116 |
+
def correct_spell(text_request: TextRequest):
|
| 117 |
+
return {"result": correct_spelling(text_request.text)}
|
| 118 |
+
|
| 119 |
+
# Additional endpoints for other functionalities can be added similarly
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
import uvicorn
|
| 123 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|