Spaces:
Runtime error
Runtime error
| import os | |
| from flask import Flask, request, send_from_directory | |
| from twilio.twiml.messaging_response import MessagingResponse | |
| from dotenv import load_dotenv | |
| import logging | |
| from datetime import datetime | |
| import pytz | |
| # --------------------------------------- | |
| # LOAD ENVIRONMENT VARIABLES | |
| # --------------------------------------- | |
| load_dotenv() | |
| # IMPORTANT: | |
| # Put your Twilio details inside Hugging Face Space β Settings β Secrets | |
| TWILIO_ACCOUNT_SID = os.getenv("ACd80b022c6634665f76332e678569025f") # place Twilio SID here | |
| TWILIO_AUTH_TOKEN = os.getenv("aea11fa641608536cbf71bb4dfb6474a") # place Twilio token here | |
| TWILIO_PHONE_NUMBER = os.getenv("8660712308") # WhatsApp number | |
| # Space public URL (required for sending back static image links) | |
| NGROK_URL = os.getenv("https://guruthetechie-ai_Virtual_tryon.hf.space/ | |
| ", "") # Example: https://your-user.hf.space/ | |
| if NGROK_URL and not NGROK_URL.endswith("/"): | |
| NGROK_URL += "/" | |
| # --------------------------------------- | |
| # FLASK APP | |
| # --------------------------------------- | |
| app = Flask(__name__) | |
| # --------------------------------------- | |
| # LOGGING (Fix for HF Spaces) | |
| # --------------------------------------- | |
| log_format = "%(asctime)s - %(levelname)s - %(message)s" | |
| log_directory = "logs" | |
| os.makedirs(log_directory, exist_ok=True) | |
| log_file = os.path.join(log_directory, "app.log") | |
| logging.basicConfig(filename=log_file, level=logging.INFO, format=log_format) | |
| # --------------------------------------- | |
| # IMPORT LOCAL MODULES | |
| # --------------------------------------- | |
| from python.business_logic import business_logic | |
| from python.utils import ( | |
| safe_extract_number, | |
| process_uploaded_media, | |
| serve_static_file, | |
| session_status, | |
| forget_user, | |
| ) | |
| # --------------------------------------- | |
| # TIME ZONE EXAMPLE (not required but kept) | |
| # --------------------------------------- | |
| ist = pytz.timezone('Asia/Kolkata') | |
| current_datetime = datetime.now(ist) | |
| # --------------------------------------- | |
| # DEFAULT ROUTE β TWILIO WEBHOOK | |
| # --------------------------------------- | |
| def chatbot(): | |
| logging.info("π© New request received") | |
| resp = MessagingResponse() | |
| incoming_msg = request.values.get("Body", "").strip() | |
| media_url = request.values.get("MediaUrl0") | |
| media_content = None | |
| # Extract WhatsApp sender number | |
| user_number = request.values.get('From') | |
| if user_number: | |
| user_number = user_number.replace("whatsapp:", "").strip() | |
| logging.info(f"β‘ Incoming message: {incoming_msg}") | |
| logging.info(f"β‘ User number: {user_number}") | |
| # --------------------------------------- | |
| # IF MEDIA β PROCESS UPLOAD | |
| # --------------------------------------- | |
| if media_url: | |
| logging.info(f"πΈ Image uploaded: {media_url}") | |
| media_content = process_uploaded_media(media_url, user_number) | |
| # --------------------------------------- | |
| # BUSINESS LOGIC HANDLER | |
| # --------------------------------------- | |
| reply = business_logic( | |
| incoming_msg=incoming_msg, | |
| user_number=user_number, | |
| media_content=media_content, | |
| ) | |
| if reply: | |
| resp.message(reply) | |
| else: | |
| resp.message("Sorry, something went wrong. Please try again!") | |
| return str(resp) | |
| # --------------------------------------- | |
| # SERVE STATIC FILES FOR WHATSAPP LINKS | |
| # --------------------------------------- | |
| def serve_static(filename): | |
| return send_from_directory('static', filename) | |
| # --------------------------------------- | |
| # HEALTH CHECK ENDPOINT | |
| # --------------------------------------- | |
| def health(): | |
| return {"status": "running", "time": str(current_datetime)} | |
| # --------------------------------------- | |
| # RUN LOCAL ONLY | |
| # (On Hugging Face, gunicorn runs this file) | |
| # --------------------------------------- | |
| if __name__ == "__main__": | |
| logging.info("π Starting Flask App on port 5000...") | |
| app.run(host="0.0.0.0", port=5000, debug=False) | |