Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import matplotlib
|
| 4 |
-
matplotlib.use('Agg')
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
from sklearn.decomposition import PCA
|
| 9 |
import plotly.graph_objects as go
|
| 10 |
-
from plotly.subplots import make_subplots
|
| 11 |
|
| 12 |
-
# مدل زبانی
|
| 13 |
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
| 14 |
|
| 15 |
-
# دادهها
|
| 16 |
school_data = {
|
| 17 |
"Stoicism": [
|
| 18 |
"The key to happiness is accepting things we cannot control.",
|
|
@@ -36,7 +32,6 @@ school_data = {
|
|
| 36 |
]
|
| 37 |
}
|
| 38 |
|
| 39 |
-
# دادههای زمانی و پروفایل روانشناختی
|
| 40 |
school_profiles = {
|
| 41 |
"Stoicism": {"timeline": "3rd century BCE – 3rd century CE", "profile": "Calm, Resilient, Virtue-centered"},
|
| 42 |
"Existentialism": {"timeline": "19th – 20th century", "profile": "Individualistic, Authentic, Anxious"},
|
|
@@ -44,73 +39,6 @@ school_profiles = {
|
|
| 44 |
"Empiricism": {"timeline": "17th – 18th century", "profile": "Practical, Observational, Experimental"}
|
| 45 |
}
|
| 46 |
|
| 47 |
-
# تابع تحلیل و یافتن مکتب فلسفی
|
| 48 |
-
def analyze_text(text):
|
| 49 |
-
if not text.strip():
|
| 50 |
-
return "Please enter a philosophical text.", None, None, None, None, None
|
| 51 |
-
|
| 52 |
-
user_vec = model.encode([text])[0]
|
| 53 |
-
|
| 54 |
-
best_school = None
|
| 55 |
-
best_score = -1
|
| 56 |
-
best_match = ""
|
| 57 |
-
|
| 58 |
-
for school, quotes in school_data.items():
|
| 59 |
-
for quote in quotes:
|
| 60 |
-
quote_vec = model.encode([quote])[0]
|
| 61 |
-
score = cosine_similarity([user_vec], [quote_vec])[0][0]
|
| 62 |
-
if score > best_score:
|
| 63 |
-
best_score = score
|
| 64 |
-
best_school = school
|
| 65 |
-
best_match = quote
|
| 66 |
-
|
| 67 |
-
# گراف مفهومی (Conceptual Map)
|
| 68 |
-
semantic_plot = create_semantic_plot(user_vec, best_match, best_school)
|
| 69 |
-
|
| 70 |
-
# بازگشت خروجیها
|
| 71 |
-
return (
|
| 72 |
-
best_school,
|
| 73 |
-
f"{best_score:.2f}",
|
| 74 |
-
school_profiles[best_school]["profile"],
|
| 75 |
-
school_profiles[best_school]["timeline"],
|
| 76 |
-
best_match,
|
| 77 |
-
semantic_plot
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
# تابع رسم گراف مفهومی
|
| 81 |
-
def create_semantic_plot(user_vec, best_quote, best_school):
|
| 82 |
-
ref_quotes = school_data[best_school]
|
| 83 |
-
quote_vecs = model.encode(ref_quotes)
|
| 84 |
-
labels = [f"Ref {i+1}" for i in range(len(ref_quotes))]
|
| 85 |
-
|
| 86 |
-
all_vecs = np.vstack([user_vec, quote_vecs])
|
| 87 |
-
pca = PCA(n_components=2)
|
| 88 |
-
reduced_vecs = pca.fit_transform(all_vecs)
|
| 89 |
-
|
| 90 |
-
fig = go.Figure()
|
| 91 |
-
fig.add_trace(go.Scatter(
|
| 92 |
-
x=[reduced_vecs[0][0]],
|
| 93 |
-
y=[reduced_vecs[0][1]],
|
| 94 |
-
mode='markers+text',
|
| 95 |
-
text=["User Input"],
|
| 96 |
-
name="User Input",
|
| 97 |
-
textposition="top center",
|
| 98 |
-
marker=dict(size=14, symbol='circle', color='blue')
|
| 99 |
-
))
|
| 100 |
-
|
| 101 |
-
fig.add_trace(go.Scatter(
|
| 102 |
-
x=reduced_vecs[1:, 0],
|
| 103 |
-
y=reduced_vecs[1:, 1],
|
| 104 |
-
mode='markers+text',
|
| 105 |
-
text=labels,
|
| 106 |
-
name="Reference Quotes",
|
| 107 |
-
textposition="top center",
|
| 108 |
-
marker=dict(size=12, symbol='square', color='orange')
|
| 109 |
-
))
|
| 110 |
-
|
| 111 |
-
fig.update_layout(title="🧭 Conceptual Map", showlegend=False)
|
| 112 |
-
return fig
|
| 113 |
-
|
| 114 |
def psychological_analysis(text):
|
| 115 |
rules = [
|
| 116 |
{
|
|
@@ -147,7 +75,6 @@ def psychological_analysis(text):
|
|
| 147 |
|
| 148 |
results = []
|
| 149 |
lower_text = text.lower()
|
| 150 |
-
|
| 151 |
for rule in rules:
|
| 152 |
if any(keyword in lower_text for keyword in rule["keywords"]):
|
| 153 |
results.append({
|
|
@@ -155,19 +82,76 @@ def psychological_analysis(text):
|
|
| 155 |
"followed": rule["followed"],
|
| 156 |
"ignored": rule["ignored"]
|
| 157 |
})
|
| 158 |
-
|
| 159 |
return results
|
| 160 |
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
|
| 170 |
-
# رابط کاربری با Gradio
|
| 171 |
with gr.Blocks(title="Philosophical Analyzer") as demo:
|
| 172 |
gr.Markdown("## 📝 Enter Philosophical Text")
|
| 173 |
input_text = gr.Textbox(lines=4, placeholder="Type or paste a philosophical text...")
|
|
@@ -176,26 +160,25 @@ with gr.Blocks(title="Philosophical Analyzer") as demo:
|
|
| 176 |
|
| 177 |
with gr.Row():
|
| 178 |
with gr.Column():
|
| 179 |
-
gr.Markdown("### 🕰️ Philosophy Timeline")
|
| 180 |
-
timeline = gr.Textbox(label="Timeline")
|
| 181 |
-
|
| 182 |
gr.Markdown("### 🧠 Philosophical School Detected:")
|
| 183 |
-
school = gr.Textbox(
|
| 184 |
|
| 185 |
gr.Markdown("### 📈 Similarity Score:")
|
| 186 |
-
score = gr.Textbox(
|
| 187 |
|
| 188 |
gr.Markdown("### 🧬 Psychological Profile:")
|
| 189 |
-
profile = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
with gr.Column():
|
| 192 |
conceptual_map = gr.Plot(label="🧭 Conceptual Map")
|
| 193 |
|
| 194 |
-
with gr.Accordion("🧾 Most Similar Reference Quote", open=False):
|
| 195 |
-
best_quote = gr.Textbox(label="Closest Match")
|
| 196 |
-
|
| 197 |
submit_btn.click(fn=analyze_text, inputs=input_text,
|
| 198 |
-
outputs=[school, score, profile, timeline, best_quote, conceptual_map])
|
| 199 |
|
| 200 |
-
# اجرا
|
| 201 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 3 |
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')
|
| 10 |
|
| 11 |
+
# دادهها
|
| 12 |
school_data = {
|
| 13 |
"Stoicism": [
|
| 14 |
"The key to happiness is accepting things we cannot control.",
|
|
|
|
| 32 |
]
|
| 33 |
}
|
| 34 |
|
|
|
|
| 35 |
school_profiles = {
|
| 36 |
"Stoicism": {"timeline": "3rd century BCE – 3rd century CE", "profile": "Calm, Resilient, Virtue-centered"},
|
| 37 |
"Existentialism": {"timeline": "19th – 20th century", "profile": "Individualistic, Authentic, Anxious"},
|
|
|
|
| 39 |
"Empiricism": {"timeline": "17th – 18th century", "profile": "Practical, Observational, Experimental"}
|
| 40 |
}
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def psychological_analysis(text):
|
| 43 |
rules = [
|
| 44 |
{
|
|
|
|
| 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({
|
|
|
|
| 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]
|
| 89 |
+
quote_vecs = model.encode(ref_quotes)
|
| 90 |
+
labels = [f"Ref {i+1}" for i in range(len(ref_quotes))]
|
| 91 |
|
| 92 |
+
all_vecs = np.vstack([user_vec, quote_vecs])
|
| 93 |
+
pca = PCA(n_components=2)
|
| 94 |
+
reduced_vecs = pca.fit_transform(all_vecs)
|
| 95 |
+
|
| 96 |
+
fig = go.Figure()
|
| 97 |
+
fig.add_trace(go.Scatter(
|
| 98 |
+
x=[reduced_vecs[0][0]],
|
| 99 |
+
y=[reduced_vecs[0][1]],
|
| 100 |
+
mode='markers+text',
|
| 101 |
+
text=["User Input"],
|
| 102 |
+
name="User Input",
|
| 103 |
+
textposition="top center",
|
| 104 |
+
marker=dict(size=14, symbol='circle', color='blue')
|
| 105 |
+
))
|
| 106 |
+
|
| 107 |
+
fig.add_trace(go.Scatter(
|
| 108 |
+
x=reduced_vecs[1:, 0],
|
| 109 |
+
y=reduced_vecs[1:, 1],
|
| 110 |
+
mode='markers+text',
|
| 111 |
+
text=labels,
|
| 112 |
+
name="Reference Quotes",
|
| 113 |
+
textposition="top center",
|
| 114 |
+
marker=dict(size=12, symbol='square', color='orange')
|
| 115 |
+
))
|
| 116 |
+
|
| 117 |
+
fig.update_layout(title="🧭 Conceptual Map", showlegend=False)
|
| 118 |
+
return fig
|
| 119 |
+
|
| 120 |
+
def analyze_text(text):
|
| 121 |
+
if not text.strip():
|
| 122 |
+
return "Please enter a philosophical text.", "", "", "", "", None, ""
|
| 123 |
+
|
| 124 |
+
user_vec = model.encode([text])[0]
|
| 125 |
+
|
| 126 |
+
best_school = None
|
| 127 |
+
best_score = -1
|
| 128 |
+
best_match = ""
|
| 129 |
+
|
| 130 |
+
for school, quotes in school_data.items():
|
| 131 |
+
for quote in quotes:
|
| 132 |
+
quote_vec = model.encode([quote])[0]
|
| 133 |
+
score = cosine_similarity([user_vec], [quote_vec])[0][0]
|
| 134 |
+
if score > best_score:
|
| 135 |
+
best_score = score
|
| 136 |
+
best_school = school
|
| 137 |
+
best_match = quote
|
| 138 |
+
|
| 139 |
+
semantic_plot = create_semantic_plot(user_vec, best_school)
|
| 140 |
+
profile = school_profiles[best_school]["profile"]
|
| 141 |
+
timeline = school_profiles[best_school]["timeline"]
|
| 142 |
+
|
| 143 |
+
# روانشناسی
|
| 144 |
+
psych_results = psychological_analysis(text)
|
| 145 |
+
psych_output = ""
|
| 146 |
+
if psych_results:
|
| 147 |
+
for item in psych_results:
|
| 148 |
+
psych_output += f"🧠 {item['aspect']}\n✅ If Followed: {item['followed']}\n❌ If Ignored: {item['ignored']}\n\n"
|
| 149 |
+
else:
|
| 150 |
+
psych_output = "No clear psychological indicators found."
|
| 151 |
+
|
| 152 |
+
return best_school, f"{best_score:.2f}", profile, timeline, best_match, semantic_plot, psych_output
|
| 153 |
|
| 154 |
|
|
|
|
| 155 |
with gr.Blocks(title="Philosophical Analyzer") as demo:
|
| 156 |
gr.Markdown("## 📝 Enter Philosophical Text")
|
| 157 |
input_text = gr.Textbox(lines=4, placeholder="Type or paste a philosophical text...")
|
|
|
|
| 160 |
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
| 163 |
gr.Markdown("### 🧠 Philosophical School Detected:")
|
| 164 |
+
school = gr.Textbox()
|
| 165 |
|
| 166 |
gr.Markdown("### 📈 Similarity Score:")
|
| 167 |
+
score = gr.Textbox()
|
| 168 |
|
| 169 |
gr.Markdown("### 🧬 Psychological Profile:")
|
| 170 |
+
profile = gr.Textbox()
|
| 171 |
+
|
| 172 |
+
gr.Markdown("### 🕰️ Philosophy Timeline")
|
| 173 |
+
timeline = gr.Textbox()
|
| 174 |
+
|
| 175 |
+
with gr.Accordion("🧾 Most Similar Reference Quote", open=False):
|
| 176 |
+
best_quote = gr.Textbox()
|
| 177 |
|
| 178 |
with gr.Column():
|
| 179 |
conceptual_map = gr.Plot(label="🧭 Conceptual Map")
|
| 180 |
|
|
|
|
|
|
|
|
|
|
| 181 |
submit_btn.click(fn=analyze_text, inputs=input_text,
|
| 182 |
+
outputs=[school, score, profile, timeline, best_quote, conceptual_map, profile])
|
| 183 |
|
|
|
|
| 184 |
demo.launch()
|