Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -731,6 +731,13 @@ def get_topics_list() -> List[str]:
|
|
| 731 |
print(f"β Error retrieving topics: {str(e)}")
|
| 732 |
return ["All Topics", "journal"]
|
| 733 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
# TWEAK JAWABAN MODEL
|
| 736 |
def groq_with_memory(message: str, topic: str = "general", retries: int = 3) -> tuple:
|
|
@@ -808,6 +815,85 @@ def groq_with_memory(message: str, topic: str = "general", retries: int = 3) ->
|
|
| 808 |
except Exception as e:
|
| 809 |
return f"β Error: {str(e)}", ""
|
| 810 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
|
| 812 |
def clear_all_history() -> Tuple[str, str, str]:
|
| 813 |
"""Clear all chat history and analytics data."""
|
|
|
|
| 731 |
print(f"β Error retrieving topics: {str(e)}")
|
| 732 |
return ["All Topics", "journal"]
|
| 733 |
|
| 734 |
+
def count_tokens_rough(text: str) -> int:
|
| 735 |
+
try:
|
| 736 |
+
return len(text) // 4
|
| 737 |
+
except Exception as e:
|
| 738 |
+
print(f"β Error counting tokens: {e}")
|
| 739 |
+
return 0
|
| 740 |
+
|
| 741 |
|
| 742 |
# TWEAK JAWABAN MODEL
|
| 743 |
def groq_with_memory(message: str, topic: str = "general", retries: int = 3) -> tuple:
|
|
|
|
| 815 |
except Exception as e:
|
| 816 |
return f"β Error: {str(e)}", ""
|
| 817 |
|
| 818 |
+
def cleanup_old_messages(days: int = 30) -> str:
|
| 819 |
+
try:
|
| 820 |
+
cutoff = (datetime.now() - timedelta(days=days)).isoformat()
|
| 821 |
+
with sqlite3.connect(storage.db.db_path) as conn:
|
| 822 |
+
conn.execute('DELETE FROM messages WHERE timestamp < ?', (cutoff,))
|
| 823 |
+
conn.commit()
|
| 824 |
+
storage.chat_history = [msg for msg in storage.chat_history if msg['timestamp'] >= cutoff]
|
| 825 |
+
storage.save_history()
|
| 826 |
+
return f"β
Cleared messages older than {days} days"
|
| 827 |
+
except Exception as e:
|
| 828 |
+
return f"β Failed to clean old messages: {str(e)}"
|
| 829 |
+
|
| 830 |
+
|
| 831 |
+
def send_message(user_input: str, topic_input: str) -> Tuple[str, str, List[str]]:
|
| 832 |
+
"""Handle sending a user message, getting AI response, and updating topic list."""
|
| 833 |
+
try:
|
| 834 |
+
if not user_input.strip():
|
| 835 |
+
return "β Please enter a message", user_input, get_topics_list()
|
| 836 |
+
if not topic_input.strip():
|
| 837 |
+
return "β Please enter a topic", user_input, get_topics_list()
|
| 838 |
+
|
| 839 |
+
response, error = groq_with_memory(user_input, topic_input)
|
| 840 |
+
if error:
|
| 841 |
+
return f"β {error}", user_input, get_topics_list()
|
| 842 |
+
|
| 843 |
+
return response, "", get_topics_list()
|
| 844 |
+
except Exception as e:
|
| 845 |
+
return f"β Error processing message: {str(e)}", user_input, get_topics_list()
|
| 846 |
+
|
| 847 |
+
def show_current_context() -> str:
|
| 848 |
+
"""Show the current session's conversation context."""
|
| 849 |
+
try:
|
| 850 |
+
messages = storage.get_messages_by_session(storage.current_session_id)
|
| 851 |
+
if not messages:
|
| 852 |
+
return "No messages in the current session."
|
| 853 |
+
context = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages[-5:]])
|
| 854 |
+
return f"Current Session Context (Last 5 Messages):\n{context}"
|
| 855 |
+
except Exception as e:
|
| 856 |
+
return f"β Error retrieving context: {str(e)}"
|
| 857 |
+
|
| 858 |
+
def get_full_history(topic_filter: str = None) -> str:
|
| 859 |
+
"""Display the full chat history, optionally filtered by topic."""
|
| 860 |
+
try:
|
| 861 |
+
filters = {'topic': topic_filter} if topic_filter and topic_filter != "All Topics" else None
|
| 862 |
+
messages = storage.search_messages("", filters)
|
| 863 |
+
if not messages:
|
| 864 |
+
return "No messages found in the history."
|
| 865 |
+
history = "\n".join([f"[{msg['timestamp']}] {msg['role'].capitalize()} (Topic: {msg['topic']}): {msg['content']}" for msg in messages])
|
| 866 |
+
return f"Chat History:\n{history}"
|
| 867 |
+
except Exception as e:
|
| 868 |
+
return f"β Error retrieving history: {str(e)}"
|
| 869 |
+
|
| 870 |
+
def get_chat_summary(topic_filter: str = None) -> str:
|
| 871 |
+
"""Provide a summary of the chat history, optionally filtered by topic."""
|
| 872 |
+
try:
|
| 873 |
+
filters = {'topic': topic_filter} if topic_filter and topic_filter != "All Topics" else None
|
| 874 |
+
messages = storage.search_messages("", filters)
|
| 875 |
+
if not messages:
|
| 876 |
+
return "No messages found for summary."
|
| 877 |
+
|
| 878 |
+
total_messages = len(messages)
|
| 879 |
+
user_messages = len([msg for msg in messages if msg['role'] == 'user'])
|
| 880 |
+
assistant_messages = len([msg for msg in messages if msg['role'] == 'assistant'])
|
| 881 |
+
topics = Counter(msg['topic'] for msg in messages)
|
| 882 |
+
sessions = len(set(msg['session_id'] for msg in messages))
|
| 883 |
+
|
| 884 |
+
summary = (
|
| 885 |
+
f"Chat Summary:\n"
|
| 886 |
+
f"Total Messages: {total_messages}\n"
|
| 887 |
+
f"User Messages: {user_messages}\n"
|
| 888 |
+
f"Assistant Messages: {assistant_messages}\n"
|
| 889 |
+
f"Number of Sessions: {sessions}\n"
|
| 890 |
+
f"Topic Distribution:\n" +
|
| 891 |
+
"\n".join([f" - {topic}: {count}" for topic, count in topics.most_common()])
|
| 892 |
+
)
|
| 893 |
+
return summary
|
| 894 |
+
except Exception as e:
|
| 895 |
+
return f"β Error generating summary: {str(e)}"
|
| 896 |
+
|
| 897 |
|
| 898 |
def clear_all_history() -> Tuple[str, str, str]:
|
| 899 |
"""Clear all chat history and analytics data."""
|