Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,90 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
-
from deep_translator import GoogleTranslator
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
CORS(app)
|
| 8 |
|
|
|
|
| 9 |
with open("diseases.json", "r", encoding="utf-8") as f:
|
| 10 |
disease_data = json.load(f)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@app.route("/chat", methods=["POST"])
|
| 13 |
def chat():
|
| 14 |
data = request.json
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
print("LANG RECEIVED
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if d.lower() in message.lower():
|
| 24 |
-
disease = d
|
| 25 |
-
break
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
Disease: {disease}
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
- """ + "\n- ".join(info["precautions"]) + """
|
| 43 |
|
| 44 |
-
|
| 45 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
if
|
| 48 |
-
|
| 49 |
-
source="en", target=
|
| 50 |
-
).translate(
|
| 51 |
else:
|
| 52 |
-
|
| 53 |
|
| 54 |
-
return jsonify({"reply":
|
| 55 |
-
|
| 56 |
-
@app.route("/")
|
| 57 |
-
def home():
|
| 58 |
-
return "Chatbot running"
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from flask_cors import CORS
|
|
|
|
| 3 |
import json
|
| 4 |
+
from deep_translator import GoogleTranslator
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
CORS(app)
|
| 8 |
|
| 9 |
+
# Load disease data
|
| 10 |
with open("diseases.json", "r", encoding="utf-8") as f:
|
| 11 |
disease_data = json.load(f)
|
| 12 |
|
| 13 |
+
DISEASE_ALIASES = {
|
| 14 |
+
"ringworm": "Tinea Ringworm Candidiasis",
|
| 15 |
+
"fungal infection": "Tinea Ringworm Candidiasis",
|
| 16 |
+
"eczema": "eczema",
|
| 17 |
+
"warts": "Warts Molluscum Viral Infections",
|
| 18 |
+
"melanoma": "melanoma",
|
| 19 |
+
"nevus": "nevus"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
SUPPORTED_LANGUAGES = {
|
| 23 |
+
"en": "English",
|
| 24 |
+
"ta": "Tamil",
|
| 25 |
+
"hi": "Hindi",
|
| 26 |
+
"te": "Telugu",
|
| 27 |
+
"ml": "Malayalam",
|
| 28 |
+
"kn": "Kannada",
|
| 29 |
+
"bn": "Bengali",
|
| 30 |
+
"mr": "Marathi",
|
| 31 |
+
"ur": "Urdu",
|
| 32 |
+
"es": "Spanish"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
@app.route("/")
|
| 36 |
+
def home():
|
| 37 |
+
return "Chatbot backend running"
|
| 38 |
+
|
| 39 |
@app.route("/chat", methods=["POST"])
|
| 40 |
def chat():
|
| 41 |
data = request.json
|
| 42 |
|
| 43 |
+
user_input = data.get("message", "")
|
| 44 |
+
user_lang = data.get("language", "en") # 🔥 THIS IS KEY
|
| 45 |
|
| 46 |
+
print("LANG RECEIVED 👉", user_lang) # 🔥 DEBUG (HF logs)
|
| 47 |
|
| 48 |
+
if user_lang not in SUPPORTED_LANGUAGES:
|
| 49 |
+
user_lang = "en"
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Translate input to English
|
| 52 |
+
if user_lang != "en":
|
| 53 |
+
translated_input = GoogleTranslator(
|
| 54 |
+
source=user_lang, target="en"
|
| 55 |
+
).translate(user_input)
|
| 56 |
+
else:
|
| 57 |
+
translated_input = user_input
|
| 58 |
|
| 59 |
+
normalized_input = translated_input.lower()
|
|
|
|
| 60 |
|
| 61 |
+
matched_disease = None
|
| 62 |
+
for key, value in DISEASE_ALIASES.items():
|
| 63 |
+
if key in normalized_input:
|
| 64 |
+
matched_disease = value
|
| 65 |
+
break
|
| 66 |
|
| 67 |
+
if not matched_disease or matched_disease not in disease_data:
|
| 68 |
+
return jsonify({"reply": "Disease information not found"})
|
| 69 |
|
| 70 |
+
info = disease_data[matched_disease]
|
|
|
|
| 71 |
|
| 72 |
+
english_response = (
|
| 73 |
+
f"Disease: {matched_disease}\n\n"
|
| 74 |
+
f"Description:\n{info['description']}\n\n"
|
| 75 |
+
f"Symptoms:\n➤ " + "\n➤ ".join(info["symptoms"]) + "\n\n"
|
| 76 |
+
f"Precautions:\n➤ " + "\n➤ ".join(info["precautions"]) + "\n\n"
|
| 77 |
+
"⚠ Educational purpose only"
|
| 78 |
+
)
|
| 79 |
|
| 80 |
+
if user_lang != "en":
|
| 81 |
+
final_response = GoogleTranslator(
|
| 82 |
+
source="en", target=user_lang
|
| 83 |
+
).translate(english_response)
|
| 84 |
else:
|
| 85 |
+
final_response = english_response
|
| 86 |
|
| 87 |
+
return jsonify({"reply": final_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
app.run(host="0.0.0.0", port=7860)
|