Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality for checking if text is generated by chatgpt #9

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions check_gpt/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from flask import Flask, request, jsonify
import Levenshtein
from openai import OpenAI

app = Flask(__name__)

client = OpenAI()

# calls ChatGPT API with given prompt.
def get_chatgpt_response(prompt: str) -> str:
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are going to write the opposite of the current text."},
{"role": "user", "content": prompt}
]
)
return completion.choices[0].message.content

# Compute Levenshtein ratio for edit distance
def levenshtein_ratio(text1: str, text2:str) -> float:
"""
A result closer to 1: less edits made
A result closer to 0: more edits made
"""

if len(text1) == 0 and len(text2) == 0:
return 1.0

distance = Levenshtein.distance(text1, text2)
max_len = max(len(text1), len(text2))
ratio = 1 - (distance / max_len)

return ratio


# Function to analyze texts based on Levenshtein ratio
def analyze_texts(original_text: str, paraphrased_text: str, ratio_threshold=0.6)-> bool:
"""
Will return True if generated by ChatGPT, False otherwise
"""
ratio = levenshtein_ratio(original_text, paraphrased_text)
return True if ratio < ratio_threshold else False


# Main function to compare the original text and check if AI-generated
def execute_gpt_comparsion(original_text:str) -> bool:
"""
Return True if written by a human
Return False if written by ChatGPT
"""
first_rewrite = get_chatgpt_response(original_text)
paraphrased_text = get_chatgpt_response(first_rewrite)

result = analyze_texts(original_text, paraphrased_text)
answer = "Likely written by a human"

if result:
answer = "Likely AI generated"

print(f"Result: {answer}")
return result


# Flask route for executing the GPT comparison
@app.route('/api/check-text', methods=['POST'])
def check_text():
data = request.get_json()
original_text = data.get('text', '')

if not original_text:
return jsonify({"error": "No text provided"}), 400

result = execute_gpt_comparsion(original_text)

return jsonify({"result": result})


if __name__ == '__main__':
app.run(debug=True)
37 changes: 37 additions & 0 deletions check_gpt/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
annotated-types==0.7.0
anyio==4.6.0
certifi==2024.8.30
charset-normalizer==3.3.2
distro==1.9.0
filelock==3.16.1
fsspec==2024.9.0
h11==0.14.0
httpcore==1.0.6
httpx==0.27.2
huggingface-hub==0.25.1
idna==3.10
Jinja2==3.1.4
jiter==0.5.0
Levenshtein==0.26.0
MarkupSafe==2.1.5
mpmath==1.3.0
networkx==3.3
numpy==2.1.2
openai==1.51.0
packaging==24.1
pydantic==2.9.2
pydantic_core==2.23.4
PyYAML==6.0.2
RapidFuzz==3.10.0
regex==2024.9.11
requests==2.32.3
safetensors==0.4.5
setuptools==75.1.0
sniffio==1.3.1
sympy==1.13.3
tokenizers==0.20.0
torch==2.4.1
tqdm==4.66.5
transformers==4.45.1
typing_extensions==4.12.2
urllib3==2.2.3