Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sqlite3
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import random
|
| 6 |
+
import re
|
| 7 |
+
import torch
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
from transformers import pipeline
|
| 12 |
+
|
| 13 |
+
# --- DATABASE INITIALIZATION ---
|
| 14 |
+
def init_db():
|
| 15 |
+
conn = sqlite3.connect('faithpath.db')
|
| 16 |
+
cursor = conn.cursor()
|
| 17 |
+
cursor.execute('''
|
| 18 |
+
CREATE TABLE IF NOT EXISTS journal_entries (
|
| 19 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 20 |
+
date TEXT,
|
| 21 |
+
entry_text TEXT,
|
| 22 |
+
detected_emotion TEXT,
|
| 23 |
+
mission_source TEXT,
|
| 24 |
+
mission_text TEXT,
|
| 25 |
+
city TEXT
|
| 26 |
+
)
|
| 27 |
+
''')
|
| 28 |
+
conn.commit()
|
| 29 |
+
conn.close()
|
| 30 |
+
|
| 31 |
+
init_db()
|
| 32 |
+
|
| 33 |
+
# --- MODEL & LIBRARY LOADING ---
|
| 34 |
+
sentiment_analyzer = pipeline('sentiment-analysis', model='savasy/bert-base-turkish-sentiment-cased')
|
| 35 |
+
|
| 36 |
+
if os.path.exists('faith_library_final_v56.json'):
|
| 37 |
+
with open('faith_library_final_v56.json', 'r', encoding='utf-8') as f:
|
| 38 |
+
faith_library = json.load(f)
|
| 39 |
+
else:
|
| 40 |
+
faith_library = {}
|
| 41 |
+
|
| 42 |
+
# --- CORE ENGINE ---
|
| 43 |
+
def semantic_bridge_engine(metin):
|
| 44 |
+
text_lower = metin.lower()
|
| 45 |
+
exclusive_logic = {
|
| 46 |
+
"öfke_ve_gerginlik": ["sabrımın son kertesindeyim", "bendini aşan", "yanardağ gibiyim", "tepemin tasını", "damarıma bas", "nevrim döndü", "parlamamak", "zor tutuyorum"],
|
| 47 |
+
"şükran_ve_minnet": ["secde-i şükran", "selamet kapısı", "bereketli lokma", "nimetlerin kadrini", "hamd etsek az", "minnettarım", "şükür secdesine", "allah bin bereket", "halimize bin şükür"],
|
| 48 |
+
"sabır_ve_metanet": ["bağrıma taş basıp", "bağrıma taş bastım", "isyan etmeksizin", "metanetimi koruyarak", "dişini sıkıp", "sıramı bekleyerek", "zamanın değirmeninde", "sabır taşı"],
|
| 49 |
+
"cömertlik_ve_paylaşma": ["rızkı paylaştıkça", "ihtiyacı olana", "gönlümüzü açtık", "infak etmek", "sadaka vermek", "ikramda bulunmak"]
|
| 50 |
+
}
|
| 51 |
+
for cat, words in exclusive_logic.items():
|
| 52 |
+
if any(w in text_lower for w in words):
|
| 53 |
+
return cat, random.choice(faith_library.get(cat, []))
|
| 54 |
+
|
| 55 |
+
bert_res = sentiment_analyzer(metin)[0]
|
| 56 |
+
cat = "mutluluk_ve_huzur" if bert_res['label'] == 'positive' else "hüzün_ve_keder"
|
| 57 |
+
return cat, random.choice(faith_library.get(cat, []))
|
| 58 |
+
|
| 59 |
+
# --- UI FUNCTIONS ---
|
| 60 |
+
def process_entry_no_city(text):
|
| 61 |
+
if len(text.strip()) < 10:
|
| 62 |
+
return "<div style='padding:20px; background:#7f1d1d; color:white; border-radius:10px;'>Hata: Lütfen analiz için daha uzun bir cümle yazın.</div>"
|
| 63 |
+
|
| 64 |
+
cat, mission = semantic_bridge_engine(text)
|
| 65 |
+
cat_title = cat.replace('_', ' ').title()
|
| 66 |
+
|
| 67 |
+
conn = sqlite3.connect('faithpath.db')
|
| 68 |
+
cursor = conn.cursor()
|
| 69 |
+
cursor.execute('''
|
| 70 |
+
INSERT INTO journal_entries (date, entry_text, detected_emotion, mission_source, mission_text, city)
|
| 71 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 72 |
+
''', (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), text, cat, mission['kaynak'], mission['metin'], "Otomatik (IP/Gizli)"))
|
| 73 |
+
conn.commit()
|
| 74 |
+
conn.close()
|
| 75 |
+
|
| 76 |
+
return f"""
|
| 77 |
+
<div style='padding: 25px; border-radius: 15px; background-color: #0f172a; border: 2px solid #10b981; color: white;'>
|
| 78 |
+
<h2 style='color: #10b981; text-align: center;'>🕊️ Manevi Rehberlik Sonucu</h2>
|
| 79 |
+
<p style='color: #94a3b8; text-transform: uppercase; font-size: 0.8em;'>Analiz Edilen Boyut: {cat_title}</p>
|
| 80 |
+
<div style='background: #1e293b; padding: 15px; border-radius: 10px; margin: 15px 0;'>
|
| 81 |
+
<p style='font-size: 1.2em; font-style: italic;'>"{mission['metin']}"</p>
|
| 82 |
+
<p style='text-align: right; color: #10b981;'>— {mission['kaynak']}</p>
|
| 83 |
+
</div>
|
| 84 |
+
<div style='background: #ffff00; color: #000080; padding: 15px; border-radius: 10px;'>
|
| 85 |
+
<strong>🎯 Bugünün Kalp Görevi:</strong><br>{mission.get('rehber_mesaji', 'Ailenizle vakit geçirin.')}
|
| 86 |
+
</div>
|
| 87 |
+
</div>
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
def get_admin_history(password):
|
| 91 |
+
if password != "Enes.13112006": # Özel Şifre Güncellendi
|
| 92 |
+
return pd.DataFrame({"Uyarı": ["Geçersiz Admin Şifresi!"]})
|
| 93 |
+
|
| 94 |
+
conn = sqlite3.connect('faithpath.db')
|
| 95 |
+
df = pd.read_sql_query("SELECT date as Tarih, entry_text as Cümle, detected_emotion as Duygu FROM journal_entries ORDER BY id DESC", conn)
|
| 96 |
+
conn.close()
|
| 97 |
+
return df
|
| 98 |
+
|
| 99 |
+
def get_dashboard_plots():
|
| 100 |
+
conn = sqlite3.connect('faithpath.db')
|
| 101 |
+
df = pd.read_sql_query("SELECT detected_emotion FROM journal_entries", conn)
|
| 102 |
+
conn.close()
|
| 103 |
+
if df.empty: return None
|
| 104 |
+
df['Duygu'] = df['detected_emotion'].str.replace('_', ' ').str.title()
|
| 105 |
+
fig = px.pie(df, names='Duygu', title='Genel Duygu Dağılımı', hole=.3, template='plotly_dark')
|
| 106 |
+
return fig
|
| 107 |
+
|
| 108 |
+
# --- GRADIO INTERFACE ---
|
| 109 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {{background-color: #0f172a}}") as demo:
|
| 110 |
+
gr.Markdown("# 🕊️ FaithPath v5.7 Admin-Ready")
|
| 111 |
+
|
| 112 |
+
with gr.Tabs():
|
| 113 |
+
with gr.Tab("🏠 Asistan"):
|
| 114 |
+
txt_input = gr.Textbox(label="Bugün ailenizle ilgili neler yaşadınız?", lines=6)
|
| 115 |
+
submit_btn = gr.Button("Analiz Et", variant="primary")
|
| 116 |
+
html_out = gr.HTML()
|
| 117 |
+
submit_btn.click(process_entry_no_city, txt_input, html_out)
|
| 118 |
+
|
| 119 |
+
with gr.Tab("📊 Dashboard"):
|
| 120 |
+
plot_out = gr.Plot()
|
| 121 |
+
refresh_btn = gr.Button("Grafiği Güncelle")
|
| 122 |
+
refresh_btn.click(get_dashboard_plots, outputs=plot_out)
|
| 123 |
+
demo.load(get_dashboard_plots, outputs=plot_out)
|
| 124 |
+
|
| 125 |
+
with gr.Tab("🔑 Admin"):
|
| 126 |
+
gr.Markdown("### Veri Geçmişi (Sadece Yetkili Erişimi)")
|
| 127 |
+
pwd = gr.Textbox(label="Admin Şifresi", type="password")
|
| 128 |
+
history_table = gr.DataFrame()
|
| 129 |
+
view_btn = gr.Button("Kayıtları Listele")
|
| 130 |
+
view_btn.click(get_admin_history, pwd, history_table)
|
| 131 |
+
|
| 132 |
+
demo.launch()
|