Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
import nltk
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
nltk.download('stopwords')
|
| 8 |
+
|
| 9 |
+
# Sample dataset with categories, questions, and answers
|
| 10 |
+
data = {
|
| 11 |
+
"Musculoskeletal Disorders": [
|
| 12 |
+
("What exercises can be recommended for a patient with lower back pain?",
|
| 13 |
+
"Exercises like McKenzie extensions, core strengthening, and pelvic tilts can help alleviate pain.")
|
| 14 |
+
],
|
| 15 |
+
"Neurological Disorders": [
|
| 16 |
+
("How do you assess balance in a patient post-stroke?",
|
| 17 |
+
"You can use tests like the Berg Balance Scale or the Functional Reach Test.")
|
| 18 |
+
],
|
| 19 |
+
"Sports Injuries": [
|
| 20 |
+
("How do you prevent recurrent ankle sprains in athletes?",
|
| 21 |
+
"Proprioception training, strengthening peroneal muscles, and using ankle supports can prevent recurrent sprains.")
|
| 22 |
+
],
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Preprocess user input and answers by removing stopwords
|
| 26 |
+
def preprocess_text(text):
|
| 27 |
+
stop_words = set(stopwords.words('english'))
|
| 28 |
+
return ' '.join([word for word in text.split() if word.lower() not in stop_words])
|
| 29 |
+
|
| 30 |
+
# Function to calculate similarity score
|
| 31 |
+
def calculate_similarity(user_answer, correct_answer):
|
| 32 |
+
vectorizer = TfidfVectorizer().fit_transform([preprocess_text(user_answer), preprocess_text(correct_answer)])
|
| 33 |
+
vectors = vectorizer.toarray()
|
| 34 |
+
score = cosine_similarity([vectors[0]], [vectors[1]])[0][0]
|
| 35 |
+
return round(score * 100, 2) # Return as percentage
|
| 36 |
+
|
| 37 |
+
# Streamlit app starts here
|
| 38 |
+
st.title("Physiotherapy Virtual Patient Interaction")
|
| 39 |
+
|
| 40 |
+
# Step 1: User chooses a category
|
| 41 |
+
category = st.selectbox("Choose a Category", list(data.keys()))
|
| 42 |
+
|
| 43 |
+
if category:
|
| 44 |
+
# Step 2: Random question from the chosen category
|
| 45 |
+
question, correct_answer = random.choice(data[category])
|
| 46 |
+
st.write(f"**Patient's Question:** {question}")
|
| 47 |
+
|
| 48 |
+
# Step 3: User input their answer
|
| 49 |
+
user_answer = st.text_area("Your Response", "")
|
| 50 |
+
|
| 51 |
+
# Step 4: Show similarity score
|
| 52 |
+
if st.button("Submit Answer"):
|
| 53 |
+
if user_answer.strip():
|
| 54 |
+
similarity_score = calculate_similarity(user_answer, correct_answer)
|
| 55 |
+
st.write(f"**Similarity Score:** {similarity_score}%")
|
| 56 |
+
else:
|
| 57 |
+
st.write("Please provide an answer before submitting.")
|
| 58 |
+
|