Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,134 +0,0 @@
|
|
| 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 |
-
)
|
| 26 |
-
''')
|
| 27 |
-
conn.commit()
|
| 28 |
-
conn.close()
|
| 29 |
-
|
| 30 |
-
init_db()
|
| 31 |
-
|
| 32 |
-
# --- MODEL & LIBRARY LOADING ---
|
| 33 |
-
print("Model ve kütüphane yükleniyor...")
|
| 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 |
-
# --- INPUT VALIDATION (STRENGTHENED) ---
|
| 43 |
-
def is_garbage_text(text):
|
| 44 |
-
if re.search(r'([a-zA-Z0-9])\1{4,}', text): return True
|
| 45 |
-
if not re.search(r'[aeıioöuüAEIİOÖUÜ]', text) and len(text) > 5: return True
|
| 46 |
-
return False
|
| 47 |
-
|
| 48 |
-
# --- SEMANTIC ENGINE (v5.6 FINAL) ---
|
| 49 |
-
def semantic_bridge_engine(metin):
|
| 50 |
-
text_lower = metin.lower()
|
| 51 |
-
exclusive_logic = {
|
| 52 |
-
"ö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"],
|
| 53 |
-
"şü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"],
|
| 54 |
-
"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şı"],
|
| 55 |
-
"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"]
|
| 56 |
-
}
|
| 57 |
-
for cat, words in exclusive_logic.items():
|
| 58 |
-
if any(w in text_lower for w in words):
|
| 59 |
-
return cat, random.choice(faith_library.get(cat, []))
|
| 60 |
-
|
| 61 |
-
bert_res = sentiment_analyzer(metin)[0]
|
| 62 |
-
cat = "mutluluk_ve_huzur" if bert_res['label'] == 'positive' else "hüzün_ve_keder"
|
| 63 |
-
return cat, random.choice(faith_library.get(cat, []))
|
| 64 |
-
|
| 65 |
-
# --- CORE FUNCTIONS ---
|
| 66 |
-
def process_entry(text):
|
| 67 |
-
clean_text = text.strip()
|
| 68 |
-
if is_garbage_text(clean_text) or len(clean_text) < 10:
|
| 69 |
-
return "<div style='padding:20px; background:#7f1d1d; color:white; border-radius:10px;'><strong>Hata:</strong> Lütfen analiz için daha detaylı ve anlamlı bir cümle yazın.</div>"
|
| 70 |
-
|
| 71 |
-
cat, mission = semantic_bridge_engine(text)
|
| 72 |
-
cat_title = cat.replace('_', ' ').title()
|
| 73 |
-
|
| 74 |
-
conn = sqlite3.connect('faithpath.db')
|
| 75 |
-
cursor = conn.cursor()
|
| 76 |
-
cursor.execute('''
|
| 77 |
-
INSERT INTO journal_entries (date, entry_text, detected_emotion, mission_source, mission_text)
|
| 78 |
-
VALUES (?, ?, ?, ?, ?)
|
| 79 |
-
''', (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), text, cat, mission['kaynak'], mission['metin']))
|
| 80 |
-
conn.commit()
|
| 81 |
-
conn.close()
|
| 82 |
-
|
| 83 |
-
return f"""
|
| 84 |
-
<div style='padding: 25px; border-radius: 15px; background-color: #0f172a; border: 2px solid #10b981; color: white; line-height: 1.6;'>
|
| 85 |
-
<h2 style='color: #10b981; text-align: center; margin-top: 0;'>🕊️ Aile Manevi Rehberlik Asistanı</h2>
|
| 86 |
-
<div style='background-color: #1e293b; padding: 15px; border-radius: 10px; margin-bottom: 15px; border-left: 5px solid #3b82f6;'>
|
| 87 |
-
<p style='color: #94a3b8; font-size: 0.8em; text-transform: uppercase; margin-bottom: 5px;'>Manevi Boyut: {cat_title}</p>
|
| 88 |
-
</div>
|
| 89 |
-
<div style='background-color: #1e293b; padding: 20px; border-radius: 10px; margin-bottom: 15px;'>
|
| 90 |
-
<p style='font-size: 1.25em; font-style: italic; color: #e2e8f0; margin-bottom: 10px;'>"{mission['metin']}"</p>
|
| 91 |
-
<p style='text-align: right; color: #10b981; font-weight: bold; margin: 0;'>— {mission['kaynak']}</p>
|
| 92 |
-
</div>
|
| 93 |
-
<div style='background-color: #1e293b; padding: 20px; border-radius: 10px; border-left: 5px solid #fde047;'>
|
| 94 |
-
<strong style='display: block; margin-bottom: 8px; color: #fde047;'>🎯 Bugünün Kalp Görevi:</strong>
|
| 95 |
-
<p style='margin: 0; font-size: 1.1em;'>{mission.get('rehber_mesaji', 'Ailenizle vakit geçirin.')}</p>
|
| 96 |
-
</div>
|
| 97 |
-
</div>
|
| 98 |
-
"""
|
| 99 |
-
|
| 100 |
-
def get_dashboard_plots():
|
| 101 |
-
conn = sqlite3.connect('faithpath.db')
|
| 102 |
-
df = pd.read_sql_query("SELECT * FROM journal_entries", conn)
|
| 103 |
-
conn.close()
|
| 104 |
-
if df.empty: return None, None
|
| 105 |
-
df['Duygu'] = df['detected_emotion'].str.replace('_', ' ').str.title()
|
| 106 |
-
fig_pie = px.pie(df, names='Duygu', title='Aile Duygu Dağılımı', hole=.3, template='plotly_dark')
|
| 107 |
-
df['date'] = pd.to_datetime(df['date'])
|
| 108 |
-
trend_df = df.groupby(df['date'].dt.date).size().reset_index(name='Kayıt Sayısı')
|
| 109 |
-
fig_trend = px.line(trend_df, x='date', y='Kayıt Sayısı', title='Zaman İçindeki Kayıt Artışı', template='plotly_dark', markers=True)
|
| 110 |
-
return fig_pie, fig_trend
|
| 111 |
-
|
| 112 |
-
# --- GRADIO INTERFACE ---
|
| 113 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {{background-color: #0f172a}}") as demo:
|
| 114 |
-
gr.Markdown("# 🕊️ FaithPath: Aile Manevi Rehberlik & Analiz Sistemi")
|
| 115 |
-
with gr.Tabs():
|
| 116 |
-
with gr.Tab("🕊️ Rehberlik Asistanı"):
|
| 117 |
-
with gr.Row():
|
| 118 |
-
with gr.Column(scale=1):
|
| 119 |
-
txt_input = gr.Textbox(label="Günlük Notunuz", placeholder="Bugün ailenizle neler yaşadınız?...", lines=10)
|
| 120 |
-
submit_btn = gr.Button("Analiz Et ve Rehberlik Al", variant="primary")
|
| 121 |
-
with gr.Column(scale=1):
|
| 122 |
-
html_out = gr.HTML()
|
| 123 |
-
submit_btn.click(process_entry, inputs=[txt_input], outputs=html_out)
|
| 124 |
-
with gr.Tab("📊 Analiz Paneli"):
|
| 125 |
-
gr.Markdown("### Sistemin Genel Durumu")
|
| 126 |
-
refresh_btn = gr.Button("Verileri Güncelle")
|
| 127 |
-
with gr.Row():
|
| 128 |
-
plot_dist = gr.Plot(label="Duygu Dağılımı")
|
| 129 |
-
plot_trend = gr.Plot(label="Kayıt Trendi")
|
| 130 |
-
refresh_btn.click(get_dashboard_plots, outputs=[plot_dist, plot_trend])
|
| 131 |
-
demo.load(get_dashboard_plots, outputs=[plot_dist, plot_trend])
|
| 132 |
-
gr.Markdown("---")
|
| 133 |
-
gr.Markdown("<center>Enes YILMAZ - Karabük University | Gelişmiş Semantik Analiz v5.6 Clean</center>")
|
| 134 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|