Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
from sklearn.decomposition import PCA
|
| 6 |
import plotly.graph_objects as go
|
|
|
|
| 7 |
|
| 8 |
# مدل زبانی
|
| 9 |
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
|
@@ -39,50 +40,66 @@ school_profiles = {
|
|
| 39 |
"Empiricism": {"timeline": "17th – 18th century", "profile": "Practical, Observational, Experimental"}
|
| 40 |
}
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def psychological_analysis(text):
|
| 43 |
-
|
| 44 |
-
{
|
| 45 |
-
"keywords": ["guilt", "remorse"],
|
| 46 |
-
"aspect": "Moral guilt",
|
| 47 |
-
"followed": "Helps in personal growth, emotional release, and reconciliation.",
|
| 48 |
-
"ignored": "Can lead to chronic anxiety, regret, or depression."
|
| 49 |
-
},
|
| 50 |
-
{
|
| 51 |
-
"keywords": ["freedom", "free will"],
|
| 52 |
-
"aspect": "Freedom & Will",
|
| 53 |
-
"followed": "Encourages autonomy and a sense of responsibility.",
|
| 54 |
-
"ignored": "May result in feelings of helplessness or existential crisis."
|
| 55 |
-
},
|
| 56 |
-
{
|
| 57 |
-
"keywords": ["altruism", "help", "others"],
|
| 58 |
-
"aspect": "Altruism",
|
| 59 |
-
"followed": "Improves social bonding and personal satisfaction.",
|
| 60 |
-
"ignored": "May cause loneliness or self-centered thinking."
|
| 61 |
-
},
|
| 62 |
-
{
|
| 63 |
-
"keywords": ["anxiety", "fear"],
|
| 64 |
-
"aspect": "Anxiety & Fear",
|
| 65 |
-
"followed": "Leads to caution and preparedness.",
|
| 66 |
-
"ignored": "Might cause mental blocks, phobia, or stress disorders."
|
| 67 |
-
},
|
| 68 |
-
{
|
| 69 |
-
"keywords": ["reason", "logic"],
|
| 70 |
-
"aspect": "Rationality",
|
| 71 |
-
"followed": "Supports wise decision-making and clarity.",
|
| 72 |
-
"ignored": "Can result in poor judgments and emotional impulses."
|
| 73 |
-
}
|
| 74 |
-
]
|
| 75 |
-
|
| 76 |
results = []
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
def create_semantic_plot(user_vec, best_school):
|
| 88 |
ref_quotes = school_data[best_school]
|
|
@@ -140,13 +157,7 @@ def analyze_text(text):
|
|
| 140 |
school_profile = school_profiles[best_school]["profile"]
|
| 141 |
timeline = school_profiles[best_school]["timeline"]
|
| 142 |
|
| 143 |
-
|
| 144 |
-
psych_output = ""
|
| 145 |
-
if psych_results:
|
| 146 |
-
for item in psych_results:
|
| 147 |
-
psych_output += f"🧠 {item['aspect']}\n✅ If Followed: {item['followed']}\n❌ If Ignored: {item['ignored']}\n\n"
|
| 148 |
-
else:
|
| 149 |
-
psych_output = "No clear psychological indicators found."
|
| 150 |
|
| 151 |
return best_school, f"{best_score:.2f}", school_profile, timeline, best_match, semantic_plot, psych_output
|
| 152 |
|
|
@@ -184,13 +195,13 @@ with gr.Blocks(title="Philosophical Analyzer") as demo:
|
|
| 184 |
fn=analyze_text,
|
| 185 |
inputs=input_text,
|
| 186 |
outputs=[
|
| 187 |
-
school,
|
| 188 |
-
score,
|
| 189 |
-
profile_box,
|
| 190 |
-
timeline,
|
| 191 |
-
best_quote,
|
| 192 |
-
conceptual_map,
|
| 193 |
-
psych_box
|
| 194 |
]
|
| 195 |
)
|
| 196 |
|
|
|
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
from sklearn.decomposition import PCA
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
# مدل زبانی
|
| 10 |
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
|
|
|
| 40 |
"Empiricism": {"timeline": "17th – 18th century", "profile": "Practical, Observational, Experimental"}
|
| 41 |
}
|
| 42 |
|
| 43 |
+
psychological_categories = [
|
| 44 |
+
{
|
| 45 |
+
"name": "Moral guilt",
|
| 46 |
+
"keywords": [
|
| 47 |
+
"guilt", "remorse", "regret", "پشیمانی", "عذاب وجدان", "ندامت"
|
| 48 |
+
],
|
| 49 |
+
"followed": "Helps in personal growth, emotional release, and reconciliation.",
|
| 50 |
+
"ignored": "Can lead to chronic anxiety, regret, or depression."
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"name": "Freedom & Will",
|
| 54 |
+
"keywords": [
|
| 55 |
+
"freedom", "free will", "liberty", "independence", "آزادی", "اراده", "خودمختاری"
|
| 56 |
+
],
|
| 57 |
+
"followed": "Encourages autonomy and a sense of responsibility.",
|
| 58 |
+
"ignored": "May result in feelings of helplessness or existential crisis."
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"name": "Altruism",
|
| 62 |
+
"keywords": [
|
| 63 |
+
"altruism", "help", "helping", "helped", "assist", "support", "others",
|
| 64 |
+
"نوعدوستی", "کمک", "یاری", "حمایت"
|
| 65 |
+
],
|
| 66 |
+
"followed": "Improves social bonding and personal satisfaction.",
|
| 67 |
+
"ignored": "May cause loneliness or self-centered thinking."
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"name": "Anxiety & Fear",
|
| 71 |
+
"keywords": [
|
| 72 |
+
"anxiety", "fear", "worry", "stress", "ترس", "اضطراب", "استرس", "نگرانی"
|
| 73 |
+
],
|
| 74 |
+
"followed": "Leads to caution and preparedness.",
|
| 75 |
+
"ignored": "Might cause mental blocks, phobia, or stress disorders."
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"name": "Rationality",
|
| 79 |
+
"keywords": [
|
| 80 |
+
"reason", "logic", "rational", "عقل", "منطق", "استدلال", "عقلانی"
|
| 81 |
+
],
|
| 82 |
+
"followed": "Supports wise decision-making and clarity.",
|
| 83 |
+
"ignored": "Can result in poor judgments and emotional impulses."
|
| 84 |
+
}
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
def psychological_analysis(text):
|
| 88 |
+
text_lower = text.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
results = []
|
| 90 |
+
for category in psychological_categories:
|
| 91 |
+
for keyword in category["keywords"]:
|
| 92 |
+
if re.search(rf"\b{re.escape(keyword.lower())}\b", text_lower):
|
| 93 |
+
results.append(category)
|
| 94 |
+
break
|
| 95 |
+
|
| 96 |
+
if not results:
|
| 97 |
+
return "No clear psychological indicators found."
|
| 98 |
+
|
| 99 |
+
output = ""
|
| 100 |
+
for item in results:
|
| 101 |
+
output += f"🧠 {item['name']}\n✅ If Followed: {item['followed']}\n❌ If Ignored: {item['ignored']}\n\n"
|
| 102 |
+
return output
|
| 103 |
|
| 104 |
def create_semantic_plot(user_vec, best_school):
|
| 105 |
ref_quotes = school_data[best_school]
|
|
|
|
| 157 |
school_profile = school_profiles[best_school]["profile"]
|
| 158 |
timeline = school_profiles[best_school]["timeline"]
|
| 159 |
|
| 160 |
+
psych_output = psychological_analysis(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
return best_school, f"{best_score:.2f}", school_profile, timeline, best_match, semantic_plot, psych_output
|
| 163 |
|
|
|
|
| 195 |
fn=analyze_text,
|
| 196 |
inputs=input_text,
|
| 197 |
outputs=[
|
| 198 |
+
school,
|
| 199 |
+
score,
|
| 200 |
+
profile_box,
|
| 201 |
+
timeline,
|
| 202 |
+
best_quote,
|
| 203 |
+
conceptual_map,
|
| 204 |
+
psych_box
|
| 205 |
]
|
| 206 |
)
|
| 207 |
|