Shirjannn commited on
Commit
bcbbad4
·
verified ·
1 Parent(s): 72cc116

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -56
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
- rules = [
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
- lower_text = text.lower()
78
- for rule in rules:
79
- if any(keyword in lower_text for keyword in rule["keywords"]):
80
- results.append({
81
- "aspect": rule["aspect"],
82
- "followed": rule["followed"],
83
- "ignored": rule["ignored"]
84
- })
85
- return results
 
 
 
 
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
- psych_results = psychological_analysis(text)
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, # best_school
188
- score, # similarity score
189
- profile_box, # school_profile
190
- timeline, # timeline
191
- best_quote, # best_match
192
- conceptual_map,# semantic_plot
193
- psych_box # psych_output
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