can you also help me with the backend and connection hugging face open source models for image analysis
Browse files- api.py +105 -0
- dashboard.html +104 -8
api.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from werkzeug.utils import secure_filename
|
| 4 |
+
import os
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# Configure upload folder
|
| 12 |
+
UPLOAD_FOLDER = 'uploads'
|
| 13 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 15 |
+
|
| 16 |
+
# Load Hugging Face model (this would be your actual food/toxin analysis model)
|
| 17 |
+
toxin_classifier = pipeline(
|
| 18 |
+
"image-classification",
|
| 19 |
+
model="facebook/deit-base-patch16-224" # Example model - replace with your actual food/toxin model
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
@app.route('/api/analyze', methods=['POST'])
|
| 23 |
+
def analyze_image():
|
| 24 |
+
if 'image' not in request.files:
|
| 25 |
+
return jsonify({'error': 'No image provided'}), 400
|
| 26 |
+
|
| 27 |
+
file = request.files['image']
|
| 28 |
+
if file.filename == '':
|
| 29 |
+
return jsonify({'error': 'No selected file'}), 400
|
| 30 |
+
|
| 31 |
+
if file:
|
| 32 |
+
filename = secure_filename(file.filename)
|
| 33 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 34 |
+
file.save(filepath)
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
# Process image with Hugging Face model
|
| 38 |
+
image = Image.open(filepath)
|
| 39 |
+
results = toxin_classifier(image)
|
| 40 |
+
|
| 41 |
+
# Process results into your application's format
|
| 42 |
+
# This is placeholder logic - you would customize based on your model's output
|
| 43 |
+
toxin_level = "Medium"
|
| 44 |
+
safety = "Caution"
|
| 45 |
+
recommendation = "Limit consumption to 1 serving per week"
|
| 46 |
+
|
| 47 |
+
# For demo purposes, we'll use the first prediction
|
| 48 |
+
if results and len(results) > 0:
|
| 49 |
+
main_prediction = results[0]
|
| 50 |
+
if main_prediction['score'] > 0.7:
|
| 51 |
+
toxin_level = "High"
|
| 52 |
+
safety = "Avoid"
|
| 53 |
+
recommendation = "Do not consume - high toxin levels detected"
|
| 54 |
+
elif main_prediction['score'] > 0.4:
|
| 55 |
+
toxin_level = "Medium"
|
| 56 |
+
safety = "Caution"
|
| 57 |
+
else:
|
| 58 |
+
toxin_level = "Low"
|
| 59 |
+
safety = "Safe"
|
| 60 |
+
|
| 61 |
+
return jsonify({
|
| 62 |
+
'toxin_level': toxin_level,
|
| 63 |
+
'safety': safety,
|
| 64 |
+
'recommendation': recommendation,
|
| 65 |
+
'image_path': f'/uploads/{filename}'
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return jsonify({'error': str(e)}), 500
|
| 70 |
+
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
app.run(debug=True)
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
Key changes made:
|
| 76 |
+
|
| 77 |
+
1. Added file upload and analysis functionality to the dashboard
|
| 78 |
+
2. Created a modal to display scan results
|
| 79 |
+
3. Added Flask backend API that:
|
| 80 |
+
- Accepts image uploads
|
| 81 |
+
- Processes them with a Hugging Face model
|
| 82 |
+
- Returns analysis results
|
| 83 |
+
4. Included error handling and loading states
|
| 84 |
+
5. Added UI for displaying the scan results
|
| 85 |
+
|
| 86 |
+
To implement this:
|
| 87 |
+
|
| 88 |
+
1. Install required Python packages:
|
| 89 |
+
```bash
|
| 90 |
+
pip install flask torch transformers pillow
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
2. Choose an appropriate Hugging Face model for your food/toxin analysis needs. The example uses a generic image classification model, but you might want:
|
| 94 |
+
- A food-specific model
|
| 95 |
+
- A toxicity detection model
|
| 96 |
+
- Or a custom fine-tuned model
|
| 97 |
+
|
| 98 |
+
3. Run the Flask API:
|
| 99 |
+
```bash
|
| 100 |
+
python api.py
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
4. Update the frontend to point to your API endpoint
|
| 104 |
+
|
| 105 |
+
The backend currently uses a placeholder model - you should replace it with a model that's actually trained for food toxin analysis. Hugging Face has several food-related models that could be adapted for this purpose.
|
dashboard.html
CHANGED
|
@@ -5,8 +5,59 @@
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>ToxiTrack - Dashboard</title>
|
| 7 |
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
|
| 8 |
-
<script src="https://cdn.tailwindcss.com"
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
|
| 11 |
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 12 |
<style>
|
|
@@ -215,10 +266,11 @@
|
|
| 215 |
<div class="bg-white p-6 rounded-xl shadow-sm mb-8">
|
| 216 |
<div class="flex justify-between items-center mb-4">
|
| 217 |
<h3 class="font-semibold text-gray-800">Recent Food Scans</h3>
|
| 218 |
-
<button class="text-sm text-emerald-500 hover:text-emerald-600 flex items-center">
|
| 219 |
<i data-feather="plus" class="w-4 h-4 mr-1"></i> New Scan
|
| 220 |
</button>
|
| 221 |
-
|
|
|
|
| 222 |
<div class="overflow-x-auto">
|
| 223 |
<table class="min-w-full divide-y divide-gray-200">
|
| 224 |
<thead>
|
|
@@ -349,11 +401,55 @@
|
|
| 349 |
</main>
|
| 350 |
</div>
|
| 351 |
</div>
|
| 352 |
-
|
| 353 |
-
<script>
|
| 354 |
feather.replace();
|
| 355 |
-
|
| 356 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
document.addEventListener('DOMContentLoaded', function() {
|
| 358 |
// Main Chart
|
| 359 |
const mainCtx = document.getElementById('mainChart').getContext('2d');
|
|
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>ToxiTrack - Dashboard</title>
|
| 7 |
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
|
| 8 |
+
<script src="https://cdn.tailwindcss.com">
|
| 9 |
+
</script>
|
| 10 |
+
|
| 11 |
+
<!-- Modal for scan results -->
|
| 12 |
+
<div id="scanModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
|
| 13 |
+
<div class="bg-white rounded-lg p-6 max-w-md w-full">
|
| 14 |
+
<div class="flex justify-between items-center mb-4">
|
| 15 |
+
<h3 class="text-lg font-bold text-gray-800">Scan Results</h3>
|
| 16 |
+
<button id="closeModal" class="text-gray-500 hover:text-gray-700">
|
| 17 |
+
<i data-feather="x"></i>
|
| 18 |
+
</button>
|
| 19 |
+
</div>
|
| 20 |
+
<div class="mb-4">
|
| 21 |
+
<img id="scanImage" src="" alt="Scanned Food" class="w-full h-48 object-cover rounded-lg mb-4">
|
| 22 |
+
<div class="grid grid-cols-2 gap-4">
|
| 23 |
+
<div>
|
| 24 |
+
<p class="text-sm text-gray-500">Toxin Level</p>
|
| 25 |
+
<p id="toxinLevel" class="font-bold"></p>
|
| 26 |
+
</div>
|
| 27 |
+
<div>
|
| 28 |
+
<p class="text-sm text-gray-500">Safety</p>
|
| 29 |
+
<p id="safetyLevel" class="font-bold"></p>
|
| 30 |
+
</div>
|
| 31 |
+
</div>
|
| 32 |
+
<div class="mt-4">
|
| 33 |
+
<p class="text-sm text-gray-500">Recommendation</p>
|
| 34 |
+
<p id="recommendation" class="text-sm"></p>
|
| 35 |
+
</div>
|
| 36 |
+
</div>
|
| 37 |
+
<button id="saveScan" class="w-full bg-emerald-500 hover:bg-emerald-600 text-white py-2 rounded-lg">
|
| 38 |
+
Save to History
|
| 39 |
+
</button>
|
| 40 |
+
</div>
|
| 41 |
+
</div>
|
| 42 |
+
|
| 43 |
+
<script>
|
| 44 |
+
// Modal functionality
|
| 45 |
+
const modal = document.getElementById('scanModal');
|
| 46 |
+
const closeModal = document.getElementById('closeModal');
|
| 47 |
+
const saveScan = document.getElementById('saveScan');
|
| 48 |
+
|
| 49 |
+
closeModal.addEventListener('click', () => {
|
| 50 |
+
modal.classList.add('hidden');
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
saveScan.addEventListener('click', () => {
|
| 54 |
+
// Here you would save the scan to your database
|
| 55 |
+
// For now we'll just close the modal and reload
|
| 56 |
+
modal.classList.add('hidden');
|
| 57 |
+
location.reload();
|
| 58 |
+
});
|
| 59 |
+
</script>
|
| 60 |
+
<script src="https://unpkg.com/feather-icons"></script>
|
| 61 |
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
|
| 62 |
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 63 |
<style>
|
|
|
|
| 266 |
<div class="bg-white p-6 rounded-xl shadow-sm mb-8">
|
| 267 |
<div class="flex justify-between items-center mb-4">
|
| 268 |
<h3 class="font-semibold text-gray-800">Recent Food Scans</h3>
|
| 269 |
+
<button id="newScanButton" class="text-sm text-emerald-500 hover:text-emerald-600 flex items-center">
|
| 270 |
<i data-feather="plus" class="w-4 h-4 mr-1"></i> New Scan
|
| 271 |
</button>
|
| 272 |
+
<input type="file" id="fileInput" accept="image/*" class="hidden">
|
| 273 |
+
</div>
|
| 274 |
<div class="overflow-x-auto">
|
| 275 |
<table class="min-w-full divide-y divide-gray-200">
|
| 276 |
<thead>
|
|
|
|
| 401 |
</main>
|
| 402 |
</div>
|
| 403 |
</div>
|
| 404 |
+
<script>
|
|
|
|
| 405 |
feather.replace();
|
| 406 |
+
|
| 407 |
+
// Scan functionality
|
| 408 |
+
document.getElementById('newScanButton').addEventListener('click', () => {
|
| 409 |
+
document.getElementById('fileInput').click();
|
| 410 |
+
});
|
| 411 |
+
|
| 412 |
+
document.getElementById('fileInput').addEventListener('change', async (e) => {
|
| 413 |
+
const file = e.target.files[0];
|
| 414 |
+
if (!file) return;
|
| 415 |
+
|
| 416 |
+
// Show loading state
|
| 417 |
+
const button = document.getElementById('newScanButton');
|
| 418 |
+
button.innerHTML = '<i data-feather="loader" class="w-4 h-4 mr-1 animate-spin"></i> Analyzing...';
|
| 419 |
+
feather.replace();
|
| 420 |
+
|
| 421 |
+
try {
|
| 422 |
+
// Upload image to backend for analysis
|
| 423 |
+
const formData = new FormData();
|
| 424 |
+
formData.append('image', file);
|
| 425 |
+
|
| 426 |
+
const response = await fetch('/api/analyze', {
|
| 427 |
+
method: 'POST',
|
| 428 |
+
body: formData
|
| 429 |
+
});
|
| 430 |
+
|
| 431 |
+
if (!response.ok) throw new Error('Analysis failed');
|
| 432 |
+
|
| 433 |
+
const result = await response.json();
|
| 434 |
+
|
| 435 |
+
// Display results (you would update this with your actual UI)
|
| 436 |
+
alert(`Analysis complete!\n\nToxin Level: ${result.toxin_level}\nSafety: ${result.safety}\nRecommendation: ${result.recommendation}`);
|
| 437 |
+
|
| 438 |
+
// Here you would update the recent scans table with the new result
|
| 439 |
+
// For now we'll just reload the page
|
| 440 |
+
location.reload();
|
| 441 |
+
|
| 442 |
+
} catch (error) {
|
| 443 |
+
console.error('Error:', error);
|
| 444 |
+
alert('Failed to analyze image. Please try again.');
|
| 445 |
+
} finally {
|
| 446 |
+
// Reset button
|
| 447 |
+
button.innerHTML = '<i data-feather="plus" class="w-4 h-4 mr-1"></i> New Scan';
|
| 448 |
+
feather.replace();
|
| 449 |
+
e.target.value = ''; // Clear file input
|
| 450 |
+
}
|
| 451 |
+
});
|
| 452 |
+
// Initialize charts
|
| 453 |
document.addEventListener('DOMContentLoaded', function() {
|
| 454 |
// Main Chart
|
| 455 |
const mainCtx = document.getElementById('mainChart').getContext('2d');
|