taha092 commited on
Commit
208bdb2
·
verified ·
1 Parent(s): 78bbe6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -27
app.py CHANGED
@@ -1,11 +1,10 @@
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
4
  from sentence_transformers import SentenceTransformer, util
5
  import numpy as np
6
- import requests
7
  import gradio.themes as grthemes
8
- import config
9
  import random
10
  import re
11
 
@@ -23,6 +22,12 @@ paraphrase_model = paraphrase_model.to(device)
23
  # ----------------------
24
  similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
25
 
 
 
 
 
 
 
26
  # ----------------------
27
  # Prompt Variations for Humanization
28
  # ----------------------
@@ -41,7 +46,6 @@ PROMPT_VARIANTS = [
41
  # Sentence Splitter
42
  # ----------------------
43
  def split_sentences(text):
44
- # Simple sentence splitter (can be improved for edge cases)
45
  sentences = re.split(r'(?<=[.!?])\s+', text.strip())
46
  return [s for s in sentences if s]
47
 
@@ -49,7 +53,6 @@ def split_sentences(text):
49
  # Light Post-Processing
50
  # ----------------------
51
  def postprocess_text(text):
52
- # Add contractions, simple idioms, and vary sentence length a bit
53
  contractions = {
54
  "do not": "don't", "cannot": "can't", "will not": "won't", "I am": "I'm",
55
  "is not": "isn't", "are not": "aren't", "did not": "didn't", "it is": "it's",
@@ -57,7 +60,6 @@ def postprocess_text(text):
57
  }
58
  for k, v in contractions.items():
59
  text = re.sub(rf'\b{k}\b', v, text, flags=re.IGNORECASE)
60
- # Add a simple idiom randomly
61
  idioms = [
62
  "at the end of the day", "to be honest", "as a matter of fact", "for what it's worth",
63
  "in a nutshell", "the bottom line is", "all things considered"
@@ -107,32 +109,20 @@ def semantic_similarity(text1, text2):
107
  return sim
108
 
109
  # ----------------------
110
- # Real AI Detection (Winston AI API)
111
  # ----------------------
112
  def check_ai_score(text):
113
- api_key = config.WINSTON_AI_API_KEY
114
- api_url = config.WINSTON_AI_API_URL
115
- if not api_key:
116
- return None, "No API key set. Please add your Winston AI API key to config.py."
117
- headers = {
118
- "Authorization": f"Bearer {api_key}",
119
- "Content-Type": "application/json"
120
- }
121
- data = {"text": text, "sentences": False}
122
  try:
123
- response = requests.post(api_url, headers=headers, json=data, timeout=30)
124
- if response.status_code == 200:
125
- result = response.json()
126
- score = result.get("score", None)
127
- if score is not None:
128
- ai_prob = 1.0 - (score / 100.0)
129
- return ai_prob, None
130
- else:
131
- return None, "No score in Winston AI response."
132
- else:
133
- return None, f"Winston AI error: {response.status_code} {response.text}"
134
  except Exception as e:
135
- return None, f"Winston AI exception: {str(e)}"
136
 
137
  # ----------------------
138
  # Humanization Score & Rating
 
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ from transformers.pipelines import pipeline
5
  from sentence_transformers import SentenceTransformer, util
6
  import numpy as np
 
7
  import gradio.themes as grthemes
 
8
  import random
9
  import re
10
 
 
22
  # ----------------------
23
  similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
24
 
25
+ # ----------------------
26
+ # Local AI Detector (desklib/ai-text-detector-v1.01)
27
+ # ----------------------
28
+ AI_DETECTOR_MODEL = "desklib/ai-text-detector-v1.01"
29
+ ai_detector = pipeline("text-classification", model=AI_DETECTOR_MODEL, device=0 if torch.cuda.is_available() else -1)
30
+
31
  # ----------------------
32
  # Prompt Variations for Humanization
33
  # ----------------------
 
46
  # Sentence Splitter
47
  # ----------------------
48
  def split_sentences(text):
 
49
  sentences = re.split(r'(?<=[.!?])\s+', text.strip())
50
  return [s for s in sentences if s]
51
 
 
53
  # Light Post-Processing
54
  # ----------------------
55
  def postprocess_text(text):
 
56
  contractions = {
57
  "do not": "don't", "cannot": "can't", "will not": "won't", "I am": "I'm",
58
  "is not": "isn't", "are not": "aren't", "did not": "didn't", "it is": "it's",
 
60
  }
61
  for k, v in contractions.items():
62
  text = re.sub(rf'\b{k}\b', v, text, flags=re.IGNORECASE)
 
63
  idioms = [
64
  "at the end of the day", "to be honest", "as a matter of fact", "for what it's worth",
65
  "in a nutshell", "the bottom line is", "all things considered"
 
109
  return sim
110
 
111
  # ----------------------
112
+ # Local AI Detection Function
113
  # ----------------------
114
  def check_ai_score(text):
 
 
 
 
 
 
 
 
 
115
  try:
116
+ result = ai_detector(text)
117
+ for r in result:
118
+ # LABEL_1 = AI, LABEL_0 = Human
119
+ if r['label'] in ['LABEL_1', 'Fake']:
120
+ return r['score'], None
121
+ elif r['label'] in ['LABEL_0', 'Real']:
122
+ return 1.0 - r['score'], None
123
+ return 0.5, None # fallback
 
 
 
124
  except Exception as e:
125
+ return None, f"AI detection error: {str(e)}"
126
 
127
  # ----------------------
128
  # Humanization Score & Rating