JohnLicode commited on
Commit
cdd1f84
·
verified ·
1 Parent(s): dd40489

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +98 -19
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import os
4
 
5
  # Load model
6
  print("Loading model...")
@@ -12,7 +12,7 @@ classifier = pipeline(
12
  print("Model loaded!")
13
 
14
  def classify_ethics(text: str, guideline_id: str = "", guideline_name: str = ""):
15
- """Classify text for ethics guideline compliance."""
16
 
17
  # Format input like training data
18
  if guideline_id and guideline_name:
@@ -39,23 +39,102 @@ def classify_ethics(text: str, guideline_id: str = "", guideline_name: str = "")
39
  "input_preview": input_text[:100] + "..."
40
  }
41
 
42
- # Create Gradio interface
43
- demo = gr.Interface(
44
- fn=classify_ethics,
45
- inputs=[
46
- gr.Textbox(label="Text to Analyze", lines=5, placeholder="Enter the text from research proposal..."),
47
- gr.Textbox(label="Guideline ID (optional)", placeholder="e.g., 1.1"),
48
- gr.Textbox(label="Guideline Name (optional)", placeholder="e.g., Objectives"),
49
- ],
50
- outputs=gr.JSON(label="Classification Result"),
51
- title="Ethics Review Classifier",
52
- description="Classify research proposal text against ethics guidelines. Returns ADDRESSED or NEEDS_REVISION.",
53
- examples=[
54
- ["The general objective is to develop an AI ethics review system. Specific objectives: 1) Create scanning module 2) Implement matching.", "1.1", "Objectives"],
55
- ["All participant data will be encrypted using AES-256 and stored securely.", "3.2", "Privacy and confidentiality"],
56
- ["The study explores innovative approaches.", "1.7", "Sampling design and size"],
57
- ],
58
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  # Launch with API enabled
61
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import json
4
 
5
  # Load model
6
  print("Loading model...")
 
12
  print("Model loaded!")
13
 
14
  def classify_ethics(text: str, guideline_id: str = "", guideline_name: str = ""):
15
+ """Classify single text for ethics guideline compliance."""
16
 
17
  # Format input like training data
18
  if guideline_id and guideline_name:
 
39
  "input_preview": input_text[:100] + "..."
40
  }
41
 
42
+
43
+ def classify_batch(batch_json: str):
44
+ """
45
+ Classify multiple texts in a single API call for better performance.
46
+
47
+ Input: JSON string with format:
48
+ [
49
+ {"text": "...", "guideline_id": "1.1", "guideline_name": "Objectives"},
50
+ {"text": "...", "guideline_id": "3.2", "guideline_name": "Privacy"},
51
+ ...
52
+ ]
53
+
54
+ Output: JSON string with results for each input.
55
+ """
56
+ try:
57
+ items = json.loads(batch_json)
58
+ except json.JSONDecodeError as e:
59
+ return json.dumps({"error": f"Invalid JSON: {str(e)}"})
60
+
61
+ if not isinstance(items, list):
62
+ return json.dumps({"error": "Input must be a JSON array"})
63
+
64
+ if len(items) > 50:
65
+ return json.dumps({"error": "Maximum 50 items per batch"})
66
+
67
+ # Prepare all inputs
68
+ formatted_inputs = []
69
+ for item in items:
70
+ text = item.get("text", "")
71
+ g_id = item.get("guideline_id", "")
72
+ g_name = item.get("guideline_name", "")
73
+
74
+ if g_id and g_name:
75
+ input_text = f"Guideline {g_id} {g_name}: {text}"
76
+ else:
77
+ input_text = text
78
+
79
+ formatted_inputs.append(input_text[:1500])
80
+
81
+ # Run batch inference (much faster than individual calls)
82
+ predictions = classifier(formatted_inputs)
83
+
84
+ # Format results
85
+ results = []
86
+ for pred in predictions:
87
+ label = pred['label']
88
+ if label == "LABEL_0":
89
+ label = "ADDRESSED"
90
+ elif label == "LABEL_1":
91
+ label = "NEEDS_REVISION"
92
+
93
+ results.append({
94
+ "label": label,
95
+ "score": round(pred['score'], 4)
96
+ })
97
+
98
+ return json.dumps(results)
99
+
100
+
101
+ # Create Gradio interface with both single and batch endpoints
102
+ with gr.Blocks(title="Ethics Review Classifier") as demo:
103
+ gr.Markdown("# Ethics Review Classifier")
104
+ gr.Markdown("Classify research proposal text against ethics guidelines. Returns ADDRESSED or NEEDS_REVISION.")
105
+
106
+ with gr.Tab("Single Classification"):
107
+ with gr.Row():
108
+ with gr.Column():
109
+ text_input = gr.Textbox(label="Text to Analyze", lines=5, placeholder="Enter the text from research proposal...")
110
+ id_input = gr.Textbox(label="Guideline ID (optional)", placeholder="e.g., 1.1")
111
+ name_input = gr.Textbox(label="Guideline Name (optional)", placeholder="e.g., Objectives")
112
+ single_btn = gr.Button("Classify", variant="primary")
113
+ with gr.Column():
114
+ single_output = gr.JSON(label="Result")
115
+
116
+ single_btn.click(classify_ethics, inputs=[text_input, id_input, name_input], outputs=single_output)
117
+
118
+ gr.Examples(
119
+ examples=[
120
+ ["The general objective is to develop an AI ethics review system. Specific objectives: 1) Create scanning module 2) Implement matching.", "1.1", "Objectives"],
121
+ ["All participant data will be encrypted using AES-256 and stored securely.", "3.2", "Privacy and confidentiality"],
122
+ ["The study explores innovative approaches.", "1.7", "Sampling design and size"],
123
+ ],
124
+ inputs=[text_input, id_input, name_input],
125
+ )
126
+
127
+ with gr.Tab("Batch Classification (Fast)"):
128
+ gr.Markdown("**For API users:** Send up to 50 items in one request for faster processing.")
129
+ batch_input = gr.Textbox(
130
+ label="Batch Input (JSON Array)",
131
+ lines=10,
132
+ placeholder='[{"text": "...", "guideline_id": "1.1", "guideline_name": "Objectives"}, ...]'
133
+ )
134
+ batch_btn = gr.Button("Classify Batch", variant="primary")
135
+ batch_output = gr.Textbox(label="Batch Results (JSON)", lines=10)
136
+
137
+ batch_btn.click(classify_batch, inputs=[batch_input], outputs=batch_output)
138
 
139
  # Launch with API enabled
140
  demo.launch()