tarrasyed19472007 commited on
Commit
346006f
·
verified ·
1 Parent(s): 26d175e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py CHANGED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from transformers import BertTokenizer, BertForSequenceClassification
3
+ import torch
4
+
5
+ # Step 1: Load the dataset (adjust the dataset path if necessary)
6
+ df = pd.read_json("hf://datasets/theprint/mindfulness-alpaca/alpaca_data_export.json")
7
+
8
+ # Check the dataset structure to understand how the data is formatted
9
+ print(df.head()) # Ensure it contains columns like 'health_issue' or related info
10
+
11
+ # Step 2: Load the pre-trained BERT model and tokenizer
12
+ model = BertForSequenceClassification.from_pretrained("bert-base-uncased")
13
+ tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
14
+
15
+ # Function to predict the health issue based on user input (using BERT)
16
+ def predict_health_issue(question):
17
+ inputs = tokenizer(question, return_tensors="pt")
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+ logits = outputs.logits
21
+ predicted_class = torch.argmax(logits, dim=-1).item()
22
+ return predicted_class
23
+
24
+ # Step 3: Define a mapping of health issues to suggested cures or workouts
25
+ health_cures = {
26
+ "fatigue": "Try regular cardio workouts and improving your sleep habits.",
27
+ "joint pain": "Consider low-impact exercises like swimming or yoga.",
28
+ "stress": "Meditate regularly and try mindfulness exercises.",
29
+ "headaches": "Stay hydrated and manage your screen time.",
30
+ "weight gain": "Focus on a balanced diet and regular exercise like walking or jogging.",
31
+ "insomnia": "Develop a consistent bedtime routine and try relaxation techniques."
32
+ }
33
+
34
+ # Step 4: Ask the user a series of health-related questions and process responses
35
+ questions = [
36
+ "Do you feel constant fatigue?",
37
+ "Have you been experiencing any joint pain?",
38
+ "Do you have difficulty sleeping?",
39
+ "Are you feeling stressed frequently?",
40
+ "Have you been gaining weight unexpectedly?",
41
+ "Do you experience frequent headaches?"
42
+ ]
43
+
44
+ responses = []
45
+
46
+ print("Please answer the following questions to help identify your health issues:")
47
+
48
+ # Ask the questions and record responses
49
+ for question in questions:
50
+ response = input(question + " (Yes/No/Other): ")
51
+ responses.append(response)
52
+
53
+ # Step 5: Predict health issues and suggest cures
54
+ print("\nHealth Issue Analysis:")
55
+ for response in responses:
56
+ predicted_issue = predict_health_issue(response)
57
+ health_issue = df['health_issue'].iloc[predicted_issue] # Map prediction to a health issue
58
+ print(f"Predicted Health Issue: {health_issue}")
59
+
60
+ if health_issue in health_cures:
61
+ print(f"Suggested cure for {health_issue}: {health_cures[health_issue]}")
62
+ else:
63
+ print(f"No suggestion available for {health_issue}. Please consult a healthcare professional.")
64
+