Update app.py
Browse files
app.py
CHANGED
|
@@ -9,35 +9,90 @@ import re
|
|
| 9 |
# Load model
|
| 10 |
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
school_data = {
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"Knowledge comes only from sensory experience.",
|
| 31 |
-
"The mind is a blank slate at birth.",
|
| 32 |
-
"All ideas originate in experience."
|
| 33 |
-
]
|
| 34 |
}
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
school_profiles = {
|
| 37 |
-
"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
psychological_categories = [
|
|
@@ -92,7 +147,7 @@ def psychological_analysis(text):
|
|
| 92 |
return output
|
| 93 |
|
| 94 |
def create_semantic_plot(user_vec, best_school):
|
| 95 |
-
ref_quotes = school_data[best_school]
|
| 96 |
quote_vecs = model.encode(ref_quotes)
|
| 97 |
labels = [f"Ref {i+1}" for i in range(len(ref_quotes))]
|
| 98 |
|
|
@@ -131,8 +186,8 @@ def analyze_text(text):
|
|
| 131 |
user_vec = model.encode([text])[0]
|
| 132 |
best_school, best_score, best_match = None, -1, ""
|
| 133 |
|
| 134 |
-
for school,
|
| 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:
|
|
@@ -172,4 +227,4 @@ with gr.Blocks(title="Philosophical Analyzer") as demo:
|
|
| 172 |
outputs=[school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
| 173 |
clear_btn.click(clear_fields, outputs=[input_text, school, score, profile_box, timeline, best_quote, conceptual_map, psych_box])
|
| 174 |
|
| 175 |
-
demo.launch()
|
|
|
|
| 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 = [
|
|
|
|
| 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 |
|
|
|
|
| 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]
|
| 193 |
if score > best_score:
|
|
|
|
| 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()
|