Update app.py
Browse files
app.py
CHANGED
|
@@ -5,96 +5,74 @@ 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')
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
school_data["Nietzschean"]["quotes"] = load_quotes_from_file("Friedrich-Nietzsche.txt", "Nietzsche")
|
| 65 |
-
|
| 66 |
-
# حذف مکاتب خالی
|
| 67 |
-
for school in list(school_data.keys()):
|
| 68 |
-
if not school_data[school]["quotes"]:
|
| 69 |
-
del school_data[school]
|
| 70 |
-
|
| 71 |
-
print(f"Loaded quotes from {len(school_data)} philosophical schools")
|
| 72 |
-
except Exception as e:
|
| 73 |
-
print(f"Error loading quotes: {str(e)}")
|
| 74 |
-
|
| 75 |
-
# فراخوانی تابع بارگیری
|
| 76 |
-
load_quotes()
|
| 77 |
-
|
| 78 |
-
# پروفایل مکاتب
|
| 79 |
-
school_profiles = {
|
| 80 |
-
"Hegelianism": {
|
| 81 |
-
"timeline": "19th century",
|
| 82 |
-
"profile": "Dialectical, Historical, Idealist"
|
| 83 |
-
},
|
| 84 |
-
"Aristotelianism": {
|
| 85 |
-
"timeline": "4th century BCE",
|
| 86 |
-
"profile": "Logical, Empirical, Teleological"
|
| 87 |
-
},
|
| 88 |
-
"Schopenhauerian": {
|
| 89 |
-
"timeline": "19th century",
|
| 90 |
-
"profile": "Pessimistic, Compassionate, Will-centered"
|
| 91 |
-
},
|
| 92 |
-
"Nietzschean": {
|
| 93 |
-
"timeline": "19th century",
|
| 94 |
-
"profile": "Existential, Will-to-Power, Übermensch"
|
| 95 |
-
}
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
psychological_categories = [
|
| 99 |
{
|
| 100 |
"name": "Moral guilt",
|
|
@@ -129,6 +107,9 @@ psychological_categories = [
|
|
| 129 |
}
|
| 130 |
]
|
| 131 |
|
|
|
|
|
|
|
|
|
|
| 132 |
def psychological_analysis(text):
|
| 133 |
text_lower = text.lower()
|
| 134 |
results = []
|
|
@@ -146,8 +127,11 @@ def psychological_analysis(text):
|
|
| 146 |
output += f"🧠 {item['name']}\n✅ If Followed: {item['followed']}\n❌ If Ignored: {item['ignored']}\n\n"
|
| 147 |
return output
|
| 148 |
|
|
|
|
|
|
|
|
|
|
| 149 |
def create_semantic_plot(user_vec, best_school):
|
| 150 |
-
ref_quotes = school_data[best_school]["quotes"]
|
| 151 |
quote_vecs = model.encode(ref_quotes)
|
| 152 |
labels = [f"Ref {i+1}" for i in range(len(ref_quotes))]
|
| 153 |
|
|
@@ -179,6 +163,9 @@ def create_semantic_plot(user_vec, best_school):
|
|
| 179 |
fig.update_layout(title="🧭 Conceptual Map", showlegend=False)
|
| 180 |
return fig
|
| 181 |
|
|
|
|
|
|
|
|
|
|
| 182 |
def analyze_text(text):
|
| 183 |
if not text.strip():
|
| 184 |
return "Please enter a philosophical text.", "", "", "", "", None, ""
|
|
@@ -186,7 +173,7 @@ def analyze_text(text):
|
|
| 186 |
user_vec = model.encode([text])[0]
|
| 187 |
best_school, best_score, best_match = None, -1, ""
|
| 188 |
|
| 189 |
-
for school, data in school_data.items():
|
| 190 |
for quote in data["quotes"]:
|
| 191 |
quote_vec = model.encode([quote])[0]
|
| 192 |
score = cosine_similarity([user_vec], [quote_vec])[0][0]
|
|
@@ -203,6 +190,9 @@ def analyze_text(text):
|
|
| 203 |
def clear_fields():
|
| 204 |
return "", "", "", "", "", "", None, ""
|
| 205 |
|
|
|
|
|
|
|
|
|
|
| 206 |
with gr.Blocks(title="Philosophical Analyzer") as demo:
|
| 207 |
gr.Markdown("## 📝 Enter Philosophical Text")
|
| 208 |
input_text = gr.Textbox(lines=4, placeholder="Type or paste a philosophical text...")
|
|
@@ -227,4 +217,4 @@ with gr.Blocks(title="Philosophical Analyzer") as demo:
|
|
| 227 |
outputs=[school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
| 228 |
clear_btn.click(clear_fields, outputs=[input_text, school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
| 229 |
|
| 230 |
-
demo.launch()
|
|
|
|
| 5 |
from sklearn.decomposition import PCA
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import re
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import zipfile
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# ------------------------
|
| 13 |
+
# تنظیمات مسیر فایلها
|
| 14 |
+
# ------------------------
|
| 15 |
+
# مسیر فایل CSV یا ZIP (در Hugging Face داخل repo قرار بده)
|
| 16 |
+
csv_path = "data/stoic_quotes_full.csv"
|
| 17 |
+
zip_path = "data/archive.zip"
|
| 18 |
+
|
| 19 |
+
# ------------------------
|
| 20 |
+
# بارگذاری مدل
|
| 21 |
+
# ------------------------
|
| 22 |
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
| 23 |
|
| 24 |
+
# ------------------------
|
| 25 |
+
# بارگذاری دادهها از CSV یا ZIP
|
| 26 |
+
# ------------------------
|
| 27 |
+
def build_school_data():
|
| 28 |
+
school_data_dynamic = {}
|
| 29 |
+
|
| 30 |
+
# حالت CSV
|
| 31 |
+
if os.path.exists(csv_path):
|
| 32 |
+
df = pd.read_csv(csv_path)
|
| 33 |
+
if "philosopher" in df.columns and "quote" in df.columns:
|
| 34 |
+
grouped = df.groupby("philosopher")["quote"].apply(list)
|
| 35 |
+
for philosopher, quotes in grouped.items():
|
| 36 |
+
school_data_dynamic[philosopher] = {
|
| 37 |
+
"philosophers": [philosopher],
|
| 38 |
+
"quotes": quotes
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# حالت ZIP (فایلهای txt)
|
| 42 |
+
elif os.path.exists(zip_path):
|
| 43 |
+
with zipfile.ZipFile(zip_path, 'r') as z:
|
| 44 |
+
for filename in z.namelist():
|
| 45 |
+
if filename.lower().endswith(".txt"):
|
| 46 |
+
with z.open(filename) as f:
|
| 47 |
+
content = f.read().decode("utf-8", errors="ignore")
|
| 48 |
+
sentences = re.split(r'(?<=[.!?])\s+', content)
|
| 49 |
+
quotes = [
|
| 50 |
+
s.strip() for s in sentences
|
| 51 |
+
if len(s.split()) > 4 and len(s) < 500
|
| 52 |
+
]
|
| 53 |
+
philosopher_name = os.path.splitext(os.path.basename(filename))[0]
|
| 54 |
+
school_data_dynamic[philosopher_name] = {
|
| 55 |
+
"philosophers": [philosopher_name],
|
| 56 |
+
"quotes": quotes
|
| 57 |
+
}
|
| 58 |
+
else:
|
| 59 |
+
print("⚠ No valid dataset found.")
|
| 60 |
+
|
| 61 |
+
return school_data_dynamic
|
| 62 |
+
|
| 63 |
+
school_data = build_school_data()
|
| 64 |
+
|
| 65 |
+
# ------------------------
|
| 66 |
+
# پروفایل مکاتب (دلخواه: میتونی این رو هم داینامیک بسازی)
|
| 67 |
+
# ------------------------
|
| 68 |
+
school_profiles = {name: {
|
| 69 |
+
"timeline": "Unknown",
|
| 70 |
+
"profile": "No profile available"
|
| 71 |
+
} for name in school_data.keys()}
|
| 72 |
+
|
| 73 |
+
# ------------------------
|
| 74 |
+
# دستهبندی روانشناختی
|
| 75 |
+
# ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
psychological_categories = [
|
| 77 |
{
|
| 78 |
"name": "Moral guilt",
|
|
|
|
| 107 |
}
|
| 108 |
]
|
| 109 |
|
| 110 |
+
# ------------------------
|
| 111 |
+
# تحلیل روانشناختی
|
| 112 |
+
# ------------------------
|
| 113 |
def psychological_analysis(text):
|
| 114 |
text_lower = text.lower()
|
| 115 |
results = []
|
|
|
|
| 127 |
output += f"🧠 {item['name']}\n✅ If Followed: {item['followed']}\n❌ If Ignored: {item['ignored']}\n\n"
|
| 128 |
return output
|
| 129 |
|
| 130 |
+
# ------------------------
|
| 131 |
+
# رسم نقشه مفهومی
|
| 132 |
+
# ------------------------
|
| 133 |
def create_semantic_plot(user_vec, best_school):
|
| 134 |
+
ref_quotes = school_data[best_school]["quotes"]
|
| 135 |
quote_vecs = model.encode(ref_quotes)
|
| 136 |
labels = [f"Ref {i+1}" for i in range(len(ref_quotes))]
|
| 137 |
|
|
|
|
| 163 |
fig.update_layout(title="🧭 Conceptual Map", showlegend=False)
|
| 164 |
return fig
|
| 165 |
|
| 166 |
+
# ------------------------
|
| 167 |
+
# تحلیل متن
|
| 168 |
+
# ------------------------
|
| 169 |
def analyze_text(text):
|
| 170 |
if not text.strip():
|
| 171 |
return "Please enter a philosophical text.", "", "", "", "", None, ""
|
|
|
|
| 173 |
user_vec = model.encode([text])[0]
|
| 174 |
best_school, best_score, best_match = None, -1, ""
|
| 175 |
|
| 176 |
+
for school, data in school_data.items():
|
| 177 |
for quote in data["quotes"]:
|
| 178 |
quote_vec = model.encode([quote])[0]
|
| 179 |
score = cosine_similarity([user_vec], [quote_vec])[0][0]
|
|
|
|
| 190 |
def clear_fields():
|
| 191 |
return "", "", "", "", "", "", None, ""
|
| 192 |
|
| 193 |
+
# ------------------------
|
| 194 |
+
# رابط Gradio
|
| 195 |
+
# ------------------------
|
| 196 |
with gr.Blocks(title="Philosophical Analyzer") as demo:
|
| 197 |
gr.Markdown("## 📝 Enter Philosophical Text")
|
| 198 |
input_text = gr.Textbox(lines=4, placeholder="Type or paste a philosophical text...")
|
|
|
|
| 217 |
outputs=[school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
| 218 |
clear_btn.click(clear_fields, outputs=[input_text, school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
| 219 |
|
| 220 |
+
demo.launch()
|