sivakumar330 commited on
Commit
dedcd45
·
verified ·
1 Parent(s): 19cb3f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -12
app.py CHANGED
@@ -6,19 +6,24 @@ from deep_translator import GoogleTranslator
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",
@@ -39,16 +44,13 @@ def home():
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"
@@ -58,25 +60,39 @@ def chat():
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
@@ -84,7 +100,10 @@ def chat():
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)
 
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
  DISEASE_ALIASES = {
15
+ "vascular": "vascular lesion",
16
+ "bcc": "Basal Cell Carcinoma (BCC)",
17
+ "basal cell": "Basal Cell Carcinoma (BCC)",
18
+ "scc": "squamous cell carcinoma",
19
  "ringworm": "Tinea Ringworm Candidiasis",
20
  "fungal infection": "Tinea Ringworm Candidiasis",
 
21
  "warts": "Warts Molluscum Viral Infections",
22
+ "mole": "nevus",
23
+ "pigmented keratosis": "pigmented benign keratosis"
24
  }
25
 
26
+ # 🌍 Supported languages
27
  SUPPORTED_LANGUAGES = {
28
  "en": "English",
29
  "ta": "Tamil",
 
44
  @app.route("/chat", methods=["POST"])
45
  def chat():
46
  data = request.json
 
47
  user_input = data.get("message", "")
48
+ user_lang = data.get("language", "en") # 👈 dropdown language
 
 
49
 
50
  if user_lang not in SUPPORTED_LANGUAGES:
51
  user_lang = "en"
52
 
53
+ # 🔄 Translate input English
54
  if user_lang != "en":
55
  translated_input = GoogleTranslator(
56
  source=user_lang, target="en"
 
60
 
61
  normalized_input = translated_input.lower()
62
 
63
+ # 🔍 Alias match
64
  matched_disease = None
65
  for key, value in DISEASE_ALIASES.items():
66
  if key in normalized_input:
67
  matched_disease = value
68
  break
69
 
70
+ # 🔍 Direct match
71
+ if not matched_disease:
72
+ for disease in disease_data:
73
+ if disease.lower() in normalized_input:
74
+ matched_disease = disease
75
+ break
76
+
77
  if not matched_disease or matched_disease not in disease_data:
78
+ return jsonify({
79
+ "reply": "Disease information not found."
80
+ })
81
 
82
  info = disease_data[matched_disease]
83
 
84
+ # 🧾 English base response
85
  english_response = (
86
  f"Disease: {matched_disease}\n\n"
87
  f"Description:\n{info['description']}\n\n"
88
  f"Symptoms:\n➤ " + "\n➤ ".join(info["symptoms"]) + "\n\n"
89
+ f"Causes:\n➤ " + "\n➤ ".join(info["causes"]) + "\n\n"
90
  f"Precautions:\n➤ " + "\n➤ ".join(info["precautions"]) + "\n\n"
91
+ f"Treatment:\n➤ " + "\n➤ ".join(info["treatment"]) + "\n\n"
92
+ "⚠ Educational purpose only. Consult a dermatologist."
93
  )
94
 
95
+ # 🌍 Translate output
96
  if user_lang != "en":
97
  final_response = GoogleTranslator(
98
  source="en", target=user_lang
 
100
  else:
101
  final_response = english_response
102
 
103
+ return jsonify({
104
+ "reply": final_response,
105
+ "language": SUPPORTED_LANGUAGES[user_lang]
106
+ })
107
 
108
  if __name__ == "__main__":
109
  app.run(host="0.0.0.0", port=7860)