Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
texts = list(school_data_dynamic.values())
|
| 36 |
-
keys = list(school_data_dynamic.keys())
|
| 37 |
-
embeddings = model.encode(texts, convert_to_tensor=False)
|
| 38 |
-
|
| 39 |
-
# کاهش ابعاد برای رسم
|
| 40 |
-
pca = PCA(n_components=2)
|
| 41 |
-
reduced_embeddings = pca.fit_transform(embeddings)
|
| 42 |
-
|
| 43 |
-
# تابع جستجو
|
| 44 |
-
def search(query):
|
| 45 |
-
query_emb = model.encode([query], convert_to_tensor=False)
|
| 46 |
-
sims = cosine_similarity([query_emb[0]], embeddings)[0]
|
| 47 |
-
top_idx = np.argmax(sims)
|
| 48 |
-
return f"Closest match: {keys[top_idx]} → {texts[top_idx]}"
|
| 49 |
-
|
| 50 |
-
# تابع برای رسم گراف
|
| 51 |
-
def plot_embeddings():
|
| 52 |
-
fig = go.Figure()
|
| 53 |
-
fig.add_trace(go.Scatter(
|
| 54 |
-
x=reduced_embeddings[:,0],
|
| 55 |
-
y=reduced_embeddings[:,1],
|
| 56 |
-
mode="markers+text",
|
| 57 |
-
text=keys,
|
| 58 |
-
textposition="top center"
|
| 59 |
-
))
|
| 60 |
-
return fig
|
| 61 |
-
|
| 62 |
-
# رابط Gradio
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
gr.Markdown("
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
btn.click(fn=search, inputs=inp, outputs=out)
|
| 71 |
-
demo.load(fn=plot_embeddings, inputs=None, outputs=graph)
|
| 72 |
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# مسیر CSV
|
| 6 |
+
csv_path = "stoic_quotes_full.csv"
|
| 7 |
+
|
| 8 |
+
# بارگذاری دادهها
|
| 9 |
+
def load_data():
|
| 10 |
+
if not os.path.exists(csv_path):
|
| 11 |
+
return None
|
| 12 |
+
return pd.read_csv(csv_path)
|
| 13 |
+
|
| 14 |
+
# تحلیل متن
|
| 15 |
+
def analyze_text(text):
|
| 16 |
+
df = load_data()
|
| 17 |
+
if df is None:
|
| 18 |
+
return "❌ دیتابیس پیدا نشد."
|
| 19 |
+
|
| 20 |
+
text = text.strip().lower()
|
| 21 |
+
if not text:
|
| 22 |
+
return "⚠️ لطفاً یک متن وارد کنید."
|
| 23 |
+
|
| 24 |
+
# جستجو در نقلقولها
|
| 25 |
+
match = df[df['quote'].str.lower() == text]
|
| 26 |
+
|
| 27 |
+
if not match.empty:
|
| 28 |
+
philosopher = match.iloc[0]['philosopher']
|
| 29 |
+
return f"✅ این جمله از {philosopher} است."
|
| 30 |
+
else:
|
| 31 |
+
return "❌ این جمله در دیتاست وجود ندارد."
|
| 32 |
+
|
| 33 |
+
# رابط کاربری
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## متن فلسفی را وارد کنید")
|
| 36 |
+
text_input = gr.Textbox(label="متن")
|
| 37 |
+
analyze_btn = gr.Button("تحلیل")
|
| 38 |
+
output = gr.Textbox(label="نتیجه")
|
| 39 |
+
|
| 40 |
+
analyze_btn.click(analyze_text, inputs=text_input, outputs=output)
|
|
|
|
|
|
|
| 41 |
|
| 42 |
demo.launch()
|