Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,10 @@ 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')
|
| 11 |
|
| 12 |
-
#
|
| 13 |
school_data = {
|
| 14 |
"Stoicism": [
|
| 15 |
"The key to happiness is accepting things we cannot control.",
|
|
@@ -43,42 +43,32 @@ school_profiles = {
|
|
| 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 |
-
|
| 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 |
}
|
|
@@ -139,70 +129,47 @@ def analyze_text(text):
|
|
| 139 |
return "Please enter a philosophical text.", "", "", "", "", None, ""
|
| 140 |
|
| 141 |
user_vec = model.encode([text])[0]
|
| 142 |
-
|
| 143 |
-
best_school = None
|
| 144 |
-
best_score = -1
|
| 145 |
-
best_match = ""
|
| 146 |
|
| 147 |
for school, quotes in school_data.items():
|
| 148 |
for quote in quotes:
|
| 149 |
quote_vec = model.encode([quote])[0]
|
| 150 |
score = cosine_similarity([user_vec], [quote_vec])[0][0]
|
| 151 |
if score > best_score:
|
| 152 |
-
best_score = score
|
| 153 |
-
best_school = school
|
| 154 |
-
best_match = quote
|
| 155 |
|
| 156 |
semantic_plot = create_semantic_plot(user_vec, 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 |
|
|
|
|
|
|
|
| 164 |
|
| 165 |
with gr.Blocks(title="Philosophical Analyzer") as demo:
|
| 166 |
gr.Markdown("## 📝 Enter Philosophical Text")
|
| 167 |
input_text = gr.Textbox(lines=4, placeholder="Type or paste a philosophical text...")
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
| 170 |
|
| 171 |
with gr.Row():
|
| 172 |
with gr.Column():
|
| 173 |
-
gr.
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
gr.
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
gr.Markdown("### 🧬 School Profile:")
|
| 180 |
-
profile_box = gr.Textbox()
|
| 181 |
-
|
| 182 |
-
gr.Markdown("### 🧠 Psychological Profile:")
|
| 183 |
-
psych_box = gr.Textbox()
|
| 184 |
-
|
| 185 |
-
gr.Markdown("### 🕰️ Philosophy Timeline")
|
| 186 |
-
timeline = gr.Textbox()
|
| 187 |
-
|
| 188 |
-
with gr.Accordion("🧾 Most Similar Reference Quote", open=False):
|
| 189 |
best_quote = gr.Textbox()
|
| 190 |
-
|
| 191 |
with gr.Column():
|
| 192 |
conceptual_map = gr.Plot(label="🧭 Conceptual Map")
|
| 193 |
|
| 194 |
-
submit_btn.click(
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
outputs=[
|
| 198 |
-
school,
|
| 199 |
-
score,
|
| 200 |
-
profile_box,
|
| 201 |
-
timeline,
|
| 202 |
-
best_quote,
|
| 203 |
-
conceptual_map,
|
| 204 |
-
psych_box
|
| 205 |
-
]
|
| 206 |
-
)
|
| 207 |
|
| 208 |
demo.launch()
|
|
|
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import re
|
| 8 |
|
| 9 |
+
# Load model
|
| 10 |
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
| 11 |
|
| 12 |
+
# Reference philosophical data
|
| 13 |
school_data = {
|
| 14 |
"Stoicism": [
|
| 15 |
"The key to happiness is accepting things we cannot control.",
|
|
|
|
| 43 |
psychological_categories = [
|
| 44 |
{
|
| 45 |
"name": "Moral guilt",
|
| 46 |
+
"keywords": ["guilt", "remorse", "regret", "پشیمانی", "عذاب وجدان", "ندامت"],
|
|
|
|
|
|
|
| 47 |
"followed": "Helps in personal growth, emotional release, and reconciliation.",
|
| 48 |
"ignored": "Can lead to chronic anxiety, regret, or depression."
|
| 49 |
},
|
| 50 |
{
|
| 51 |
"name": "Freedom & Will",
|
| 52 |
+
"keywords": ["freedom", "free will", "liberty", "independence", "آزادی", "اراده", "خودمختاری"],
|
|
|
|
|
|
|
| 53 |
"followed": "Encourages autonomy and a sense of responsibility.",
|
| 54 |
"ignored": "May result in feelings of helplessness or existential crisis."
|
| 55 |
},
|
| 56 |
{
|
| 57 |
"name": "Altruism",
|
| 58 |
+
"keywords": ["altruism", "help", "helping", "helped", "assist", "support", "others",
|
| 59 |
+
"نوعدوستی", "کمک", "یاری", "حمایت"],
|
|
|
|
|
|
|
| 60 |
"followed": "Improves social bonding and personal satisfaction.",
|
| 61 |
"ignored": "May cause loneliness or self-centered thinking."
|
| 62 |
},
|
| 63 |
{
|
| 64 |
"name": "Anxiety & Fear",
|
| 65 |
+
"keywords": ["anxiety", "fear", "worry", "stress", "ترس", "اضطراب", "استرس", "نگرانی"],
|
|
|
|
|
|
|
| 66 |
"followed": "Leads to caution and preparedness.",
|
| 67 |
"ignored": "Might cause mental blocks, phobia, or stress disorders."
|
| 68 |
},
|
| 69 |
{
|
| 70 |
"name": "Rationality",
|
| 71 |
+
"keywords": ["reason", "logic", "rational", "عقل", "منطق", "استدلال", "عقلانی"],
|
|
|
|
|
|
|
| 72 |
"followed": "Supports wise decision-making and clarity.",
|
| 73 |
"ignored": "Can result in poor judgments and emotional impulses."
|
| 74 |
}
|
|
|
|
| 129 |
return "Please enter a philosophical text.", "", "", "", "", None, ""
|
| 130 |
|
| 131 |
user_vec = model.encode([text])[0]
|
| 132 |
+
best_school, best_score, best_match = None, -1, ""
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
for school, quotes in school_data.items():
|
| 135 |
for quote in quotes:
|
| 136 |
quote_vec = model.encode([quote])[0]
|
| 137 |
score = cosine_similarity([user_vec], [quote_vec])[0][0]
|
| 138 |
if score > best_score:
|
| 139 |
+
best_score, best_school, best_match = score, school, quote
|
|
|
|
|
|
|
| 140 |
|
| 141 |
semantic_plot = create_semantic_plot(user_vec, best_school)
|
| 142 |
school_profile = school_profiles[best_school]["profile"]
|
| 143 |
timeline = school_profiles[best_school]["timeline"]
|
|
|
|
| 144 |
psych_output = psychological_analysis(text)
|
| 145 |
|
| 146 |
return best_school, f"{best_score:.2f}", school_profile, timeline, best_match, semantic_plot, psych_output
|
| 147 |
|
| 148 |
+
def clear_fields():
|
| 149 |
+
return "", "", "", "", "", None, ""
|
| 150 |
|
| 151 |
with gr.Blocks(title="Philosophical Analyzer") as demo:
|
| 152 |
gr.Markdown("## 📝 Enter Philosophical Text")
|
| 153 |
input_text = gr.Textbox(lines=4, placeholder="Type or paste a philosophical text...")
|
| 154 |
|
| 155 |
+
with gr.Row():
|
| 156 |
+
submit_btn = gr.Button("Analyze")
|
| 157 |
+
clear_btn = gr.Button("Clear")
|
| 158 |
|
| 159 |
with gr.Row():
|
| 160 |
with gr.Column():
|
| 161 |
+
school = gr.Textbox(label="Philosophical School Detected")
|
| 162 |
+
score = gr.Textbox(label="Similarity Score")
|
| 163 |
+
profile_box = gr.Textbox(label="School Profile")
|
| 164 |
+
psych_box = gr.Textbox(label="Psychological Profile")
|
| 165 |
+
timeline = gr.Textbox(label="Philosophy Timeline")
|
| 166 |
+
with gr.Accordion("Most Similar Reference Quote", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
best_quote = gr.Textbox()
|
|
|
|
| 168 |
with gr.Column():
|
| 169 |
conceptual_map = gr.Plot(label="🧭 Conceptual Map")
|
| 170 |
|
| 171 |
+
submit_btn.click(analyze_text, inputs=input_text,
|
| 172 |
+
outputs=[school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
| 173 |
+
clear_btn.click(clear_fields, outputs=[school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
demo.launch()
|