Spaces:
Running
Running
Update model/model.py
Browse files- model/model.py +50 -29
model/model.py
CHANGED
@@ -2,6 +2,8 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
2 |
import torch
|
3 |
from datetime import datetime
|
4 |
import gc
|
|
|
|
|
5 |
|
6 |
class ContentAnalyzer:
|
7 |
def __init__(self):
|
@@ -72,10 +74,15 @@ class ContentAnalyzer:
|
|
72 |
def analyze_text(self, text):
|
73 |
"""Main analysis function"""
|
74 |
if not self.load_model():
|
75 |
-
return {
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
79 |
script_chunks = [text[i:i + chunk_size] for i in range(0, len(text), chunk_size - overlap)]
|
80 |
|
81 |
trigger_categories = {
|
@@ -102,30 +109,44 @@ class ContentAnalyzer:
|
|
102 |
final_triggers = [category for category, count in identified_triggers.items() if count > 0.5]
|
103 |
self.cleanup()
|
104 |
|
|
|
105 |
if not final_triggers:
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
analyzer = ContentAnalyzer()
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
return result
|
|
|
2 |
import torch
|
3 |
from datetime import datetime
|
4 |
import gc
|
5 |
+
import json
|
6 |
+
import gradio as gr
|
7 |
|
8 |
class ContentAnalyzer:
|
9 |
def __init__(self):
|
|
|
74 |
def analyze_text(self, text):
|
75 |
"""Main analysis function"""
|
76 |
if not self.load_model():
|
77 |
+
return {
|
78 |
+
"detected_triggers": {"0": "Error"},
|
79 |
+
"confidence": "Low - Model loading failed",
|
80 |
+
"model": self.model_name,
|
81 |
+
"analysis_timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
82 |
+
}
|
83 |
+
|
84 |
+
chunk_size = 256
|
85 |
+
overlap = 15
|
86 |
script_chunks = [text[i:i + chunk_size] for i in range(0, len(text), chunk_size - overlap)]
|
87 |
|
88 |
trigger_categories = {
|
|
|
109 |
final_triggers = [category for category, count in identified_triggers.items() if count > 0.5]
|
110 |
self.cleanup()
|
111 |
|
112 |
+
# Format the output as requested
|
113 |
if not final_triggers:
|
114 |
+
result = {
|
115 |
+
"detected_triggers": {"0": "None"},
|
116 |
+
"confidence": "High - No concerning content detected",
|
117 |
+
"model": self.model_name,
|
118 |
+
"analysis_timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
119 |
+
}
|
120 |
+
else:
|
121 |
+
triggers_dict = {str(i): trigger for i, trigger in enumerate(final_triggers)}
|
122 |
+
result = {
|
123 |
+
"detected_triggers": triggers_dict,
|
124 |
+
"confidence": "High - Content detected",
|
125 |
+
"model": self.model_name,
|
126 |
+
"analysis_timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
127 |
+
}
|
128 |
+
|
129 |
+
return result
|
130 |
+
|
131 |
+
def analyze_content(text):
|
132 |
+
"""Function to be called from Gradio interface"""
|
133 |
analyzer = ContentAnalyzer()
|
134 |
+
result = analyzer.analyze_text(text)
|
135 |
+
return json.dumps(result, indent=2)
|
136 |
+
|
137 |
+
# Gradio interface
|
138 |
+
def gradio_interface(text):
|
139 |
+
return analyze_content(text)
|
140 |
+
|
141 |
+
# Create and launch the Gradio interface
|
142 |
+
iface = gr.Interface(
|
143 |
+
fn=gradio_interface,
|
144 |
+
inputs=gr.Textbox(lines=8, label="Input Text"),
|
145 |
+
outputs=gr.JSON(),
|
146 |
+
title="Content Analysis",
|
147 |
+
description="Analyze text content for sensitive topics"
|
148 |
+
)
|
149 |
+
|
150 |
+
# Launch the interface
|
151 |
+
if __name__ == "__main__":
|
152 |
+
iface.launch()
|
|