Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +33 -0
- index.html +190 -0
- main.py +155 -0
Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# 1. Install system dependencies
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
libgl1 \
|
| 8 |
+
libglib2.0-0 \
|
| 9 |
+
libsm6 \
|
| 10 |
+
libxext6 \
|
| 11 |
+
libxrender1 \
|
| 12 |
+
libgomp1 \
|
| 13 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 14 |
+
|
| 15 |
+
# 2. Install PyTorch (CPU)
|
| 16 |
+
RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
|
| 17 |
+
|
| 18 |
+
# 3. Install Requirements
|
| 19 |
+
COPY requirements.txt .
|
| 20 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
+
|
| 22 |
+
# 4. Copy App Code
|
| 23 |
+
COPY main.py .
|
| 24 |
+
COPY index.html .
|
| 25 |
+
|
| 26 |
+
# 5. Security Permissions
|
| 27 |
+
RUN useradd -m -u 1000 user && chown -R user:user /app
|
| 28 |
+
USER user
|
| 29 |
+
|
| 30 |
+
# 6. Run Server
|
| 31 |
+
ENV PYTHONUNBUFFERED=1
|
| 32 |
+
EXPOSE 7860
|
| 33 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
index.html
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
| 6 |
+
<title>Eatlytic: Health Scanner</title>
|
| 7 |
+
<link href="[https://fonts.googleapis.com/css2?family=Outfit:wght@400;600;800&display=swap](https://fonts.googleapis.com/css2?family=Outfit:wght@400;600;800&display=swap)" rel="stylesheet">
|
| 8 |
+
<link rel="stylesheet" href="[https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css](https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css)">
|
| 9 |
+
<style>
|
| 10 |
+
/* --- Styles --- */
|
| 11 |
+
* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Outfit', sans-serif; -webkit-tap-highlight-color: transparent; }
|
| 12 |
+
body { background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 50%, #bbf7d0 100%); color: #1f2937; height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
|
| 13 |
+
|
| 14 |
+
.header { padding: 20px; text-align: center; background: rgba(255,255,255,0.85); backdrop-filter: blur(12px); box-shadow: 0 4px 20px rgba(0,0,0,0.03); z-index: 10; }
|
| 15 |
+
.logo { font-size: 1.5rem; font-weight: 800; color: #16a34a; }
|
| 16 |
+
|
| 17 |
+
.main-area { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; gap: 15px; z-index: 5; }
|
| 18 |
+
|
| 19 |
+
.card-btn { width: 100%; max-width: 350px; background: rgba(255,255,255,0.8); padding: 20px; border-radius: 20px; text-align: center; cursor: pointer; border: 2px solid transparent; }
|
| 20 |
+
.card-btn:active { transform: scale(0.98); background: white; }
|
| 21 |
+
|
| 22 |
+
/* ANALYZE BUTTON */
|
| 23 |
+
#analyze-btn {
|
| 24 |
+
display: none; /* Hidden until image selected */
|
| 25 |
+
width: 100%; max-width: 350px; padding: 20px;
|
| 26 |
+
background: #16a34a; color: white; border: none; border-radius: 20px;
|
| 27 |
+
font-size: 1.2rem; font-weight: 800; cursor: pointer;
|
| 28 |
+
box-shadow: 0 10px 20px rgba(22, 163, 74, 0.3);
|
| 29 |
+
animation: popIn 0.3s ease;
|
| 30 |
+
}
|
| 31 |
+
@keyframes popIn { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
|
| 32 |
+
|
| 33 |
+
/* Preview Image */
|
| 34 |
+
#preview-container { display: none; margin-bottom: 20px; }
|
| 35 |
+
#preview-img { width: 150px; height: 150px; object-fit: cover; border-radius: 20px; border: 4px solid white; box-shadow: 0 10px 20px rgba(0,0,0,0.1); }
|
| 36 |
+
|
| 37 |
+
/* Camera & Results */
|
| 38 |
+
#camera-ui, #result-sheet, #loader { position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: none; }
|
| 39 |
+
#camera-ui { background: black; z-index: 100; flex-direction: column; }
|
| 40 |
+
#camera-ui.active { display: flex; }
|
| 41 |
+
video { width: 100%; flex: 1; object-fit: cover; }
|
| 42 |
+
|
| 43 |
+
#result-sheet { background: #f8fcf9; z-index: 200; transform: translateY(100%); transition: 0.3s; padding: 25px; flex-direction: column; overflow-y: auto; }
|
| 44 |
+
#result-sheet.show { transform: translateY(0); display: flex; }
|
| 45 |
+
|
| 46 |
+
/* Result Styles */
|
| 47 |
+
.score-card { background: white; padding: 30px; border-radius: 25px; text-align: center; margin-bottom: 25px; }
|
| 48 |
+
.score-val { font-size: 3.5rem; font-weight: 800; }
|
| 49 |
+
.info-card { background: white; border-radius: 16px; padding: 20px; margin-bottom: 15px; border-left: 5px solid #ddd; }
|
| 50 |
+
.info-card.good { border-left-color: #16a34a; }
|
| 51 |
+
.info-card.bad { border-left-color: #ef4444; }
|
| 52 |
+
|
| 53 |
+
#loader { background: rgba(255,255,255,0.9); z-index: 300; justify-content: center; align-items: center; flex-direction: column; }
|
| 54 |
+
.pulse { width: 60px; height: 60px; background: #16a34a; border-radius: 50%; animation: pulse 1.5s infinite; }
|
| 55 |
+
@keyframes pulse { 0% { transform: scale(0.8); opacity: 0.5; } 100% { transform: scale(1.2); opacity: 0; } }
|
| 56 |
+
</style>
|
| 57 |
+
</head>
|
| 58 |
+
<body>
|
| 59 |
+
|
| 60 |
+
<div class="header">
|
| 61 |
+
<div class="logo">Eatlytic</div>
|
| 62 |
+
<p style="color:#666">Gym • Study • Skin</p>
|
| 63 |
+
</div>
|
| 64 |
+
|
| 65 |
+
<div class="main-area">
|
| 66 |
+
<div id="preview-container">
|
| 67 |
+
<img id="preview-img" src="">
|
| 68 |
+
</div>
|
| 69 |
+
|
| 70 |
+
<div class="card-btn" onclick="startCamera()">📸 Open Camera</div>
|
| 71 |
+
<div class="card-btn" onclick="document.getElementById('fileInput').click()">🖼️ Upload Image</div>
|
| 72 |
+
|
| 73 |
+
<button id="analyze-btn" onclick="analyzeNow()">🚀 Analyze Now</button>
|
| 74 |
+
|
| 75 |
+
<input type="file" id="fileInput" hidden accept="image/*" onchange="handleFile(this.files[0])">
|
| 76 |
+
</div>
|
| 77 |
+
|
| 78 |
+
<div id="camera-ui">
|
| 79 |
+
<video id="video" autoplay playsinline></video>
|
| 80 |
+
<div style="height:120px; background:black; display:flex; justify-content:center; align-items:center; position:relative;">
|
| 81 |
+
<button onclick="stopCamera()" style="position:absolute; left:30px; color:white; background:none; border:none; font-size:1rem;">Cancel</button>
|
| 82 |
+
<div onclick="capture()" style="width:70px; height:70px; background:white; border-radius:50%; border:4px solid #555;"></div>
|
| 83 |
+
</div>
|
| 84 |
+
<canvas id="canvas" hidden></canvas>
|
| 85 |
+
</div>
|
| 86 |
+
|
| 87 |
+
<div id="loader">
|
| 88 |
+
<div class="pulse"></div>
|
| 89 |
+
<p style="margin-top:20px; font-weight:700; color:#16a34a">AI is Thinking...</p>
|
| 90 |
+
</div>
|
| 91 |
+
|
| 92 |
+
<div id="result-sheet">
|
| 93 |
+
<div id="result-content"></div>
|
| 94 |
+
<button onclick="closeResults()" style="margin-top:auto; padding:18px; background:#1f2937; color:white; border:none; border-radius:15px; font-weight:700;">Check Another</button>
|
| 95 |
+
</div>
|
| 96 |
+
|
| 97 |
+
<script>
|
| 98 |
+
// ⚠️ CRITICAL: Replace this with your own URL if needed.
|
| 99 |
+
// If you are using Hugging Face, make sure this matches exactly.
|
| 100 |
+
const API_URL = "[https://shaikhsarib-e.hf.space](https://shaikhsarib-e.hf.space)";
|
| 101 |
+
let selectedFile = null;
|
| 102 |
+
|
| 103 |
+
// --- File Handling ---
|
| 104 |
+
function handleFile(file) {
|
| 105 |
+
if (!file) return;
|
| 106 |
+
selectedFile = file;
|
| 107 |
+
|
| 108 |
+
// Show Preview
|
| 109 |
+
const reader = new FileReader();
|
| 110 |
+
reader.onload = (e) => {
|
| 111 |
+
document.getElementById('preview-img').src = e.target.result;
|
| 112 |
+
document.getElementById('preview-container').style.display = 'block';
|
| 113 |
+
document.getElementById('analyze-btn').style.display = 'block'; // Show Analyze Button
|
| 114 |
+
};
|
| 115 |
+
reader.readAsDataURL(file);
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// --- Camera Logic ---
|
| 119 |
+
let stream;
|
| 120 |
+
async function startCamera() {
|
| 121 |
+
try {
|
| 122 |
+
stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } });
|
| 123 |
+
document.getElementById('video').srcObject = stream;
|
| 124 |
+
document.getElementById('camera-ui').classList.add('active');
|
| 125 |
+
} catch (e) { alert("Camera Error"); }
|
| 126 |
+
}
|
| 127 |
+
function stopCamera() {
|
| 128 |
+
if(stream) stream.getTracks().forEach(t => t.stop());
|
| 129 |
+
document.getElementById('camera-ui').classList.remove('active');
|
| 130 |
+
}
|
| 131 |
+
function capture() {
|
| 132 |
+
const video = document.getElementById('video');
|
| 133 |
+
const canvas = document.getElementById('canvas');
|
| 134 |
+
canvas.width = video.videoWidth;
|
| 135 |
+
canvas.height = video.videoHeight;
|
| 136 |
+
canvas.getContext('2d').drawImage(video, 0, 0);
|
| 137 |
+
canvas.toBlob(blob => { stopCamera(); handleFile(blob); }, 'image/jpeg', 0.8);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// --- Analyze Logic (Triggered by Button) ---
|
| 141 |
+
async function analyzeNow() {
|
| 142 |
+
if (!selectedFile) return;
|
| 143 |
+
document.getElementById('loader').style.display = 'flex';
|
| 144 |
+
|
| 145 |
+
const formData = new FormData();
|
| 146 |
+
formData.append('image', selectedFile);
|
| 147 |
+
formData.append('user_profile', "General Health");
|
| 148 |
+
formData.append('product_name', "");
|
| 149 |
+
|
| 150 |
+
try {
|
| 151 |
+
const res = await fetch(`${API_URL}/analyze`, { method: 'POST', body: formData });
|
| 152 |
+
if (!res.ok) throw new Error("Server Error");
|
| 153 |
+
const data = await res.json();
|
| 154 |
+
showData(data);
|
| 155 |
+
} catch (err) { alert(err.message); }
|
| 156 |
+
finally { document.getElementById('loader').style.display = 'none'; }
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
function showData(data) {
|
| 160 |
+
const score = data.compatibility_score;
|
| 161 |
+
let color = score >= 7 ? '#16a34a' : (score >= 4 ? '#eab308' : '#ef4444');
|
| 162 |
+
|
| 163 |
+
document.getElementById('result-content').innerHTML = `
|
| 164 |
+
<div class="score-card">
|
| 165 |
+
<div class="score-val" style="color:${color}">${score}</div>
|
| 166 |
+
<div style="font-weight:700; color:${color}">${data.recommendation}</div>
|
| 167 |
+
<h3>${data.product_name}</h3>
|
| 168 |
+
<p>${data.overall_summary}</p>
|
| 169 |
+
</div>
|
| 170 |
+
<div class="info-card good">
|
| 171 |
+
<h4 style="color:#16a34a">✅ Good</h4>
|
| 172 |
+
<ul>${data.body_benefits.map(b => `<li>${b}</li>`).join('')}</ul>
|
| 173 |
+
</div>
|
| 174 |
+
<div class="info-card bad">
|
| 175 |
+
<h4 style="color:#ef4444">⚠️ Bad</h4>
|
| 176 |
+
<ul>${data.body_risks.map(r => `<li>${r}</li>`).join('')}</ul>
|
| 177 |
+
</div>
|
| 178 |
+
`;
|
| 179 |
+
document.getElementById('result-sheet').classList.add('show');
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
function closeResults() {
|
| 183 |
+
document.getElementById('result-sheet').classList.remove('show');
|
| 184 |
+
selectedFile = null;
|
| 185 |
+
document.getElementById('preview-container').style.display = 'none';
|
| 186 |
+
document.getElementById('analyze-btn').style.display = 'none';
|
| 187 |
+
}
|
| 188 |
+
</script>
|
| 189 |
+
</body>
|
| 190 |
+
</html>
|
main.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import base64
|
| 5 |
+
import requests
|
| 6 |
+
from typing import Optional
|
| 7 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from fastapi.responses import FileResponse
|
| 10 |
+
from groq import Groq
|
| 11 |
+
|
| 12 |
+
# --- Setup ---
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
app = FastAPI(title="Eatlytic: Universal Scanner")
|
| 17 |
+
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"],
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# --- BRAIN MEMORY ---
|
| 27 |
+
BRAIN_FILE = "brain.json"
|
| 28 |
+
|
| 29 |
+
def load_brain():
|
| 30 |
+
if os.path.exists(BRAIN_FILE):
|
| 31 |
+
try:
|
| 32 |
+
with open(BRAIN_FILE, "r") as f: return json.load(f)
|
| 33 |
+
except: return {}
|
| 34 |
+
return {}
|
| 35 |
+
|
| 36 |
+
def save_to_brain(name: str, data: dict):
|
| 37 |
+
if not name or name == "Unknown": return
|
| 38 |
+
brain = load_brain()
|
| 39 |
+
brain[name.lower().strip()] = data
|
| 40 |
+
with open(BRAIN_FILE, "w") as f: json.dump(brain, f)
|
| 41 |
+
|
| 42 |
+
# --- KEYS ---
|
| 43 |
+
GROQ_API_KEY = os.environ.get("GROQ_KEY")
|
| 44 |
+
SERPER_API_KEY = os.environ.get("SERPER_KEY")
|
| 45 |
+
client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
|
| 46 |
+
|
| 47 |
+
# --- TOOLS ---
|
| 48 |
+
def search_google(query: str) -> str:
|
| 49 |
+
if not SERPER_API_KEY: return "No Search Key found."
|
| 50 |
+
url = "https://google.serper.dev/search"
|
| 51 |
+
payload = json.dumps({"q": query})
|
| 52 |
+
headers = {'X-API-KEY': SERPER_API_KEY, 'Content-Type': 'application/json'}
|
| 53 |
+
try:
|
| 54 |
+
response = requests.post(url, headers=headers, data=payload)
|
| 55 |
+
data = response.json()
|
| 56 |
+
results = []
|
| 57 |
+
if "organic" in data:
|
| 58 |
+
for item in data["organic"][:3]:
|
| 59 |
+
results.append(f"- {item.get('title')}: {item.get('snippet')}")
|
| 60 |
+
return "\n".join(results)
|
| 61 |
+
except:
|
| 62 |
+
return "Search failed."
|
| 63 |
+
|
| 64 |
+
def encode_image(image_content):
|
| 65 |
+
return base64.b64encode(image_content).decode('utf-8')
|
| 66 |
+
|
| 67 |
+
def clean_json(text: str) -> str:
|
| 68 |
+
text = text.strip()
|
| 69 |
+
if text.startswith("```json"): text = text[7:]
|
| 70 |
+
elif text.startswith("```"): text = text[3:]
|
| 71 |
+
if text.endswith("```"): text = text[:-3]
|
| 72 |
+
return text.strip()
|
| 73 |
+
|
| 74 |
+
# --- ROUTES ---
|
| 75 |
+
@app.get("/")
|
| 76 |
+
async def read_index():
|
| 77 |
+
return FileResponse('index.html')
|
| 78 |
+
|
| 79 |
+
@app.post("/analyze")
|
| 80 |
+
async def analyze_multimodal(
|
| 81 |
+
user_profile: str = Form(...),
|
| 82 |
+
product_name: Optional[str] = Form(None),
|
| 83 |
+
image: Optional[UploadFile] = File(None)
|
| 84 |
+
):
|
| 85 |
+
if not client: raise HTTPException(500, "GROQ_KEY Missing")
|
| 86 |
+
|
| 87 |
+
base64_image = ""
|
| 88 |
+
if image:
|
| 89 |
+
content = await image.read()
|
| 90 |
+
base64_image = encode_image(content)
|
| 91 |
+
|
| 92 |
+
# Check Brain First
|
| 93 |
+
if product_name:
|
| 94 |
+
brain = load_brain()
|
| 95 |
+
if product_name.lower().strip() in brain:
|
| 96 |
+
return brain[product_name.lower().strip()]
|
| 97 |
+
|
| 98 |
+
# Live Google Context
|
| 99 |
+
web_context = search_google(f"health benefits side effects ingredients {product_name or 'food cosmetic product'}")
|
| 100 |
+
|
| 101 |
+
messages = [
|
| 102 |
+
{
|
| 103 |
+
"role": "user",
|
| 104 |
+
"content": [
|
| 105 |
+
{
|
| 106 |
+
"type": "text",
|
| 107 |
+
"text": f"""
|
| 108 |
+
You are Eatlytic, an AI Health Expert.
|
| 109 |
+
USER PROFILE: {user_profile}
|
| 110 |
+
WEB DATA: {web_context}
|
| 111 |
+
|
| 112 |
+
Task: Identify the item and analyze IMPACT on BODY, BRAIN, and SKIN.
|
| 113 |
+
Return valid JSON only:
|
| 114 |
+
{{
|
| 115 |
+
"product_name": "Name",
|
| 116 |
+
"compatibility_score": 0-10,
|
| 117 |
+
"recommendation": "Verdict",
|
| 118 |
+
"overall_summary": "Summary",
|
| 119 |
+
"body_benefits": ["Pro 1", "Pro 2"],
|
| 120 |
+
"body_risks": ["Con 1", "Con 2"]
|
| 121 |
+
}}
|
| 122 |
+
"""
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"type": "image_url",
|
| 126 |
+
"image_url": { "url": f"data:image/jpeg;base64,{base64_image}" }
|
| 127 |
+
}
|
| 128 |
+
]
|
| 129 |
+
}
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
try:
|
| 133 |
+
# Using Llama 3.2 11B Vision (Stable & Free)
|
| 134 |
+
completion = client.chat.completions.create(
|
| 135 |
+
model="llama-3.2-11b-vision-preview",
|
| 136 |
+
messages=messages,
|
| 137 |
+
temperature=0.1,
|
| 138 |
+
max_tokens=1024,
|
| 139 |
+
response_format={"type": "json_object"}
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
result = json.loads(clean_json(completion.choices[0].message.content))
|
| 143 |
+
|
| 144 |
+
if result.get("product_name") and result["product_name"] != "Unknown":
|
| 145 |
+
save_to_brain(result["product_name"], result)
|
| 146 |
+
|
| 147 |
+
return result
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
logger.error(f"Vision AI Error: {e}")
|
| 151 |
+
raise HTTPException(500, f"AI Failed: {str(e)}")
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
import uvicorn
|
| 155 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|