ehtyalee commited on
Commit
afc262e
·
verified ·
1 Parent(s): 34831e0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +640 -0
  2. requirements.txt +14 -0
app.py ADDED
@@ -0,0 +1,640 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --- Imports ---
2
+ import os
3
+ import gradio as gr
4
+ import pickle # Keep for loading the custom image model pkl
5
+ import torch
6
+ import numpy as np
7
+ from transformers import (
8
+ AutoTokenizer,
9
+ AutoModelForSequenceClassification,
10
+ pipeline,
11
+ AutoFeatureExtractor # Needed for custom ViT model
12
+ )
13
+ from huggingface_hub import login, HfFolder # Added HfFolder for token check
14
+ from PIL import Image
15
+ import requests
16
+ from io import BytesIO
17
+ import torchvision.transforms as transforms
18
+ import traceback
19
+
20
+ # --- Hugging Face Token Handling (Using Secrets) ---
21
+ # Load token from environment variable if available (recommended for Spaces)
22
+ HF_TOKEN = os.environ.get("HF_TOKEN")
23
+
24
+ # Attempt login using the token from secrets
25
+ logged_in = False
26
+ if HF_TOKEN:
27
+ try:
28
+ login(token=HF_TOKEN)
29
+ logged_in = True
30
+ print("Successfully logged in to Hugging Face Hub using token from environment variable.")
31
+ except Exception as e:
32
+ print(f"Hugging Face Hub login using provided token failed: {e}")
33
+ print("Proceeding without explicit login. Private models may fail.")
34
+ else:
35
+ # Check if already logged in via CLI/notebook login
36
+ if HfFolder.get_token():
37
+ print("Already logged in to Hugging Face Hub (found existing token).")
38
+ logged_in = True
39
+ HF_TOKEN = HfFolder.get_token() # Use existing token if needed later
40
+ else:
41
+ print("HF_TOKEN secret not set. Proceeding without login. Public models should still work.")
42
+ print("If you need to use private models, add HF_TOKEN as a secret to this Space.")
43
+
44
+ # --- CombinedAnalyzer Class Definition ---
45
+ # (Keep this class exactly as you provided it)
46
+ class CombinedAnalyzer:
47
+ """
48
+ A class to encapsulate sentiment analysis and AI text detection pipelines for reviews.
49
+ """
50
+ def __init__(self,
51
+ sentiment_model_name="distilbert-base-uncased-finetuned-sst-2-english",
52
+ detector_model_name="Hello-SimpleAI/chatgpt-detector-roberta",
53
+ auth_token=None):
54
+ print(f"Initializing CombinedAnalyzer with Sentiment: '{sentiment_model_name}' and Detector: '{detector_model_name}'...")
55
+ self.device = 0 if torch.cuda.is_available() else -1 # Use pipeline's device handling convention (-1 for CPU, >=0 for GPU)
56
+ self.sentiment_model_name = sentiment_model_name
57
+ self.detector_model_name = detector_model_name
58
+ self.sentiment_pipeline = None
59
+ self.detector_pipeline = None
60
+ # --- Load pipelines INSIDE __init__ ---
61
+ try:
62
+ print(f" -> Loading sentiment pipeline: {self.sentiment_model_name}")
63
+ self.sentiment_pipeline = pipeline("sentiment-analysis", model=self.sentiment_model_name, device=self.device, token=auth_token if auth_token else None)
64
+ print(" -> Sentiment pipeline loaded.")
65
+ except Exception as e:
66
+ print(f"ERROR loading sentiment pipeline '{self.sentiment_model_name}': {e}")
67
+ try:
68
+ print(f" -> Loading AI text detector pipeline: {self.detector_model_name}")
69
+ self.detector_pipeline = pipeline("text-classification", model=self.detector_model_name, device=self.device, token=auth_token if auth_token else None)
70
+ print(" -> AI text detector pipeline loaded.")
71
+ except Exception as e:
72
+ print(f"ERROR loading AI text detector pipeline '{self.detector_model_name}': {e}")
73
+ print("CombinedAnalyzer initialization complete.")
74
+
75
+ def analyze(self, text):
76
+ """Analyzes text for sentiment and AI generation likelihood."""
77
+ if not isinstance(text, str) or not text.strip():
78
+ return {
79
+ "sentiment_label": "N/A", "sentiment_score": 0,
80
+ "authenticity_label": "N/A", "authenticity_score": 0,
81
+ "error": "Input text cannot be empty."
82
+ }
83
+ results = {}
84
+ # 1. Sentiment Analysis
85
+ if self.sentiment_pipeline and callable(self.sentiment_pipeline):
86
+ try:
87
+ sentiment_result = self.sentiment_pipeline(text)[0]
88
+ results['sentiment_label'] = sentiment_result['label']
89
+ results['sentiment_score'] = round(sentiment_result['score'] * 100, 2)
90
+ except Exception as e:
91
+ print(f"Sentiment Analysis Error: {e}")
92
+ results['sentiment_label'] = "Error"
93
+ results['sentiment_score'] = 0
94
+ results['error'] = results.get('error', '') + f" Sentiment Error: {e};"
95
+ else:
96
+ results['sentiment_label'] = "Model N/A"
97
+ results['sentiment_score'] = 0
98
+ # 2. AI Text Detection (Authenticity)
99
+ if self.detector_pipeline and callable(self.detector_pipeline):
100
+ try:
101
+ detector_result = self.detector_pipeline(text)[0]
102
+ auth_label_raw = detector_result['label']
103
+ auth_score = round(detector_result['score'] * 100, 2)
104
+ if auth_label_raw.lower() in ['chatgpt', 'ai', 'generated', 'label_1', 'fake']:
105
+ auth_label_display = "Likely AI-Generated"
106
+ elif auth_label_raw.lower() in ['human', 'real', 'label_0']:
107
+ auth_label_display = "Likely Human-Written"
108
+ else:
109
+ auth_label_display = f"Label: {auth_label_raw}" # Fallback
110
+ results['authenticity_label'] = auth_label_display
111
+ results['authenticity_score'] = auth_score # Keep score as model's confidence in the label
112
+
113
+ except Exception as e:
114
+ print(f"AI Text Detection Error: {e}")
115
+ results['authenticity_label'] = "Error"
116
+ results['authenticity_score'] = 0
117
+ results['error'] = results.get('error', '') + f" Authenticity Error: {e};"
118
+ else:
119
+ results['authenticity_label'] = "Model N/A"
120
+ results['authenticity_score'] = 0
121
+ return results
122
+
123
+
124
+ # --- Define the Main Multi-Detection System ---
125
+ class MultiDetectionSystem:
126
+ """
127
+ Encapsulates models for fake news, AI image, and review analysis.
128
+ Handles loading, preprocessing, and inference for HF Spaces.
129
+ """
130
+ def __init__(self, auth_token=None):
131
+ print("\nLoading MultiDetectionSystem models. This may take a few minutes...")
132
+ self.auth_token = auth_token
133
+ self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
134
+ # Pipeline device uses -1 for CPU, >=0 for GPU index
135
+ self.device_pipeline_arg = 0 if torch.cuda.is_available() else -1
136
+ print(f"Using device (torch models): {self.device}")
137
+ print(f"Using device (pipelines): {self.device_pipeline_arg}")
138
+
139
+
140
+ # --- Fake News Detection ---
141
+ self.fake_news_model_name = "MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli"
142
+ self.fake_news_tokenizer = None
143
+ self.fake_news_model = None
144
+ try:
145
+ print(f" -> Loading fake news tokenizer: {self.fake_news_model_name}")
146
+ self.fake_news_tokenizer = AutoTokenizer.from_pretrained(
147
+ self.fake_news_model_name,
148
+ token=self.auth_token # Pass token if available
149
+ )
150
+ print(f" -> Loading fake news model: {self.fake_news_model_name}")
151
+ self.fake_news_model = AutoModelForSequenceClassification.from_pretrained(
152
+ self.fake_news_model_name,
153
+ token=self.auth_token # Pass token if available
154
+ ).to(self.device)
155
+ self.fake_news_model.eval()
156
+ print(" -> Fake news model loaded.")
157
+ except Exception as e:
158
+ print(f"ERROR loading fake news model '{self.fake_news_model_name}': {e}")
159
+ self.fake_news_tokenizer = None
160
+ self.fake_news_model = None
161
+ # --- End of Fake News Section ---
162
+
163
+ # --- AI Image Detection (Custom PKL Model) ---
164
+ # IMPORTANT: Place 'finetune_vit_model.pkl' in the root of your HF Space repo
165
+ # Or change this path if you place it in a subdirectory (e.g., "models/finetune_vit_model.pkl")
166
+ self.image_model_path = "finetune_vit_model.pkl" # <<<--- ADJUSTED PATH
167
+
168
+ # IMPORTANT: Ensure this matches the BASE model you fine-tuned
169
+ self.image_feature_extractor_name = "google/vit-base-patch16-224-in21k" # <<<--- VERIFY THIS NAME
170
+
171
+ self.image_classifier = None
172
+ self.image_feature_extractor = None
173
+ try:
174
+ # 1. Load the Feature Extractor
175
+ print(f" -> Loading image feature extractor: {self.image_feature_extractor_name}")
176
+ self.image_feature_extractor = AutoFeatureExtractor.from_pretrained(
177
+ self.image_feature_extractor_name,
178
+ token=self.auth_token # Pass token if available
179
+ )
180
+ print(" -> Image feature extractor loaded.")
181
+
182
+ # 2. Load CUSTOM Model from PKL (relative path)
183
+ print(f" -> Loading CUSTOM AI image model from PKL: {self.image_model_path}")
184
+ if not os.path.exists(self.image_model_path):
185
+ # Provide more specific error for Spaces deployment
186
+ raise FileNotFoundError(
187
+ f"PKL file not found at '{self.image_model_path}'. "
188
+ f"Make sure '{os.path.basename(self.image_model_path)}' is uploaded to the root of this Space repository "
189
+ f"and Git LFS is tracking it if it's large."
190
+ )
191
+
192
+ with open(self.image_model_path, 'rb') as f:
193
+ # Load assuming the necessary classes are defined or imported
194
+ self.image_classifier = pickle.load(f)
195
+ print(" -> Custom AI image model loaded successfully from PKL.")
196
+
197
+ if not isinstance(self.image_classifier, torch.nn.Module):
198
+ print(f"Warning: Loaded object from PKL is type {type(self.image_classifier)}, not torch.nn.Module.")
199
+
200
+ # 3. Prepare the model
201
+ self.image_classifier = self.image_classifier.to(self.device)
202
+ self.image_classifier.eval()
203
+ print(f" -> Custom AI image model moved to {self.device} and set to eval mode.")
204
+
205
+ except FileNotFoundError as e:
206
+ print(f"FATAL ERROR: {e}. AI Image detection will not work.")
207
+ self.image_classifier = None
208
+ self.image_feature_extractor = None
209
+ except (pickle.UnpicklingError, ImportError) as e:
210
+ print(f"FATAL ERROR unpickling model from '{self.image_model_path}': {e}")
211
+ print("Ensure the environment has all necessary libraries and class definitions required by the PKL file.")
212
+ traceback.print_exc()
213
+ self.image_classifier = None
214
+ self.image_feature_extractor = None
215
+ except Exception as e:
216
+ print(f"ERROR loading image feature extractor or custom model: {e}")
217
+ traceback.print_exc()
218
+ self.image_classifier = None
219
+ self.image_feature_extractor = None
220
+ # --- End of AI Image Detection Section ---
221
+
222
+ # --- Review Analysis (using CombinedAnalyzer) ---
223
+ # Pass the pipeline device argument and token
224
+ self.review_analyzer = CombinedAnalyzer(auth_token=self.auth_token)
225
+ # Override device for CombinedAnalyzer pipelines if needed (optional)
226
+ # self.review_analyzer.device = self.device_pipeline_arg
227
+ # self.review_analyzer.sentiment_pipeline.device = torch.device(f'cuda:{self.device_pipeline_arg}') if self.device_pipeline_arg >= 0 else torch.device('cpu')
228
+ # self.review_analyzer.detector_pipeline.device = torch.device(f'cuda:{self.device_pipeline_arg}') if self.device_pipeline_arg >= 0 else torch.device('cpu')
229
+
230
+
231
+ print("\nMultiDetectionSystem initialization complete!")
232
+
233
+ # --- detect_fake_news method ---
234
+ # (Keep this method exactly as you provided it)
235
+ def detect_fake_news(self, text):
236
+ """Detects likelihood of text being fake news."""
237
+ if not self.fake_news_tokenizer or not self.fake_news_model:
238
+ return {"real": 0, "fake": 0, "conclusion": "Fake News Model N/A"}
239
+ if not text or not isinstance(text, str) or not text.strip():
240
+ return {"real": 0, "fake": 0, "conclusion": "Please provide text"}
241
+ try:
242
+ inputs = self.fake_news_tokenizer(text, truncation=True, return_tensors="pt", max_length=512).to(self.device)
243
+ with torch.no_grad():
244
+ outputs = self.fake_news_model(**inputs)
245
+ scores = torch.softmax(outputs.logits.cpu(), dim=1)[0].tolist()
246
+
247
+ # NLI model mapping: 0: contradiction (Fake), 1: neutral, 2: entailment (Real)
248
+ fake_score = scores[0]
249
+ real_score = scores[2]
250
+
251
+ total_relevant_score = fake_score + real_score
252
+ if total_relevant_score > 1e-6:
253
+ display_real = (real_score / total_relevant_score) * 100
254
+ display_fake = (fake_score / total_relevant_score) * 100
255
+ else:
256
+ display_real, display_fake = 0, 0
257
+
258
+ if display_fake > display_real: conclusion = "Likely FAKE news"
259
+ elif display_real > display_fake: conclusion = "Likely REAL news"
260
+ else: conclusion = "UNCERTAIN (Scores are equal or very low)"
261
+
262
+ return {"real": round(display_real, 2), "fake": round(display_fake, 2), "conclusion": conclusion}
263
+ except Exception as e:
264
+ print(f"Error during fake news detection: {e}")
265
+ traceback.print_exc()
266
+ return {"real": 0, "fake": 0, "conclusion": "Detection Error"}
267
+
268
+ # --- detect_ai_image method ---
269
+ # (Keep this method exactly as you provided it, ensuring Label Mapping is correct)
270
+ def detect_ai_image(self, image):
271
+ """Detects likelihood of an image being AI-generated using the custom model."""
272
+ if not self.image_classifier or not self.image_feature_extractor:
273
+ return {"human-generated": 0, "ai-generated": 0, "conclusion": "Image Model/Extractor N/A"}
274
+ if image is None:
275
+ return {"human-generated": 0, "ai-generated": 0, "conclusion": "Please provide an image"}
276
+
277
+ try:
278
+ if not isinstance(image, Image.Image):
279
+ try: image = Image.fromarray(np.uint8(image)).convert('RGB')
280
+ except Exception as e:
281
+ print(f"Image conversion error: Input type was {type(image)}. Error: {e}")
282
+ return {"human-generated": 0, "ai-generated": 0, "conclusion": "Invalid image format"}
283
+ if image.mode != 'RGB': image = image.convert('RGB')
284
+
285
+ inputs = self.image_feature_extractor(images=image, return_tensors="pt")
286
+ pixel_values = inputs['pixel_values'].to(self.device)
287
+
288
+ with torch.no_grad():
289
+ outputs = self.image_classifier(pixel_values=pixel_values)
290
+ if not hasattr(outputs, 'logits'):
291
+ # Check if it's a direct tensor output (less common from HF models but possible)
292
+ if isinstance(outputs, torch.Tensor):
293
+ logits = outputs
294
+ else:
295
+ print(f"Error: Model output (type: {type(outputs)}) has no 'logits' and isn't a tensor.")
296
+ return {"human-generated": 0, "ai-generated": 0, "conclusion": "Model Output Error (Format)"}
297
+ else:
298
+ logits = outputs.logits
299
+
300
+
301
+ probabilities = torch.softmax(logits, dim=-1)[0].cpu().tolist()
302
+
303
+ # !!! --- CRITICAL: Verify Label Mapping --- !!!
304
+ # These indices MUST match how your custom model was trained and saved.
305
+ # If your model outputs [prob_human, prob_ai]:
306
+ human_prob_index = 0 # <<<--- ADJUST IF NEEDED
307
+ ai_prob_index = 1 # <<<--- ADJUST IF NEEDED
308
+ # If your model outputs [prob_ai, prob_human]:
309
+ # human_prob_index = 1
310
+ # ai_prob_index = 0
311
+ # !!! --- --- --- --- --- --- --- --- --- --- --- !!!
312
+ print(f"Using label indices -> Human: {human_prob_index}, AI: {ai_prob_index}") # Log the indices being used
313
+
314
+ num_classes = len(probabilities)
315
+ if not (0 <= human_prob_index < num_classes and 0 <= ai_prob_index < num_classes):
316
+ print(f"ERROR: Invalid probability indices ({human_prob_index}, {ai_prob_index}) for {num_classes} output classes.")
317
+ return {"human-generated": 0, "ai-generated": 0, "conclusion": "Model Output Error (Index)"}
318
+ if human_prob_index == ai_prob_index:
319
+ print(f"ERROR: Human and AI probability indices cannot be the same ({human_prob_index}).")
320
+ return {"human-generated": 0, "ai-generated": 0, "conclusion": "Configuration Error (Index)"}
321
+
322
+ human_score = probabilities[human_prob_index]
323
+ ai_score = probabilities[ai_prob_index]
324
+ print(f"Raw probabilities: {probabilities}")
325
+ print(f" -> Human Score (idx {human_prob_index}): {human_score:.4f}, AI Score (idx {ai_prob_index}): {ai_score:.4f}")
326
+
327
+ display_human = round(human_score * 100, 2)
328
+ display_ai = round(ai_score * 100, 2)
329
+
330
+ confidence_threshold = 50.0
331
+ if display_ai > display_human and display_ai >= confidence_threshold: conclusion = "Likely AI-GENERATED image"
332
+ elif display_human > display_ai and display_human >= confidence_threshold: conclusion = "Likely HUMAN-CREATED image"
333
+ else: conclusion = "UNCERTAIN origin"
334
+
335
+ return {"human-generated": display_human, "ai-generated": display_ai, "conclusion": conclusion}
336
+
337
+ except Exception as e:
338
+ print(f"Error during AI image detection: {e}")
339
+ traceback.print_exc()
340
+ return {"human-generated": 0, "ai-generated": 0, "conclusion": "Detection Error"}
341
+
342
+ # --- analyze_review method ---
343
+ # (Keep this method exactly as you provided it)
344
+ def analyze_review(self, review_text):
345
+ """Analyzes a review text using the CombinedAnalyzer."""
346
+ if not self.review_analyzer:
347
+ print("Error: Review Analyzer was not initialized.")
348
+ return {"sentiment_label": "System Error", "sentiment_score": 0, "authenticity_label": "System Error", "authenticity_score": 0, "error": "Review Analyzer N/A"}
349
+ if not review_text or not isinstance(review_text, str) or not review_text.strip():
350
+ return {"sentiment_label": "N/A", "sentiment_score": 0, "authenticity_label": "N/A", "authenticity_score": 0, "error": "Please provide review text"}
351
+ try:
352
+ analysis_result = self.review_analyzer.analyze(review_text)
353
+ return analysis_result
354
+ except Exception as e:
355
+ print(f"Error during review analysis delegation: {e}")
356
+ traceback.print_exc()
357
+ return {"sentiment_label": "Error", "sentiment_score": 0, "authenticity_label": "Error", "authenticity_score": 0, "error": f"Analysis Error"}
358
+
359
+ # --- analyze_all method ---
360
+ # (Keep this method exactly as you provided it)
361
+ def analyze_all(self, news_text, image, review_text):
362
+ """Runs all relevant analyses based on the provided inputs."""
363
+ news_text_to_analyze = news_text if news_text and isinstance(news_text, str) and news_text.strip() else ""
364
+ review_text_to_analyze = review_text if review_text and isinstance(review_text, str) and review_text.strip() else ""
365
+ image_to_analyze = image
366
+
367
+ fake_news_result = self.detect_fake_news(news_text_to_analyze) if news_text_to_analyze else {"real": 0, "fake": 0, "conclusion": "No text provided"}
368
+ ai_image_result = self.detect_ai_image(image_to_analyze) if image_to_analyze is not None else {"human-generated": 0, "ai-generated": 0, "conclusion": "No image provided"}
369
+ review_result = self.analyze_review(review_text_to_analyze) if review_text_to_analyze else {"sentiment_label": "N/A", "sentiment_score": 0, "authenticity_label": "N/A", "authenticity_score": 0, "error": "No text provided"}
370
+
371
+ return {
372
+ "fake_news_analysis": fake_news_result,
373
+ "ai_image_analysis": ai_image_result,
374
+ "review_analysis": review_result
375
+ }
376
+
377
+
378
+ # --- Gradio Interface Creation ---
379
+ # (Keep this function exactly as you provided it, including format_results_html)
380
+ def create_interface(system_instance):
381
+ """Creates the Gradio interface using the loaded MultiDetectionSystem."""
382
+ if system_instance is None:
383
+ with gr.Blocks(theme=gr.themes.Soft()) as interface:
384
+ gr.Markdown("# Error: Multi-Detection System Failed to Initialize")
385
+ gr.Markdown("""
386
+ The application cannot start because the underlying AI models could not be loaded or initialized. Please check the Space logs for specific errors:
387
+ * **PKL File:** Ensure `finetune_vit_model.pkl` is uploaded to the Space repository (root directory by default) and tracked with Git LFS if large.
388
+ * **Feature Extractor:** Verify `image_feature_extractor_name` in the code matches the base model used for fine-tuning the PKL.
389
+ * **Model Names:** Double-check all Hugging Face model names (`fake_news_model_name`, etc.).
390
+ * **HF Token:** Ensure the `HF_TOKEN` secret is set correctly if using private models.
391
+ * **Dependencies:** Check `requirements.txt` and potential conflicts.
392
+ * **Pickle Compatibility:** The PKL file might require specific library versions or class definitions present in the environment.
393
+ """)
394
+ return interface
395
+
396
+ # Helper function to format the analysis results into HTML for display
397
+ def format_results_html(results_dict):
398
+ # (This function remains the same as before)
399
+ if not results_dict:
400
+ return '<p style="color: red;">An unexpected error occurred: No results dictionary received.</p>'
401
+
402
+ html = "<h2>Analysis Results</h2>"
403
+
404
+ # --- Fake News Analysis ---
405
+ news_result = results_dict.get("fake_news_analysis", {"real": 0, "fake": 0, "conclusion": "Analysis Error or N/A"})
406
+ news_real = news_result.get('real', 0)
407
+ news_fake = news_result.get('fake', 0)
408
+ news_conclusion = news_result.get('conclusion', 'N/A')
409
+ if 'FAKE' in news_conclusion.upper(): conclusion_color_news = '#dc3545' # Red
410
+ elif 'REAL' in news_conclusion.upper(): conclusion_color_news = '#28a745' # Green
411
+ else: conclusion_color_news = '#ffc107' # Yellow/Orange
412
+
413
+ html += f"""
414
+ <div style="margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9;">
415
+ <h3>Fake News Detection</h3>
416
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
417
+ <div style="flex-basis: 80px; font-weight: bold; margin-right: 10px;">Real:</div>
418
+ <div style="flex-grow: 1; height: 20px; background-color: #e9ecef; border-radius: 5px; overflow: hidden;">
419
+ <div style="width: {news_real}%; height: 100%; background-color: #28a745; transition: width 0.5s ease-in-out;" title="{news_real}%"></div>
420
+ </div>
421
+ <span style="margin-left: 10px; font-weight: bold; white-space: nowrap;">{news_real}%</span>
422
+ </div>
423
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
424
+ <div style="flex-basis: 80px; font-weight: bold; margin-right: 10px;">Fake:</div>
425
+ <div style="flex-grow: 1; height: 20px; background-color: #e9ecef; border-radius: 5px; overflow: hidden;">
426
+ <div style="width: {news_fake}%; height: 100%; background-color: #dc3545; transition: width 0.5s ease-in-out;" title="{news_fake}%"></div>
427
+ </div>
428
+ <span style="margin-left: 10px; font-weight: bold; white-space: nowrap;">{news_fake}%</span>
429
+ </div>
430
+ <p style="font-weight: bold; margin-top: 10px; color: {conclusion_color_news};">Conclusion: {news_conclusion}</p>
431
+ </div>"""
432
+
433
+ # --- AI Image Analysis ---
434
+ image_result = results_dict.get("ai_image_analysis", {"human-generated": 0, "ai-generated": 0, "conclusion": "Analysis Error or N/A"})
435
+ img_human = image_result.get('human-generated', 0)
436
+ img_ai = image_result.get('ai-generated', 0)
437
+ img_conclusion = image_result.get('conclusion', 'N/A')
438
+ if 'AI-GENERATED' in img_conclusion.upper(): conclusion_color_img = '#dc3545' # Red
439
+ elif 'HUMAN-CREATED' in img_conclusion.upper(): conclusion_color_img = '#28a745' # Green
440
+ else: conclusion_color_img = '#ffc107' # Yellow/Orange
441
+
442
+ html += f"""
443
+ <div style="margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9;">
444
+ <h3>AI Image Detection</h3>
445
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
446
+ <div style="flex-basis: 80px; font-weight: bold; margin-right: 10px;">Human:</div>
447
+ <div style="flex-grow: 1; height: 20px; background-color: #e9ecef; border-radius: 5px; overflow: hidden;">
448
+ <div style="width: {img_human}%; height: 100%; background-color: #28a745; transition: width 0.5s ease-in-out;" title="{img_human}%"></div>
449
+ </div>
450
+ <span style="margin-left: 10px; font-weight: bold; white-space: nowrap;">{img_human}%</span>
451
+ </div>
452
+ <div style="display: flex; align-items: center; margin-bottom: 10px;">
453
+ <div style="flex-basis: 80px; font-weight: bold; margin-right: 10px;">AI:</div>
454
+ <div style="flex-grow: 1; height: 20px; background-color: #e9ecef; border-radius: 5px; overflow: hidden;">
455
+ <div style="width: {img_ai}%; height: 100%; background-color: #dc3545; transition: width 0.5s ease-in-out;" title="{img_ai}%"></div>
456
+ </div>
457
+ <span style="margin-left: 10px; font-weight: bold; white-space: nowrap;">{img_ai}%</span>
458
+ </div>
459
+ <p style="font-weight: bold; margin-top: 10px; color: {conclusion_color_img};">Conclusion: {img_conclusion}</p>
460
+ </div>"""
461
+
462
+ # --- Review Analysis ---
463
+ review_result = results_dict.get("review_analysis", {"sentiment_label": "N/A", "sentiment_score": 0, "authenticity_label": "N/A", "authenticity_score": 0, "error": None})
464
+ sentiment_label = review_result.get('sentiment_label', 'N/A').upper()
465
+ sentiment_score = review_result.get('sentiment_score', 0)
466
+ authenticity_label = review_result.get('authenticity_label', 'N/A').upper()
467
+ authenticity_score = review_result.get('authenticity_score', 0)
468
+ review_error = review_result.get('error')
469
+
470
+ sentiment_color = '#dc3545' if 'NEGATIVE' in sentiment_label else '#28a745' if 'POSITIVE' in sentiment_label else '#6c757d'
471
+ authenticity_color = '#dc3545' if 'AI-GENERATED' in authenticity_label else '#28a745' if 'HUMAN-WRITTEN' in authenticity_label else '#6c757d'
472
+
473
+ sentiment_text = f"{review_result.get('sentiment_label', 'N/A')} ({sentiment_score}%)"
474
+ authenticity_text = f"{review_result.get('authenticity_label', 'N/A')} ({authenticity_score}%)"
475
+
476
+ html += f"""
477
+ <div style="padding: 15px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9;">
478
+ <h3>Review Analysis</h3>
479
+ <div style="margin-bottom: 15px;">
480
+ <h4>Sentiment</h4>
481
+ <p style="font-weight: bold; color: {sentiment_color}; margin-bottom: 5px;">{sentiment_text}</p>
482
+ <div style="height: 10px; background-color: #e9ecef; border-radius: 5px; overflow: hidden;" title="Confidence: {sentiment_score}%">
483
+ <div style="width: {sentiment_score}%; height: 100%; background-color: {sentiment_color}; transition: width 0.5s ease-in-out;"></div>
484
+ </div>
485
+ </div>
486
+ <div>
487
+ <h4>Authenticity (AI Text Detection)</h4>
488
+ <p style="font-weight: bold; color: {authenticity_color}; margin-bottom: 5px;">{authenticity_text}</p>
489
+ <div style="height: 10px; background-color: #e9ecef; border-radius: 5px; overflow: hidden;" title="Confidence: {authenticity_score}%">
490
+ <div style="width: {authenticity_score}%; height: 100%; background-color: {authenticity_color}; transition: width 0.5s ease-in-out;"></div>
491
+ </div>
492
+ </div>"""
493
+ if review_error and review_error not in ["Input text cannot be empty.", "Please provide review text", "No text provided"]:
494
+ html += f'<p style="color: red; margin-top: 10px;">Analysis Note: {review_error}</p>'
495
+ html += "</div>"
496
+
497
+ return html
498
+
499
+ # --- Define the Gradio Interface Layout ---
500
+ # (This part remains largely the same, maybe update the description slightly)
501
+ with gr.Blocks(title="Multi-Detection System", theme=gr.themes.Soft()) as interface:
502
+ gr.Markdown(f"""# Multi-Detection Analysis System
503
+ Combines AI models to analyze text and images for authenticity and sentiment.
504
+ * **Fake News Detection:** Analyzes text using `{system_instance.fake_news_model_name if system_instance else 'DeBERTa NLI'}`.
505
+ * **AI Image Detection:** Checks if an image was likely AI-generated (using custom fine-tuned ViT model from `{system_instance.image_model_path if system_instance else 'PKL file'}`). Base Feature Extractor: `{system_instance.image_feature_extractor_name if system_instance else 'ViT Base'}`.
506
+ * **Review Analysis:** Assesses sentiment (`{system_instance.review_analyzer.sentiment_model_name if system_instance else 'DistilBERT SST-2'}`) and authenticity (`{system_instance.review_analyzer.detector_model_name if system_instance else 'RoBERTa Detector'}`).
507
+ """)
508
+
509
+ with gr.Tabs():
510
+ # --- Tab 1: All-in-One ---
511
+ with gr.TabItem("All-in-One Analysis"):
512
+ with gr.Row():
513
+ with gr.Column(scale=1):
514
+ news_input = gr.Textbox(label="News Text Input", lines=5, placeholder="Enter news article text here...")
515
+ image_input = gr.Image(label="Image Input", type="pil", sources=["upload", "clipboard"])
516
+ review_input = gr.Textbox(label="Review Text Input", lines=5, placeholder="Enter product/service review here...")
517
+ analyze_btn = gr.Button("Analyze All Inputs", variant="primary")
518
+ with gr.Column(scale=2):
519
+ results_html = gr.HTML(label="Analysis Results")
520
+
521
+ # --- Tab 2: Fake News Only ---
522
+ with gr.TabItem("Fake News Detection Only"):
523
+ with gr.Row():
524
+ with gr.Column(scale=1):
525
+ news_only_input = gr.Textbox(label="News Text", lines=10, placeholder="Enter news text...")
526
+ news_only_btn = gr.Button("Detect Fake News", variant="primary")
527
+ with gr.Column(scale=2):
528
+ news_only_html = gr.HTML(label="Fake News Analysis Results")
529
+
530
+ # --- Tab 3: AI Image Only ---
531
+ with gr.TabItem("AI Image Detection Only"):
532
+ with gr.Row():
533
+ with gr.Column(scale=1):
534
+ image_only_input = gr.Image(label="Image", type="pil", sources=["upload", "clipboard"])
535
+ image_only_btn = gr.Button("Detect AI Image", variant="primary")
536
+ with gr.Column(scale=2):
537
+ image_only_html = gr.HTML(label="AI Image Analysis Results")
538
+
539
+ # --- Tab 4: Review Analysis Only ---
540
+ with gr.TabItem("Review Analysis Only"):
541
+ with gr.Row():
542
+ with gr.Column(scale=1):
543
+ review_only_input = gr.Textbox(label="Review Text", lines=10, placeholder="Enter review text...")
544
+ review_only_btn = gr.Button("Analyze Review", variant="primary")
545
+ with gr.Column(scale=2):
546
+ review_only_html = gr.HTML(label="Review Analysis Results")
547
+
548
+ # --- Define Click Event Handlers ---
549
+ # (These remain the same)
550
+ analyze_btn.click(
551
+ fn=lambda text, img, rev: format_results_html(system_instance.analyze_all(text, img, rev)),
552
+ inputs=[news_input, image_input, review_input],
553
+ outputs=results_html,
554
+ api_name="analyze_all"
555
+ )
556
+
557
+ def create_dummy_results(key_to_keep, actual_result):
558
+ base = {
559
+ "fake_news_analysis": {"real": 0, "fake": 0, "conclusion": "Not Analyzed"},
560
+ "ai_image_analysis": {"human-generated": 0, "ai-generated": 0, "conclusion": "Not Analyzed"},
561
+ "review_analysis": {"sentiment_label": "N/A", "sentiment_score": 0, "authenticity_label": "N/A", "authenticity_score": 0, "error": "Not Analyzed"}
562
+ }
563
+ if key_to_keep in base:
564
+ base[key_to_keep] = actual_result
565
+ return base
566
+
567
+ news_only_btn.click(
568
+ fn=lambda text: format_results_html(create_dummy_results("fake_news_analysis", system_instance.detect_fake_news(text))),
569
+ inputs=news_only_input,
570
+ outputs=news_only_html,
571
+ api_name="detect_fake_news"
572
+ )
573
+ image_only_btn.click(
574
+ fn=lambda img: format_results_html(create_dummy_results("ai_image_analysis", system_instance.detect_ai_image(img))),
575
+ inputs=image_only_input,
576
+ outputs=image_only_html,
577
+ api_name="detect_ai_image"
578
+ )
579
+ review_only_btn.click(
580
+ fn=lambda rev: format_results_html(create_dummy_results("review_analysis", system_instance.analyze_review(rev))),
581
+ inputs=review_only_input,
582
+ outputs=review_only_html,
583
+ api_name="analyze_review"
584
+ )
585
+
586
+ # --- Add Examples ---
587
+ # (Keep examples as they are)
588
+ gr.Examples(
589
+ examples=[
590
+ ["Scientists discover water plumes on Jupiter's moon Europa, suggesting potential for life.", None, "The hotel room was clean and the bed was comfortable, but the breakfast was overpriced and disappointing."],
591
+ ["BREAKING NEWS: Celebrity Couple Announces Shocking Split After 10 Years of Marriage!", None, None],
592
+ [None, None, "This app constantly crashes and the customer support is useless. Worst purchase ever. Avoid at all costs!!"],
593
+ ["Local bakery wins national award for its innovative sourdough bread recipe. The owner credits her grandmother's secret technique.", None, "Amazing product! It does exactly what it promises and the quality is top-notch. Highly recommended for everyone!"],
594
+ ["Study shows chocolate consumption linked to higher intelligence. Researchers urge public to eat more dark chocolate daily.", None, "It was okay. Nothing special, but not terrible either. Just average."],
595
+ ["URGENT: Government confirms aliens landed in Nevada! Stock up on supplies NOW!", None, "Absolutely revolutionary! This product changed my life overnight. The sleek design and intuitive interface are unparalleled. Five stars!"],
596
+ ],
597
+ inputs=[news_input, image_input, review_input],
598
+ outputs=results_html,
599
+ fn=lambda text, img, rev: format_results_html(system_instance.analyze_all(text, img, rev)),
600
+ label="Example Scenarios (Click to Load into All-in-One Tab)"
601
+ )
602
+
603
+ return interface
604
+
605
+
606
+ # --- Main Execution Block (Modified for Direct Initialization) ---
607
+ if __name__ == "__main__":
608
+ print("-" * 30)
609
+ print("Initializing MultiDetectionSystem for Hugging Face Spaces.")
610
+ print("Loading models from Hugging Face Hub and local PKL file...")
611
+ # System Pickling logic removed
612
+
613
+ detection_system = None # Initialize to None
614
+ try:
615
+ # Directly initialize the system, passing the HF token from secrets if available
616
+ detection_system = MultiDetectionSystem(auth_token=HF_TOKEN)
617
+
618
+ # Basic check after initialization (optional but good practice)
619
+ if not detection_system.fake_news_model:
620
+ print("Warning: Fake news model failed to load.")
621
+ if not detection_system.image_classifier or not detection_system.image_feature_extractor:
622
+ print("Warning: Custom image model/extractor failed to load. Check PKL path and base model name.")
623
+ if not detection_system.review_analyzer or not detection_system.review_analyzer.sentiment_pipeline or not detection_system.review_analyzer.detector_pipeline:
624
+ print("Warning: One or more review analysis pipelines failed to load.")
625
+
626
+ except Exception as e:
627
+ print(f"\nCRITICAL ERROR during MultiDetectionSystem initialization: {e}")
628
+ print("The application might not function correctly.")
629
+ traceback.print_exc()
630
+ # detection_system remains None
631
+
632
+ # --- Create and Launch Gradio Interface ---
633
+ print("\nCreating Gradio interface...")
634
+ # Create interface even if system failed, it will show an error message.
635
+ app_interface = create_interface(detection_system)
636
+
637
+ print("Launching Gradio interface...")
638
+ # Use launch() without share=True for Spaces deployment
639
+ # debug=True is helpful for seeing errors in the logs
640
+ app_interface.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ transformers
4
+ numpy
5
+ Pillow
6
+ requests
7
+ huggingface_hub
8
+ torchvision
9
+ sentencepiece # Often needed by tokenizers like DeBERTa
10
+
11
+ # You might need to pin versions if you encounter compatibility issues
12
+ # e.g., transformers==4.35.0
13
+ # torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cu118
14
+ # (Adjust CUDA version in torch URL if using GPU on Spaces)