Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,143 +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 json
|
| 7 |
-
import os
|
| 8 |
-
from datetime import datetime
|
| 9 |
-
from transformers import pipeline
|
| 10 |
-
|
| 11 |
-
# --- DATABASE INITIALIZATION ---
|
| 12 |
-
def init_db():
|
| 13 |
-
conn = sqlite3.connect('faithpath.db')
|
| 14 |
-
cursor = conn.cursor()
|
| 15 |
-
cursor.execute('''
|
| 16 |
-
CREATE TABLE IF NOT EXISTS journal_entries (
|
| 17 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 18 |
-
date TEXT,
|
| 19 |
-
entry_text TEXT,
|
| 20 |
-
detected_emotion TEXT,
|
| 21 |
-
mission_source TEXT,
|
| 22 |
-
mission_text TEXT,
|
| 23 |
-
city TEXT
|
| 24 |
-
)
|
| 25 |
-
''')
|
| 26 |
-
conn.commit()
|
| 27 |
-
conn.close()
|
| 28 |
-
|
| 29 |
-
init_db()
|
| 30 |
-
|
| 31 |
-
# --- MODEL & LIBRARY LOADING ---
|
| 32 |
-
sentiment_analyzer = pipeline('sentiment-analysis', model='savasy/bert-base-turkish-sentiment-cased')
|
| 33 |
-
|
| 34 |
-
if os.path.exists('faith_library_final_v56.json'):
|
| 35 |
-
with open('faith_library_final_v56.json', 'r', encoding='utf-8') as f:
|
| 36 |
-
faith_library = json.load(f)
|
| 37 |
-
else:
|
| 38 |
-
faith_library = {}
|
| 39 |
-
|
| 40 |
-
# --- CORE ENGINE ---
|
| 41 |
-
def semantic_bridge_engine(metin):
|
| 42 |
-
text_lower = metin.lower()
|
| 43 |
-
exclusive_logic = {
|
| 44 |
-
'ö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'],
|
| 45 |
-
'şü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'],
|
| 46 |
-
'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şı'],
|
| 47 |
-
'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']
|
| 48 |
-
}
|
| 49 |
-
for cat, words in exclusive_logic.items():
|
| 50 |
-
if any(w in text_lower for w in words):
|
| 51 |
-
return cat, random.choice(faith_library.get(cat, []))
|
| 52 |
-
|
| 53 |
-
bert_res = sentiment_analyzer(metin)[0]
|
| 54 |
-
cat = 'mutluluk_ve_huzur' if bert_res['label'] == 'positive' else 'hüzün_ve_keder'
|
| 55 |
-
return cat, random.choice(faith_library.get(cat, []))
|
| 56 |
-
|
| 57 |
-
# --- UI FUNCTIONS ---
|
| 58 |
-
def process_entry_user(text):
|
| 59 |
-
if len(text.strip()) < 10:
|
| 60 |
-
return "<div style='padding:20px; background:#7f1d1d; color:white; border-radius:10px;'>Hata: Analiz için en az bir cümle yazmalısınız.</div>"
|
| 61 |
-
|
| 62 |
-
cat, mission = semantic_bridge_engine(text)
|
| 63 |
-
cat_title = cat.replace('_', ' ').title()
|
| 64 |
-
|
| 65 |
-
conn = sqlite3.connect('faithpath.db')
|
| 66 |
-
cursor = conn.cursor()
|
| 67 |
-
cursor.execute('''
|
| 68 |
-
INSERT INTO journal_entries (date, entry_text, detected_emotion, mission_source, mission_text, city)
|
| 69 |
-
VALUES (?, ?, ?, ?, ?, ?)
|
| 70 |
-
''', (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), text, cat, mission['kaynak'], mission['metin'], 'Web Kullanicisi'))
|
| 71 |
-
conn.commit()
|
| 72 |
-
conn.close()
|
| 73 |
-
|
| 74 |
-
return f"""
|
| 75 |
-
<div style='padding: 25px; border-radius: 15px; background-color: #0f172a; border: 2px solid #10b981; color: white;'>
|
| 76 |
-
<h2 style='color: #10b981; text-align: center;'>FaithPath Manevi Rehberlik</h2>
|
| 77 |
-
<div style='background: #1e293b; padding: 15px; border-radius: 10px; margin: 15px 0;'>
|
| 78 |
-
<p style='font-size: 1.2em; font-style: italic;'>"{mission['metin']}"</p>
|
| 79 |
-
<p style='text-align: right; color: #10b981;'>- {mission['kaynak']}</p>
|
| 80 |
-
</div>
|
| 81 |
-
<div style='background: #064e3b; color: #ecfdf5; padding: 15px; border-radius: 10px; border-left: 5px solid #10b981;'>
|
| 82 |
-
<strong>Bugünün Kalp Görevi:</strong><br>{mission.get('rehber_mesaji', 'Ailenizle vakit geçirin.')}
|
| 83 |
-
</div>
|
| 84 |
-
</div>
|
| 85 |
-
"""
|
| 86 |
-
|
| 87 |
-
# --- ADMIN & ANALYTICS FUNCTIONS ---
|
| 88 |
-
def get_admin_content(password):
|
| 89 |
-
if password != 'Enes.13112006':
|
| 90 |
-
return gr.update(visible=False), None, None, '### Hata: Geçersiz Şifre'
|
| 91 |
-
|
| 92 |
-
conn = sqlite3.connect('faithpath.db')
|
| 93 |
-
df = pd.read_sql_query('SELECT * FROM journal_entries', conn)
|
| 94 |
-
conn.close()
|
| 95 |
-
|
| 96 |
-
if df.empty:
|
| 97 |
-
return gr.update(visible=True), None, None, '### Sistemde henüz veri bulunmuyor.'
|
| 98 |
-
|
| 99 |
-
df['Duygu'] = df['detected_emotion'].str.replace('_', ' ').str.title()
|
| 100 |
-
fig_pie = px.pie(df, names='Duygu', title='Duygu Dağılımı', hole=.3, template='plotly_dark')
|
| 101 |
-
|
| 102 |
-
df['date_only'] = pd.to_datetime(df['date']).dt.date
|
| 103 |
-
trend_data = df.groupby('date_only').size().reset_index(name='Kayıt Sayısı')
|
| 104 |
-
fig_trend = px.line(trend_data, x='date_only', y='Kayıt Sayısı', title='Günlük Kayıt Trendi', template='plotly_dark')
|
| 105 |
-
|
| 106 |
-
# FIXED SYNTAX HERE: Using double quotes for dict key inside f-string single quotes
|
| 107 |
-
last_date = df['date'].iloc[-1]
|
| 108 |
-
stats_msg = f'### Sistem İstatistikleri\n- Toplam Kayıt: {len(df)}\n- Son İşlem: {last_date}'
|
| 109 |
-
|
| 110 |
-
return gr.update(visible=True), fig_pie, fig_trend, stats_msg
|
| 111 |
-
|
| 112 |
-
# --- GRADIO UI ---
|
| 113 |
-
with gr.Blocks(theme=gr.themes.Soft(), css='.gradio-container {background-color: #0f172a}') as demo:
|
| 114 |
-
gr.Markdown('# FaithPath Professional v5.8.3')
|
| 115 |
-
|
| 116 |
-
with gr.Tabs():
|
| 117 |
-
with gr.Tab('Asistan'):
|
| 118 |
-
txt_input = gr.Textbox(label='Bugün ailenizle ilgili neler yaşadınız?', lines=6)
|
| 119 |
-
submit_btn = gr.Button('Analiz Et', variant='primary')
|
| 120 |
-
html_out = gr.HTML()
|
| 121 |
-
submit_btn.click(process_entry_user, txt_input, html_out)
|
| 122 |
-
|
| 123 |
-
with gr.Tab('Yönetici Paneli'):
|
| 124 |
-
admin_pwd = gr.Textbox(label='Şifre', type='password')
|
| 125 |
-
login_btn = gr.Button('Giriş Yap')
|
| 126 |
-
|
| 127 |
-
stats_area = gr.Markdown()
|
| 128 |
-
|
| 129 |
-
with gr.Column(visible=False) as admin_zone:
|
| 130 |
-
with gr.Row():
|
| 131 |
-
plot_pie = gr.Plot(label='Pasta Grafiği')
|
| 132 |
-
plot_trend = gr.Plot(label='Trend Grafiği')
|
| 133 |
-
|
| 134 |
-
with gr.Row():
|
| 135 |
-
export_btn = gr.Button('CSV Aktar')
|
| 136 |
-
reset_btn = gr.Button('Sıfırla', variant='stop')
|
| 137 |
-
export_f = gr.File(label='Dosya')
|
| 138 |
-
|
| 139 |
-
login_btn.click(get_admin_content, admin_pwd, [admin_zone, plot_pie, plot_trend, stats_area])
|
| 140 |
-
|
| 141 |
-
gr.Markdown('<center>Enes YILMAZ - Karabük University</center>')
|
| 142 |
-
|
| 143 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|