from flask import Flask, request, jsonify, send_file, after_this_request from flask_cors import CORS import json import os import uuid # ๐Ÿ”Š Text To Speech from gtts import gTTS # ๐ŸŒ Optional translator try: from deep_translator import GoogleTranslator TRANSLATOR_AVAILABLE = True except: TRANSLATOR_AVAILABLE = False app = Flask(__name__) CORS(app) # ----------------------------- # Load disease data # ----------------------------- BASE_DIR = os.path.dirname(os.path.abspath(__file__)) DATA_PATH = os.path.join(BASE_DIR, "diseases.json") with open(DATA_PATH, "r", encoding="utf-8") as f: disease_data = json.load(f) # dict # ----------------------------- # Disease aliases # ----------------------------- DISEASE_ALIASES = { "vascular": "vascular lesion", "vascular lesion": "vascular lesion", "bcc": "Basal Cell Carcinoma (BCC)", "basal cell": "Basal Cell Carcinoma (BCC)", "scc": "squamous cell carcinoma", "squamous": "squamous cell carcinoma", "nv": "Melanocytic Nevi (NV)", "melanocytic": "Melanocytic Nevi (NV)", "mole": "nevus", "bkl": "Benign Keratosis-like Lesions (BKL)", "keratosis": "Benign Keratosis-like Lesions (BKL)", "ringworm": "Tinea Ringworm Candidiasis", "fungal infection": "Tinea Ringworm Candidiasis", "warts": "Warts Molluscum Viral Infections", "ak": "actinic keratosis", "actinic": "actinic keratosis", "df": "dermatofibroma", "dermatofibroma": "dermatofibroma", "pigmented keratosis": "pigmented benign keratosis" } # ----------------------------- # Supported languages # ----------------------------- SUPPORTED_LANGUAGES = { "en": "English", "ta": "Tamil", "hi": "Hindi", "te": "Telugu", "ml": "Malayalam", "kn": "Kannada", "bn": "Bengali", "mr": "Marathi", "ur": "Urdu", "es": "Spanish" } # ----------------------------- # Home # ----------------------------- @app.route("/") def home(): return "Chatbot backend running" # ----------------------------- # CHAT API # ----------------------------- @app.route("/chat", methods=["GET", "POST"]) def chat(): # GET โ†’ simple test if request.method == "GET": return jsonify({ "info": "Use POST method", "example": { "message": "acne", "language": "en" } }) try: data = request.get_json(force=True) if "message" not in data: return jsonify({"reply": "Invalid request"}), 400 user_input = data["message"] user_lang = data.get("language", "en") if user_lang not in SUPPORTED_LANGUAGES: user_lang = "en" # ๐Ÿ”„ Translate input โ†’ English translated_input = user_input if user_lang != "en" and TRANSLATOR_AVAILABLE: try: translated_input = GoogleTranslator( source=user_lang, target="en" ).translate(user_input) except: translated_input = user_input normalized_input = translated_input.lower() # ๐Ÿ” Alias match matched_disease = None for key, value in DISEASE_ALIASES.items(): if key in normalized_input: matched_disease = value break # ๐Ÿ” Direct match if not matched_disease: for disease_name in disease_data.keys(): if disease_name.lower() in normalized_input: matched_disease = disease_name break if not matched_disease or matched_disease not in disease_data: return jsonify({"reply": "Disease information not found."}) info = disease_data[matched_disease] # ๐Ÿงพ English response english_response = ( f"Disease: {matched_disease}\n\n" f"Type: {info.get('type','-')}\n" f"Nature: {info.get('nature','-')}\n" f"Contagious: {info.get('contagious','-')}\n\n" f"Description:\n{info.get('description','-')}\n\n" f"Symptoms:\nโžค " + "\nโžค ".join(info.get("symptoms", [])) + "\n\n" f"Causes:\nโžค " + "\nโžค ".join(info.get("causes", [])) + "\n\n" f"Precautions:\nโžค " + "\nโžค ".join(info.get("precautions", [])) + "\n\n" f"Treatment:\nโžค " + "\nโžค ".join(info.get("treatment", [])) + "\n\n" "โš  Educational purpose only. Consult a dermatologist." ) # ๐ŸŒ Translate output final_response = english_response if user_lang != "en" and TRANSLATOR_AVAILABLE: try: final_response = GoogleTranslator( source="en", target=user_lang ).translate(english_response) except: final_response = english_response return jsonify({ "reply": final_response, "language": SUPPORTED_LANGUAGES[user_lang] }) except Exception as e: print("CHAT ERROR:", e) return jsonify({"reply": "Server error"}), 500 # ----------------------------- # ๐Ÿ”Š TEXT TO SPEECH API (FINAL) # ----------------------------- @app.route("/tts", methods=["POST"]) def tts(): try: data = request.get_json(force=True) text = data.get("text") lang = data.get("language", "en") if not text: return jsonify({"error": "No text provided"}), 400 if lang not in SUPPORTED_LANGUAGES: lang = "en" filename = f"tts_{uuid.uuid4()}.mp3" gTTS(text=text, lang=lang).save(filename) # ๐Ÿ”ฅ delete file after sending @after_this_request def cleanup(response): try: os.remove(filename) except: pass return response return send_file( filename, mimetype="audio/mpeg", as_attachment=False # IMPORTANT for browser play ) except Exception as e: print("TTS ERROR:", e) return jsonify({"error": "TTS failed"}), 500 # ----------------------------- # RUN # ----------------------------- if __name__ == "__main__": app.run(host="0.0.0.0", port=7860, debug=True)