shanti commited on
Commit
bcf9174
·
verified ·
1 Parent(s): 8ce7422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -20,7 +20,7 @@ imagenet_class_labels = response.json()
20
  resnet50_model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
21
  resnet50_model.eval()
22
 
23
- # Load ResNet18 for AI vs. Human detection (Use custom-trained weights if available)
24
  resnet18_model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
25
  resnet18_model.eval()
26
 
@@ -31,7 +31,7 @@ transform = transforms.Compose([
31
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
32
  ])
33
 
34
- # HTML Template with improved UI and interpretation
35
  HTML_TEMPLATE = """
36
  <!DOCTYPE html>
37
  <html lang="en">
@@ -46,6 +46,7 @@ HTML_TEMPLATE = """
46
  button { background-color: #4CAF50; color: white; border: none; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 16px; }
47
  button:hover { background-color: #45a049; }
48
  .result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
 
49
  ul { text-align: left; }
50
  </style>
51
  </head>
@@ -63,6 +64,13 @@ HTML_TEMPLATE = """
63
  <button type="submit">Upload and Analyze</button>
64
  </form>
65
 
 
 
 
 
 
 
 
66
  <div style="margin-top: 30px;">
67
  <h2>🤖 What is ResNet50?</h2>
68
  <p>ResNet50 is a 50-layer deep convolutional neural network designed for image classification tasks. It can recognize thousands of objects from the ImageNet dataset.</p>
@@ -100,7 +108,7 @@ def home():
100
  def detect():
101
  text = request.form.get("text")
102
  final_label = "REAL" if "trusted" in text.lower() else "FAKE" # Placeholder logic
103
- return render_template_string(HTML_TEMPLATE, ai_prediction=f"News is {final_label}.", classification_results=None)
104
 
105
  @app.route("/detect_image", methods=["POST"])
106
  def detect_image():
@@ -108,9 +116,11 @@ def detect_image():
108
  return "No image uploaded.", 400
109
 
110
  file = request.files["image"]
111
- img_path = os.path.join(upload_folder, file.filename)
 
112
  file.save(img_path)
113
 
 
114
  img = Image.open(img_path).convert("RGB")
115
  img_tensor = transform(img).unsqueeze(0)
116
 
@@ -129,11 +139,15 @@ def detect_image():
129
  {"label": imagenet_class_labels[idx], "score": prob.item()} for idx, prob in zip(top5_indices, top5_probs)
130
  ]
131
 
 
 
132
  return render_template_string(
133
  HTML_TEMPLATE,
134
  ai_prediction=f"{ai_label} (Confidence: {(ai_confidence * 100):.2f}%)",
135
- classification_results=classification_results
 
136
  )
137
 
138
  if __name__ == "__main__":
139
- app.run(host="0.0.0.0", port=7860) # Updated for Hugging Face Spaces (no ngrok required)
 
 
20
  resnet50_model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
21
  resnet50_model.eval()
22
 
23
+ # Load ResNet18 for AI vs. Human detection
24
  resnet18_model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
25
  resnet18_model.eval()
26
 
 
31
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
32
  ])
33
 
34
+ # HTML Template with image upload preview
35
  HTML_TEMPLATE = """
36
  <!DOCTYPE html>
37
  <html lang="en">
 
46
  button { background-color: #4CAF50; color: white; border: none; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 16px; }
47
  button:hover { background-color: #45a049; }
48
  .result { background: #e7f3fe; padding: 15px; border-radius: 10px; margin-top: 20px; }
49
+ img { max-width: 100%; border-radius: 10px; margin-top: 10px; }
50
  ul { text-align: left; }
51
  </style>
52
  </head>
 
64
  <button type="submit">Upload and Analyze</button>
65
  </form>
66
 
67
+ {% if uploaded_image_url %}
68
+ <div class="result">
69
+ <h2>🖼️ Uploaded Image:</h2>
70
+ <img src="{{ uploaded_image_url }}" alt="Uploaded Image">
71
+ </div>
72
+ {% endif %}
73
+
74
  <div style="margin-top: 30px;">
75
  <h2>🤖 What is ResNet50?</h2>
76
  <p>ResNet50 is a 50-layer deep convolutional neural network designed for image classification tasks. It can recognize thousands of objects from the ImageNet dataset.</p>
 
108
  def detect():
109
  text = request.form.get("text")
110
  final_label = "REAL" if "trusted" in text.lower() else "FAKE" # Placeholder logic
111
+ return render_template_string(HTML_TEMPLATE, ai_prediction=f"News is {final_label}.", classification_results=None, uploaded_image_url=None)
112
 
113
  @app.route("/detect_image", methods=["POST"])
114
  def detect_image():
 
116
  return "No image uploaded.", 400
117
 
118
  file = request.files["image"]
119
+ img_filename = file.filename
120
+ img_path = os.path.join(upload_folder, img_filename)
121
  file.save(img_path)
122
 
123
+ # Process the image
124
  img = Image.open(img_path).convert("RGB")
125
  img_tensor = transform(img).unsqueeze(0)
126
 
 
139
  {"label": imagenet_class_labels[idx], "score": prob.item()} for idx, prob in zip(top5_indices, top5_probs)
140
  ]
141
 
142
+ uploaded_image_url = f"/static/uploads/{img_filename}"
143
+
144
  return render_template_string(
145
  HTML_TEMPLATE,
146
  ai_prediction=f"{ai_label} (Confidence: {(ai_confidence * 100):.2f}%)",
147
+ classification_results=classification_results,
148
+ uploaded_image_url=uploaded_image_url
149
  )
150
 
151
  if __name__ == "__main__":
152
+ app.run(host="0.0.0.0", port=7860) # Suitable for Hugging Face Spaces
153
+