Mohammedmarzuk17 commited on
Commit
d4d9ecd
·
verified ·
1 Parent(s): f63fa9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -42
app.py CHANGED
@@ -1,25 +1,29 @@
1
- Python 3.12.10 (tags/v3.12.10:0cc8128, Apr 8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)] on win32
2
- Enter "help" below or click "Help" above for more information.
3
  import gradio as gr
4
  from transformers import pipeline
5
 
6
  # ---------------------------
7
  # Load Models
8
  # ---------------------------
 
 
9
  claim_model_name = "microsoft/deberta-v3-base-zeroshot-v1.1"
10
- claim_classifier = pipeline("zero-shot-classification", model=claim_model_name, device=0)
11
  claim_labels = ["factual claim", "opinion", "personal anecdote", "other"]
12
 
 
13
  ai_detect_model_name = "roberta-base-openai-detector"
14
- ai_detector = pipeline("text-classification", model=ai_detect_model_name, device=0)
15
 
 
16
  nli_model_name = "valhalla/distilbart-mnli-12-3"
17
- nli_pipeline = pipeline("text-classification", model=nli_model_name, tokenizer=nli_model_name, device=0)
18
 
19
  # ---------------------------
20
  # Functions
21
  # ---------------------------
 
22
  def extract_claims(page_text):
 
23
  sentences = [s.strip() for s in page_text.split(".") if len(s.strip()) > 5]
24
  results = []
25
  for s in sentences:
@@ -29,6 +33,7 @@ def extract_claims(page_text):
29
  return results[:5]
30
 
31
  def detect_ai(texts):
 
32
  if isinstance(texts, str):
33
  texts = [texts]
34
  results = []
@@ -38,56 +43,81 @@ def detect_ai(texts):
38
  return results
39
 
40
  def fact_check(claims, evidence_text):
 
41
  if isinstance(claims, str):
42
  claims = [claims]
43
  results = []
44
  for c in claims:
45
- out = nli_pipeline(hypothesis=c, sequence_pair=evidence_text)
 
 
46
  results.append({"claim": c, "label": out[0]["label"], "score": round(out[0]["score"], 3)})
47
  return results
48
 
49
  # ---------------------------
50
  # Unified Predict Function
51
  # ---------------------------
 
52
  def predict(page_text="", selected_text="", evidence_text=""):
53
  """
54
  1. Extract top 5 claims from page_text
55
  2. Run AI Detection on claims + selected_text
56
- 3. Run Fact-Checking on claims + evidence_text if provided
57
  """
58
- # Extract claims
59
  claims = extract_claims(page_text) if page_text else []
60
- ...
61
- ... # Combine claims + selected text for AI detection
62
- ... ai_input = claims.copy()
63
- ... if selected_text:
64
- ... ai_input.append(selected_text)
65
- ... ai_results = detect_ai(ai_input) if ai_input else []
66
- ...
67
- ... # Fact-checking: only if evidence is provided
68
- ... fc_results = fact_check(claims + ([selected_text] if selected_text else []), evidence_text) if evidence_text else []
69
- ...
70
- ... return {
71
- ... "claims": claims,
72
- ... "ai_detection": ai_results,
73
- ... "fact_checking": fc_results
74
- ... }
75
- ...
76
- ... # ---------------------------
77
- ... # Gradio UI
78
- ... # ---------------------------
79
- ... with gr.Blocks() as demo:
80
- ... gr.Markdown("## EduShield AI Backend - Predict API & UI")
81
- ...
82
- ... page_text_input = gr.Textbox(label="Full Page Text", lines=10, placeholder="Paste page text here...")
83
- ... selected_text_input = gr.Textbox(label="Selected Text", lines=5, placeholder="Paste selected text here...")
84
- ... evidence_input = gr.Textbox(label="Evidence Text", lines=5, placeholder="Paste evidence text here...")
85
- ... predict_btn = gr.Button("Run Predict")
86
- ... output_json = gr.JSON(label="Predict Results")
87
- ... predict_btn.click(predict, inputs=[page_text_input, selected_text_input, evidence_input], outputs=output_json)
88
- ...
89
- ... # ---------------------------
90
- ... # Launch
91
- ... # ---------------------------
92
- ... if __name__ == "__main__":
93
- ... demo.launch(server_name="0.0.0.0")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
  # ---------------------------
5
  # Load Models
6
  # ---------------------------
7
+
8
+ # Claim Extraction
9
  claim_model_name = "microsoft/deberta-v3-base-zeroshot-v1.1"
10
+ claim_classifier = pipeline("zero-shot-classification", model=claim_model_name)
11
  claim_labels = ["factual claim", "opinion", "personal anecdote", "other"]
