Spaces:
Sleeping
Sleeping
Initial deploy CalChecker
Browse files- Dockerfile +6 -0
- app.py +82 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
WORKDIR /code
|
| 3 |
+
COPY requirements.txt .
|
| 4 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 5 |
+
COPY . .
|
| 6 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
from threading import Thread
|
| 5 |
+
from flask import Flask
|
| 6 |
+
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, WebAppInfo
|
| 7 |
+
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
from supabase import create_client, Client
|
| 10 |
+
|
| 11 |
+
# --- SERVER FLASK (PENTING: Port 7860 untuk Hugging Face) ---
|
| 12 |
+
server = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
@server.route('/')
|
| 15 |
+
def health_check():
|
| 16 |
+
return "Bot status: ACTIVE", 200
|
| 17 |
+
|
| 18 |
+
def run_web_server():
|
| 19 |
+
# Hugging Face secara default membaca port 7860
|
| 20 |
+
server.run(host='0.0.0.0', port=7860)
|
| 21 |
+
|
| 22 |
+
# --- KONFIGURASI SUPABASE & GEMINI ---
|
| 23 |
+
# Variabel diambil dari Secrets di Hugging Face Settings
|
| 24 |
+
supabase: Client = create_client(
|
| 25 |
+
os.getenv("SUPABASE_URL"),
|
| 26 |
+
os.getenv("SUPABASE_SERVICE_ROLE_KEY")
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 30 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 31 |
+
|
| 32 |
+
async def analyze_food(image_bytes):
|
| 33 |
+
prompt = """Analyze this food image. Return ONLY a raw JSON object:
|
| 34 |
+
{"food_name": "name", "calories": 100, "protein": 5.0, "carbs": 10.0, "fat": 2.0}
|
| 35 |
+
Do not include markdown tags like ```json."""
|
| 36 |
+
|
| 37 |
+
response = model.generate_content([{"mime_type": "image/jpeg", "data": image_bytes}, prompt])
|
| 38 |
+
clean_text = response.text.strip().replace("```json", "").replace("```", "")
|
| 39 |
+
return json.loads(clean_text)
|
| 40 |
+
|
| 41 |
+
# --- HANDLERS BOT ---
|
| 42 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 43 |
+
# WEBAPP_URL diambil dari Secrets (URL Netlify Anda)
|
| 44 |
+
url = os.getenv("WEBAPP_URL")
|
| 45 |
+
kbd = [[InlineKeyboardButton("📊 Buka Dashboard Kalori", web_app=WebAppInfo(url=url))]]
|
| 46 |
+
await update.message.reply_text(
|
| 47 |
+
f"Halo {update.effective_user.first_name}! 🍎\nKirim foto makanan untuk cek kalori.",
|
| 48 |
+
reply_markup=InlineKeyboardMarkup(kbd)
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
async def handle_photo(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 52 |
+
status = await update.message.reply_text("Sedang menganalisis makanan... ⏳")
|
| 53 |
+
photo = await update.message.photo[-1].get_file()
|
| 54 |
+
img_bytes = await photo.download_as_bytearray()
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
data = await analyze_food(img_bytes)
|
| 58 |
+
supabase.table("calorie_logs").insert({
|
| 59 |
+
"user_id": update.effective_user.id,
|
| 60 |
+
**data
|
| 61 |
+
}).execute()
|
| 62 |
+
|
| 63 |
+
res = (f"✅ *{data['food_name']}*\n🔥 {data['calories']} kcal\n"
|
| 64 |
+
f"P: {data['protein']}g | C: {data['carbs']}g | L: {data['fat']}g")
|
| 65 |
+
await status.edit_text(res, parse_mode="Markdown")
|
| 66 |
+
except Exception as e:
|
| 67 |
+
await status.edit_text("Gagal menganalisis makanan. Pastikan foto makanan terlihat jelas.")
|
| 68 |
+
|
| 69 |
+
# --- RUNNER ---
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
# 1. Jalankan Flask di Thread berbeda agar UptimeRobot bisa nge-ping
|
| 72 |
+
Thread(target=run_web_server).start()
|
| 73 |
+
|
| 74 |
+
# 2. Jalankan Bot
|
| 75 |
+
token = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 76 |
+
app = ApplicationBuilder().token(token).build()
|
| 77 |
+
|
| 78 |
+
app.add_handler(CommandHandler("start", start))
|
| 79 |
+
app.add_handler(MessageHandler(filters.PHOTO, handle_photo))
|
| 80 |
+
|
| 81 |
+
print("Bot is starting...")
|
| 82 |
+
app.run_polling()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-telegram-bot
|
| 2 |
+
google-generativeai
|
| 3 |
+
supabase
|
| 4 |
+
python-dotenv
|
| 5 |
+
flask
|