Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -846,16 +846,22 @@ def cleanup_old_messages(days: int = 30) -> str:
|
|
| 846 |
except Exception as e:
|
| 847 |
return f"β Failed to clean old messages: {str(e)}"
|
| 848 |
|
|
|
|
|
|
|
| 849 |
def get_topics_list() -> List[str]:
|
| 850 |
-
"""Retrieve a list of unique topics from the database."""
|
| 851 |
try:
|
| 852 |
with sqlite3.connect(DATABASE_FILE) as conn:
|
| 853 |
cursor = conn.execute('SELECT DISTINCT topic FROM messages ORDER BY topic')
|
| 854 |
topics = [row[0] for row in cursor.fetchall()]
|
| 855 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 856 |
except sqlite3.Error as e:
|
| 857 |
print(f"β Error retrieving topics: {str(e)}")
|
| 858 |
-
return ["All Topics"]
|
| 859 |
|
| 860 |
def send_message(user_input: str, topic_input: str) -> Tuple[str, str]:
|
| 861 |
"""Handle sending a user message and getting AI response."""
|
|
@@ -996,13 +1002,7 @@ def export_data() -> str:
|
|
| 996 |
except Exception as e:
|
| 997 |
return f"β Error exporting data: {str(e)}"
|
| 998 |
|
| 999 |
-
#
|
| 1000 |
-
storage = ChatStorage()
|
| 1001 |
-
analyzer = SmartAnalyzer(storage)
|
| 1002 |
-
search_manager = AdvancedSearchManager(storage)
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
# Define custom CSS for the Gradio interface
|
| 1006 |
custom_css = """
|
| 1007 |
.response-area {background-color: #f9f9f9; border-radius: 5px; padding: 10px;}
|
| 1008 |
.input-area {border: 1px solid #ccc; border-radius: 5px;}
|
|
@@ -1010,6 +1010,10 @@ custom_css = """
|
|
| 1010 |
.analytics-display {font-family: monospace; background-color: #e8f4f8;}
|
| 1011 |
"""
|
| 1012 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1013 |
|
| 1014 |
# Main Gradio Interface
|
| 1015 |
with gr.Blocks(
|
|
@@ -1045,7 +1049,7 @@ with gr.Blocks(
|
|
| 1045 |
with gr.Column(scale=1):
|
| 1046 |
topic_input = gr.Textbox(
|
| 1047 |
label="π·οΈ Topic",
|
| 1048 |
-
value="journal",
|
| 1049 |
placeholder="e.g., work, personal, ideas"
|
| 1050 |
)
|
| 1051 |
send_btn = gr.Button("π€ Send", variant="primary", size="lg")
|
|
@@ -1145,7 +1149,7 @@ with gr.Blocks(
|
|
| 1145 |
)
|
| 1146 |
|
| 1147 |
topic_filter.change(
|
| 1148 |
-
fn=lambda x: x if x != "All Topics" else None,
|
| 1149 |
inputs=topic_filter,
|
| 1150 |
outputs=topic_filter
|
| 1151 |
)
|
|
|
|
| 846 |
except Exception as e:
|
| 847 |
return f"β Failed to clean old messages: {str(e)}"
|
| 848 |
|
| 849 |
+
# ... all imports, classes (ChatMessage, DatabaseManager, ChatStorage, AdvancedSearchManager, SmartAnalyzer), and other code ...
|
| 850 |
+
|
| 851 |
def get_topics_list() -> List[str]:
|
| 852 |
+
"""Retrieve a list of unique topics from the database, ensuring 'journal' is included."""
|
| 853 |
try:
|
| 854 |
with sqlite3.connect(DATABASE_FILE) as conn:
|
| 855 |
cursor = conn.execute('SELECT DISTINCT topic FROM messages ORDER BY topic')
|
| 856 |
topics = [row[0] for row in cursor.fetchall()]
|
| 857 |
+
if not topics:
|
| 858 |
+
return ["All Topics", "journal"]
|
| 859 |
+
if "journal" not in topics:
|
| 860 |
+
topics.append("journal")
|
| 861 |
+
return ["All Topics"] + sorted(topics)
|
| 862 |
except sqlite3.Error as e:
|
| 863 |
print(f"β Error retrieving topics: {str(e)}")
|
| 864 |
+
return ["All Topics", "journal"]
|
| 865 |
|
| 866 |
def send_message(user_input: str, topic_input: str) -> Tuple[str, str]:
|
| 867 |
"""Handle sending a user message and getting AI response."""
|
|
|
|
| 1002 |
except Exception as e:
|
| 1003 |
return f"β Error exporting data: {str(e)}"
|
| 1004 |
|
| 1005 |
+
# Define custom CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1006 |
custom_css = """
|
| 1007 |
.response-area {background-color: #f9f9f9; border-radius: 5px; padding: 10px;}
|
| 1008 |
.input-area {border: 1px solid #ccc; border-radius: 5px;}
|
|
|
|
| 1010 |
.analytics-display {font-family: monospace; background-color: #e8f4f8;}
|
| 1011 |
"""
|
| 1012 |
|
| 1013 |
+
# Initialize storage and components
|
| 1014 |
+
storage = ChatStorage()
|
| 1015 |
+
analyzer = SmartAnalyzer(storage)
|
| 1016 |
+
search_manager = AdvancedSearchManager(storage)
|
| 1017 |
|
| 1018 |
# Main Gradio Interface
|
| 1019 |
with gr.Blocks(
|
|
|
|
| 1049 |
with gr.Column(scale=1):
|
| 1050 |
topic_input = gr.Textbox(
|
| 1051 |
label="π·οΈ Topic",
|
| 1052 |
+
value=get_topics_list()[1] if len(get_topics_list()) > 1 else "journal",
|
| 1053 |
placeholder="e.g., work, personal, ideas"
|
| 1054 |
)
|
| 1055 |
send_btn = gr.Button("π€ Send", variant="primary", size="lg")
|
|
|
|
| 1149 |
)
|
| 1150 |
|
| 1151 |
topic_filter.change(
|
| 1152 |
+
fn=lambda x: x if x in get_topics_list() and x != "All Topics" else None,
|
| 1153 |
inputs=topic_filter,
|
| 1154 |
outputs=topic_filter
|
| 1155 |
)
|