CoolSpring commited on
Commit
0d8bade
·
unverified ·
1 Parent(s): 28f34f2

Optimize sentence-level analysis

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -39,7 +39,15 @@ def classify_text(text):
39
 
40
  def highlight_suspicious_sentences(text):
41
  sentences = sent_tokenize(text)
42
- scores = [classify_text(sentence) for sentence in sentences]
 
 
 
 
 
 
 
 
43
  return sentences, scores
44
 
45
 
@@ -69,21 +77,22 @@ if text_input != st.session_state.text_input:
69
 
70
  if st.button("Analyze and Highlight"):
71
  if text_input:
72
- overall_probability = classify_text(text_input)
73
- st.markdown(
74
- f"<h3>Overall probability of being AI-generated: <span style='color: {'red' if overall_probability > 0.5 else 'green'};'>{overall_probability:.2%}</span></h3>",
75
- unsafe_allow_html=True,
76
- )
77
-
78
- st.markdown("### Sentence-level analysis:")
79
- sentences, scores = highlight_suspicious_sentences(text_input)
80
-
81
- for sentence, score in zip(sentences, scores):
82
- color = get_color(score)
83
  st.markdown(
84
- f"<div style='background-color: {color}; padding: 10px; margin: 5px 0; border-radius: 5px;'><strong>{score:.2%}</strong> - {sentence}</div>",
85
  unsafe_allow_html=True,
86
  )
 
 
 
 
 
 
 
 
 
 
87
  else:
88
  st.warning("Please enter some text to analyze.")
89
 
 
39
 
40
  def highlight_suspicious_sentences(text):
41
  sentences = sent_tokenize(text)
42
+ inputs = tokenizer(
43
+ sentences, return_tensors="pt", truncation=True, max_length=512, padding=True
44
+ )
45
+ with torch.no_grad():
46
+ logits = model(**inputs).logits
47
+ probabilities = torch.softmax(logits, dim=1)
48
+ scores = probabilities[
49
+ :, 1
50
+ ].tolist() # Probability of being AI-generated for each sentence
51
  return sentences, scores
52
 
53
 
 
77
 
78
  if st.button("Analyze and Highlight"):
79
  if text_input:
80
+ with st.spinner("Analyzing text..."):
81
+ overall_probability = classify_text(text_input)
 
 
 
 
 
 
 
 
 
82
  st.markdown(
83
+ f"<h3>Overall probability of being AI-generated: <span style='color: {'red' if overall_probability > 0.5 else 'green'};'>{overall_probability:.2%}</span></h3>",
84
  unsafe_allow_html=True,
85
  )
86
+
87
+ st.markdown("### Sentence-level analysis:")
88
+ sentences, scores = highlight_suspicious_sentences(text_input)
89
+
90
+ for sentence, score in zip(sentences, scores):
91
+ color = get_color(score)
92
+ st.markdown(
93
+ f"<div style='background-color: {color}; padding: 10px; margin: 5px 0; border-radius: 5px;'><strong>{score:.2%}</strong> - {sentence}</div>",
94
+ unsafe_allow_html=True,
95
+ )
96
  else:
97
  st.warning("Please enter some text to analyze.")
98