Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -878,12 +878,128 @@ def show_current_context() -> str:
|
|
| 878 |
messages = storage.get_messages_by_session(storage.current_session_id)
|
| 879 |
if not messages:
|
| 880 |
return "No messages in the current session."
|
| 881 |
-
# Show up to the last 5 messages for brevity
|
| 882 |
context = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages[-5:]])
|
| 883 |
return f"Current Session Context (Last 5 Messages):\n{context}"
|
| 884 |
except Exception as e:
|
| 885 |
return f"β Error retrieving context: {str(e)}"
|
| 886 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 887 |
|
| 888 |
|
| 889 |
# Define custom CSS for the Gradio interface
|
|
|
|
| 878 |
messages = storage.get_messages_by_session(storage.current_session_id)
|
| 879 |
if not messages:
|
| 880 |
return "No messages in the current session."
|
|
|
|
| 881 |
context = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages[-5:]])
|
| 882 |
return f"Current Session Context (Last 5 Messages):\n{context}"
|
| 883 |
except Exception as e:
|
| 884 |
return f"β Error retrieving context: {str(e)}"
|
| 885 |
|
| 886 |
+
def get_full_history(topic_filter: str = None) -> str:
|
| 887 |
+
"""Display the full chat history, optionally filtered by topic."""
|
| 888 |
+
try:
|
| 889 |
+
filters = {'topic': topic_filter} if topic_filter and topic_filter != "All Topics" else None
|
| 890 |
+
messages = storage.search_messages("", filters)
|
| 891 |
+
if not messages:
|
| 892 |
+
return "No messages found in the history."
|
| 893 |
+
history = "\n".join([f"[{msg['timestamp']}] {msg['role'].capitalize()} (Topic: {msg['topic']}): {msg['content']}" for msg in messages])
|
| 894 |
+
return f"Chat History:\n{history}"
|
| 895 |
+
except Exception as e:
|
| 896 |
+
return f"β Error retrieving history: {str(e)}"
|
| 897 |
+
|
| 898 |
+
def get_chat_summary(topic_filter: str = None) -> str:
|
| 899 |
+
"""Provide a summary of the chat history, optionally filtered by topic."""
|
| 900 |
+
try:
|
| 901 |
+
filters = {'topic': topic_filter} if topic_filter and topic_filter != "All Topics" else None
|
| 902 |
+
messages = storage.search_messages("", filters)
|
| 903 |
+
if not messages:
|
| 904 |
+
return "No messages found for summary."
|
| 905 |
+
|
| 906 |
+
total_messages = len(messages)
|
| 907 |
+
user_messages = len([msg for msg in messages if msg['role'] == 'user'])
|
| 908 |
+
assistant_messages = len([msg for msg in messages if msg['role'] == 'assistant'])
|
| 909 |
+
topics = Counter(msg['topic'] for msg in messages)
|
| 910 |
+
sessions = len(set(msg['session_id'] for msg in messages))
|
| 911 |
+
|
| 912 |
+
summary = (
|
| 913 |
+
f"Chat Summary:\n"
|
| 914 |
+
f"Total Messages: {total_messages}\n"
|
| 915 |
+
f"User Messages: {user_messages}\n"
|
| 916 |
+
f"Assistant Messages: {assistant_messages}\n"
|
| 917 |
+
f"Number of Sessions: {sessions}\n"
|
| 918 |
+
f"Topic Distribution:\n" +
|
| 919 |
+
"\n".join([f" - {topic}: {count}" for topic, count in topics.most_common()])
|
| 920 |
+
)
|
| 921 |
+
return summary
|
| 922 |
+
except Exception as e:
|
| 923 |
+
return f"β Error generating summary: {str(e)}"
|
| 924 |
+
|
| 925 |
+
def clear_all_history() -> Tuple[str, str, str]:
|
| 926 |
+
"""Clear all chat history and analytics data."""
|
| 927 |
+
try:
|
| 928 |
+
storage.clear_history()
|
| 929 |
+
return (
|
| 930 |
+
"β
All chat history and analytics cleared successfully.",
|
| 931 |
+
"", # Clear history_display
|
| 932 |
+
"" # Clear analytics_display
|
| 933 |
+
)
|
| 934 |
+
except Exception as e:
|
| 935 |
+
return (
|
| 936 |
+
f"β Error clearing history: {str(e)}",
|
| 937 |
+
history_display.value if 'history_display' in globals() else "",
|
| 938 |
+
analytics_display.value if 'analytics_display' in globals() else ""
|
| 939 |
+
)
|
| 940 |
+
|
| 941 |
+
def get_analytics_report() -> str:
|
| 942 |
+
"""Generate a detailed analytics report using SmartAnalyzer."""
|
| 943 |
+
try:
|
| 944 |
+
patterns = analyzer.get_conversation_patterns()
|
| 945 |
+
mood_trends = analyzer.get_mood_trends()
|
| 946 |
+
productivity = analyzer.get_productivity_insights()
|
| 947 |
+
|
| 948 |
+
if "error" in patterns or "error" in mood_trends or "error" in productivity:
|
| 949 |
+
return (
|
| 950 |
+
f"β Error in analytics:\n"
|
| 951 |
+
f"Patterns: {patterns.get('error', 'OK')}\n"
|
| 952 |
+
f"Mood Trends: {mood_trends.get('error', 'OK')}\n"
|
| 953 |
+
f"Productivity: {productivity.get('error', 'OK')}"
|
| 954 |
+
)
|
| 955 |
+
|
| 956 |
+
report = (
|
| 957 |
+
"π Analytics Report\n\n"
|
| 958 |
+
"=== Conversation Patterns ===\n"
|
| 959 |
+
f"Total Messages: {patterns['total_messages']}\n"
|
| 960 |
+
f"User Messages: {patterns['user_messages']}\n"
|
| 961 |
+
f"Messages by Role: {patterns['by_role']}\n"
|
| 962 |
+
f"Top Topics: {patterns['by_topic']}\n"
|
| 963 |
+
f"Peak Hours: {patterns['peak_hours']}\n"
|
| 964 |
+
f"Peak Days: {patterns['peak_days']}\n"
|
| 965 |
+
f"Common Words: {patterns['common_words']}\n"
|
| 966 |
+
f"Average Words per Message: {patterns['avg_words_per_message']}\n"
|
| 967 |
+
f"Date Range: {patterns['date_range']['start']} to {patterns['date_range']['end']}\n\n"
|
| 968 |
+
"=== Mood Trends ===\n"
|
| 969 |
+
f"Overall Sentiment: {dict(mood_trends['overall_sentiment'])}\n"
|
| 970 |
+
f"Sentiment by Topic: {mood_trends['sentiment_by_topic']}\n"
|
| 971 |
+
f"Daily Mood Summary:\n" +
|
| 972 |
+
"\n".join([f" - {date}: {data['dominant']} ({data['distribution']})"
|
| 973 |
+
for date, data in mood_trends['daily_moods'].items()]) + "\n\n"
|
| 974 |
+
"=== Productivity Insights ===\n"
|
| 975 |
+
f"Total Sessions: {productivity['total_sessions']}\n"
|
| 976 |
+
f"Average Messages per Session: {round(productivity['avg_messages_per_session'], 1)}\n"
|
| 977 |
+
f"Most Active Day: {productivity['most_active_day']}\n"
|
| 978 |
+
f"Recent Activity (Last 7 Days): {productivity['recent_activity']} messages\n"
|
| 979 |
+
f"Consistency Score: {productivity['consistency_score']}\n"
|
| 980 |
+
f"Weekly Patterns: {productivity['weekly_patterns']}"
|
| 981 |
+
)
|
| 982 |
+
return report
|
| 983 |
+
except Exception as e:
|
| 984 |
+
return f"β Error generating analytics report: {str(e)}"
|
| 985 |
+
|
| 986 |
+
def export_data() -> str:
|
| 987 |
+
"""Export chat history and analytics data to JSON format."""
|
| 988 |
+
try:
|
| 989 |
+
export_data = {
|
| 990 |
+
"chat_history": storage.chat_history,
|
| 991 |
+
"analytics": storage.analytics,
|
| 992 |
+
"sessions": storage.get_all_sessions()
|
| 993 |
+
}
|
| 994 |
+
export_json = json.dumps(export_data, indent=2, ensure_ascii=False)
|
| 995 |
+
return export_json
|
| 996 |
+
except Exception as e:
|
| 997 |
+
return f"β Error exporting data: {str(e)}"
|
| 998 |
+
|
| 999 |
+
# Initialize storage and components
|
| 1000 |
+
storage = ChatStorage()
|
| 1001 |
+
analyzer = SmartAnalyzer(storage)
|
| 1002 |
+
search_manager = AdvancedSearchManager(storage)
|
| 1003 |
|
| 1004 |
|
| 1005 |
# Define custom CSS for the Gradio interface
|