Shirjannn commited on
Commit
af74727
·
verified ·
1 Parent(s): 6c575f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -91
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
- # Load model
 
 
 
 
 
 
 
 
 
 
 
 
10
  model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
11
 
12
- # تابع برای استخراج نقل‌قول‌ها از فایل
13
- def load_quotes_from_file(filename, philosopher):
14
- try:
15
- with open(filename, 'r', encoding='utf-8') as file:
16
- content = file.read()
17
-
18
- # حذف بخش‌های متا
19
- content = re.sub(r'\[file name\].*?\[file content begin\]', '', content, flags=re.DOTALL)
20
- content = re.sub(r'\[file content end\].*', '', content, flags=re.DOTALL)
21
-
22
- # تقسیم به جملات معنادار
23
- sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|\!)\s', content)
24
-
25
- # فیلتر جملات کوتاه و بی‌معنی
26
- meaningful_quotes = [
27
- s.strip() for s in sentences
28
- if len(s.split()) > 4 # جملات حداقل 4 کلمه‌ای
29
- and not s.startswith((' ', '\n'))
30
- and len(s) < 500 # جملات خیلی طولانی نباشند
31
- ]
32
-
33
- return meaningful_quotes
34
- except Exception as e:
35
- print(f"Error loading {filename}: {str(e)}")
36
- return []
37
-
38
- # ساختار داده‌های فلسفی جدید
39
- school_data = {
40
- "Hegelianism": {
41
- "philosophers": ["Hegel"],
42
- "quotes": []
43
- },
44
- "Aristotelianism": {
45
- "philosophers": ["Aristotle"],
46
- "quotes": []
47
- },
48
- "Schopenhauerian": {
49
- "philosophers": ["Schopenhauer"],
50
- "quotes": []
51
- },
52
- "Nietzschean": {
53
- "philosophers": ["Nietzsche"],
54
- "quotes": []
55
- }
56
- }
57
-
58
- # تابع بارگیری نقل‌قول‌ها
59
- def load_quotes():
60
- try:
61
- school_data["Hegelianism"]["quotes"] = load_quotes_from_file("Hegel.txt", "Hegel")
62
- school_data["Aristotelianism"]["quotes"] = load_quotes_from_file("Aristotle.txt", "Aristotle")
63
- school_data["Schopenhauerian"]["quotes"] = load_quotes_from_file("Arthur-Schopenhauer-Quotes.txt", "Schopenhauer")
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()