Spaces:
Running
Running
Commit
·
e4c9f8c
1
Parent(s):
9c86082
Gave access to Llama 3.2
Browse files- model/analyzer.py +18 -16
- requirements.txt +3 -1
model/analyzer.py
CHANGED
@@ -4,6 +4,8 @@ import torch
|
|
4 |
from datetime import datetime
|
5 |
import gc
|
6 |
import json
|
|
|
|
|
7 |
|
8 |
class ContentAnalyzer:
|
9 |
def __init__(self):
|
@@ -11,24 +13,36 @@ class ContentAnalyzer:
|
|
11 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
self.tokenizer = None
|
13 |
self.model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def load_model(self):
|
16 |
try:
|
17 |
print("Loading tokenizer...")
|
18 |
-
self.tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
|
|
|
|
19 |
|
20 |
print(f"Loading model on {self.device}...")
|
21 |
self.model = AutoModelForCausalLM.from_pretrained(
|
22 |
self.model_name,
|
23 |
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
24 |
low_cpu_mem_usage=True,
|
25 |
-
device_map="auto"
|
|
|
26 |
)
|
27 |
return True
|
28 |
except Exception as e:
|
29 |
print(f"Model loading error: {str(e)}")
|
30 |
return False
|
31 |
|
|
|
32 |
def cleanup(self):
|
33 |
if self.device == "cuda":
|
34 |
torch.cuda.empty_cache()
|
@@ -96,7 +110,7 @@ class ContentAnalyzer:
|
|
96 |
for chunk_idx, chunk in enumerate(script_chunks, 1):
|
97 |
print(f"\n--- Processing Chunk {chunk_idx}/{len(script_chunks)} ---")
|
98 |
for category, info in trigger_categories.items():
|
99 |
-
|
100 |
if response == "YES":
|
101 |
identified_triggers[category] = identified_triggers.get(category, 0) + 1
|
102 |
elif response == "MAYBE":
|
@@ -126,16 +140,4 @@ class ContentAnalyzer:
|
|
126 |
def analyze_content(text):
|
127 |
analyzer = ContentAnalyzer()
|
128 |
result = analyzer.analyze_text(text)
|
129 |
-
return json.dumps(result, indent=2)
|
130 |
-
|
131 |
-
# Create and launch the Gradio interface
|
132 |
-
iface = gr.Interface(
|
133 |
-
fn=analyze_content,
|
134 |
-
inputs=gr.Textbox(lines=8, label="Input Text"),
|
135 |
-
outputs=gr.JSON(),
|
136 |
-
title="Content Analysis",
|
137 |
-
description="Analyze text content for sensitive topics"
|
138 |
-
)
|
139 |
-
|
140 |
-
if __name__ == "__main__":
|
141 |
-
iface.launch()
|
|
|
4 |
from datetime import datetime
|
5 |
import gc
|
6 |
import json
|
7 |
+
import os
|
8 |
+
from huggingface_hub import login
|
9 |
|
10 |
class ContentAnalyzer:
|
11 |
def __init__(self):
|
|
|
13 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
self.tokenizer = None
|
15 |
self.model = None
|
16 |
+
# Authenticate with Hugging Face
|
17 |
+
if "HF_TOKEN" in os.environ:
|
18 |
+
print("Authenticating with Hugging Face...")
|
19 |
+
login(token=os.environ["HF_TOKEN"])
|
20 |
+
else:
|
21 |
+
print("Warning: HF_TOKEN not found in environment variables")
|
22 |
|
23 |
def load_model(self):
|
24 |
try:
|
25 |
print("Loading tokenizer...")
|
26 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
27 |
+
self.model_name,
|
28 |
+
use_fast=True,
|
29 |
+
token=os.environ.get("HF_TOKEN") # Add token here
|
30 |
+
)
|
31 |
|
32 |
print(f"Loading model on {self.device}...")
|
33 |
self.model = AutoModelForCausalLM.from_pretrained(
|
34 |
self.model_name,
|
35 |
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
36 |
low_cpu_mem_usage=True,
|
37 |
+
device_map="auto",
|
38 |
+
token=os.environ.get("HF_TOKEN") # Add token here
|
39 |
)
|
40 |
return True
|
41 |
except Exception as e:
|
42 |
print(f"Model loading error: {str(e)}")
|
43 |
return False
|
44 |
|
45 |
+
# Rest of your code remains exactly the same
|
46 |
def cleanup(self):
|
47 |
if self.device == "cuda":
|
48 |
torch.cuda.empty_cache()
|
|
|
110 |
for chunk_idx, chunk in enumerate(script_chunks, 1):
|
111 |
print(f"\n--- Processing Chunk {chunk_idx}/{len(script_chunks)} ---")
|
112 |
for category, info in trigger_categories.items():
|
113 |
+
score, response = self.analyze_chunk(chunk, info)
|
114 |
if response == "YES":
|
115 |
identified_triggers[category] = identified_triggers.get(category, 0) + 1
|
116 |
elif response == "MAYBE":
|
|
|
140 |
def analyze_content(text):
|
141 |
analyzer = ContentAnalyzer()
|
142 |
result = analyzer.analyze_text(text)
|
143 |
+
return json.dumps(result, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -3,4 +3,6 @@ flask_cors
|
|
3 |
torch
|
4 |
gradio
|
5 |
transformers
|
6 |
-
accelerate
|
|
|
|
|
|
3 |
torch
|
4 |
gradio
|
5 |
transformers
|
6 |
+
accelerate
|
7 |
+
safetensors
|
8 |
+
huggingface-hub
|