12
 
13
+ # AI-Generated Text Detection
14
  ai_detect_model_name = "roberta-base-openai-detector"
15
+ ai_detector = pipeline("text-classification", model=ai_detect_model_name)
16
 
17
+ # Fact-Checking (NLI)
18
  nli_model_name = "valhalla/distilbart-mnli-12-3"
19
+ nli_pipeline = pipeline("text-classification", model=nli_model_name, tokenizer=nli_model_name)
20
 
21
  # ---------------------------
22
  # Functions
23
  # ---------------------------
24
+
25
  def extract_claims(page_text):
26
+ """Return top 5 factual claims from page text"""
27
  sentences = [s.strip() for s in page_text.split(".") if len(s.strip()) > 5]
28
  results = []
29
  for s in sentences:
 
33
  return results[:5]
34
 
35
  def detect_ai(texts):
36
+ """Detect AI-generated text for a list of strings"""
37
  if isinstance(texts, str):
38
  texts = [texts]
39
  results = []
 
43
  return results
44
 
45
  def fact_check(claims, evidence_text):
46
+ """Perform fact-checking using NLI: ENTAILMENT / CONTRADICTION / NEUTRAL"""
47
  if isinstance(claims, str):
48
  claims = [claims]
49
  results = []
50
  for c in claims:
51
+ # Combine claim + evidence for NLI input
52
+ nli_input = f"{c} </s></s> {evidence_text}"
53
+ out = nli_pipeline(nli_input)
54
  results.append({"claim": c, "label": out[0]["label"], "score": round(out[0]["score"], 3)})
55
  return results
56
 
57
  # ---------------------------
58
  # Unified Predict Function
59
  # ---------------------------
60
+
61
  def predict(page_text="", selected_text="", evidence_text=""):
62
  """
63
  1. Extract top 5 claims from page_text
64
  2. Run AI Detection on claims + selected_text
65
+ 3. Run Fact-Checking on claims + selected_text if evidence provided
66
  """
67
+ # 1️⃣ Extract claims
68
  claims = extract_claims(page_text) if page_text else []
69
+
70
+ # 2️⃣ AI Detection (claims + selected text)
71
+ ai_input = claims.copy()
72
+ if selected_text:
73
+ ai_input.append(selected_text)
74
+ ai_results = detect_ai(ai_input) if ai_input else []
75
+
76
+ # 3️⃣ Fact-Checking (claims + selected text)
77
+ fc_input = claims.copy()
78
+ if selected_text:
79
+ fc_input.append(selected_text)
80
+ fc_results = fact_check(fc_input, evidence_text) if evidence_text else []
81
+
82
+ return {
83
+ "claims": claims,
84
+ "ai_detection": ai_results,
85
+ "fact_checking": fc_results
86
+ }
87
+
88
+ # ---------------------------
89
+ # Gradio UI
90
+ # ---------------------------
91
+
92
+ with gr.Blocks() as demo:
93
+ gr.Markdown("## EduShield AI Backend - Predict API & UI")
94
+
95
+ with gr.Tab("Predict"):
96
+ page_text_input = gr.Textbox(label="Full Page Text", lines=10, placeholder="Paste page text here...")
97
+ selected_text_input = gr.Textbox(label="Selected Text", lines=5, placeholder="Paste selected text here...")
98
+ evidence_input = gr.Textbox(label="Evidence Text", lines=5, placeholder="Paste evidence text here...")
99
+ predict_btn = gr.Button("Run Predict")
100
+ output_json = gr.JSON(label="Predict Results")
101
+ predict_btn.click(
102
+ fn=predict,
103
+ inputs=[page_text_input, selected_text_input, evidence_input],
104
+ outputs=output_json
105
+ )
106
+
107
+ with gr.Tab("Instructions"):
108
+ gr.Markdown("""
109
+ ### How to Use EduShield AI Backend
110
+ 1. **Full Page Text**: Paste all visible text from a page. The system will extract the top 5 factual claims.
111
+ 2. **Selected Text**: Paste any specific text to detect AI-generation and/or fact-check.
112
+ 3. **Evidence Text**: Paste evidence against which to fact-check the claims.
113
+ 4. Click **Run Predict** to get results:
114
+ - **Claims**: Top factual statements from the page
115
+ - **AI Detection**: Label + probability for AI-generated content
116
+ - **Fact-Checking**: ENTAILMENT / CONTRADICTION / NEUTRAL with score
117
+ """)
118
+
119
+ # ---------------------------
120
+ # Launch
121
+ # ---------------------------
122
+ if __name__ == "__main__":
123
+ demo.launch()