Skip to content

Commit

Permalink
Refactor message handling to replace viral messages with categorized …
Browse files Browse the repository at this point in the history
…message display; add MessageCategory model and update analysis prompt for improved insights.
  • Loading branch information
zoharbabin committed Dec 26, 2024
1 parent 33aab66 commit 8aadfe8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 46 deletions.
103 changes: 69 additions & 34 deletions gh_static_front/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function displayResults(data) {
displayMemorableMoments(data);
displayHappiestDays(data);
displaySaddestDays(data);
displayViralMessages(data);
displayMessageCategories(data);
displayMediaStats(data);
displaySharedLinks(data);

Expand Down Expand Up @@ -406,39 +406,6 @@ function displaySaddestDays(data) {
document.getElementById('saddest-days').innerHTML = saddestHtml;
}

function displayViralMessages(data) {
if (!data.viral_messages) {
console.log('No viral messages found');
return;
}

const viralHtml = data.viral_messages
.map(msg => {
return `
<div class="p-2 bg-purple-50 rounded hover:bg-purple-100 transition-all duration-200 cursor-pointer"
onclick="createConfetti(event.clientX, event.clientY)">
<div class="flex items-start gap-2">
<div class="flex-grow">
<p class="font-medium text-purple-900 mb-1">${msg.message}</p>
<div class="text-xs text-purple-600 flex items-center gap-2 mb-1">
<span class="inline-flex items-center">💬 ${msg.replies}</span>
<span class="inline-flex items-center">❤️ ${msg.reactions}</span>
</div>
</div>
</div>
<div class="text-sm text-gray-600 border-l-2 border-purple-200 pl-2 mt-1">
${msg.thread.map(reply => `
<p class="text-sm py-0.5 hover:bg-purple-50 rounded">${reply}</p>
`).join('')}
</div>
</div>
`;
})
.join('');

document.getElementById('viral-messages').innerHTML = viralHtml;
}

function displayMediaStats(data) {
if (!data.media_stats) {
console.log('No media stats found');
Expand Down Expand Up @@ -532,4 +499,72 @@ function displaySharedLinks(data) {
.join('');

document.getElementById('shared-links').innerHTML = linksHtml;
}

function displayMessageCategories(data) {
if (!data.message_categories || data.message_categories.length === 0) {
console.log('No message categories found');
return;
}

const categoryColors = {
'celebration': 'from-yellow-50 to-yellow-100',
'business': 'from-blue-50 to-blue-100',
'team': 'from-green-50 to-green-100',
'strategic': 'from-purple-50 to-purple-100',
'knowledge': 'from-red-50 to-red-100'
};

const getGradient = (category) => {
const baseCategory = Object.keys(categoryColors).find(key =>
category.toLowerCase().includes(key.toLowerCase())
);
return categoryColors[baseCategory] || 'from-gray-50 to-gray-100';
};

const categoriesHtml = data.message_categories
.map(category => `
<div class="mb-6">
<div class="bg-gradient-to-r ${getGradient(category.category)} p-6 rounded-lg shadow-md
hover:shadow-lg transition-all duration-300 cursor-pointer"
onclick="createConfetti(event.clientX, event.clientY)">
<div class="flex justify-between items-start mb-4">
<div>
<h3 class="text-xl font-bold text-gray-800">${category.category}</h3>
<p class="text-sm text-gray-600">${category.subcategory}</p>
</div>
<span class="text-sm font-semibold bg-white px-3 py-1 rounded-full shadow">
Impact: ${(category.impact_score * 100).toFixed(0)}%
</span>
</div>
<div class="space-y-2">
${category.messages.map(msg => `
<div class="bg-white bg-opacity-60 p-3 rounded">
<p class="text-gray-800">${msg}</p>
</div>
`).join('')}
</div>
<div class="mt-4 flex flex-wrap gap-2">
${category.participants.map(participant => `
<span class="text-sm bg-white bg-opacity-70 px-2 py-1 rounded">
${participant}
</span>
`).join('')}
</div>
<div class="mt-4 text-sm text-gray-600 italic">
${category.context}
</div>
<div class="mt-2 text-xs text-gray-500">
${category.timestamp}
</div>
</div>
</div>
`)
.join('');

document.getElementById('message-categories').innerHTML = categoriesHtml;
}
9 changes: 6 additions & 3 deletions gh_static_front/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ <h2 class="text-2xl font-bold mb-4 text-blue-600">Most Reflective Days</h2>
</div>
</div>

<!-- Viral Messages -->
<!-- Message Categories -->
<div class="bg-white rounded-lg shadow-md p-6 col-span-1 md:col-span-2">
<h2 class="text-2xl font-bold mb-4">Epic Chat Moments</h2>
<div id="viral-messages" class="space-y-2"></div>
<h2 class="text-2xl font-bold mb-4">Message Categories</h2>
<div id="message-categories" class="grid gap-6">
<!-- Categories will be dynamically inserted here -->
</div>
</div>

<!-- Media Statistics -->
Expand Down Expand Up @@ -162,6 +164,7 @@ <h2 class="text-2xl font-bold mb-4">Holiday Greeting</h2>
<h2 class="text-2xl font-bold mb-4">Your Group's Holiday Poem</h2>
<div id="chat-poem" class="text-center text-lg font-serif p-6 bg-gradient-to-r from-red-50 to-green-50 rounded-lg whitespace-pre-line"></div>
</div>

</div>
</main>

Expand Down
52 changes: 43 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ class MediaStats(BaseModel):
top_media_sharers: List[UserActivity]
most_reacted_media: List[MediaItem]

class MessageCategory(BaseModel):
category: str
subcategory: str
messages: List[str]
context: str
participants: List[str]
impact_score: float
timestamp: str

class ChatSummary(BaseModel):
most_active_users: List[UserActivity]
popular_topics: List[str]
Expand All @@ -175,6 +184,7 @@ class ChatSummary(BaseModel):
shared_links: List[SharedLink]
chat_poem: str
media_stats: MediaStats
message_categories: List[MessageCategory]

def calculate_md5(content: bytes) -> str:
"""Calculate MD5 hash of file content."""
Expand Down Expand Up @@ -585,13 +595,18 @@ async def process_date_group(dates):
batch_results = []

for date in dates:
day_messages = daily_messages[date]
if len(day_messages) > 5:
indices = [0, len(day_messages)//4, len(day_messages)//2,
(3*len(day_messages))//4, len(day_messages)-1]
group_messages.extend([day_messages[i] for i in indices])
# Filter out media messages first
filtered_messages = [
msg for msg in daily_messages[date]
if not MEDIA_PATTERN.search(str(msg))
]

if len(filtered_messages) > 5:
indices = [0, len(filtered_messages)//4, len(filtered_messages)//2,
(3*len(filtered_messages))//4, len(filtered_messages)-1]
group_messages.extend([filtered_messages[i] for i in indices])
else:
group_messages.extend(day_messages)
group_messages.extend(filtered_messages)

if len(group_messages) >= 15:
sentiment = await analyze_sentiment_batch(group_messages)
Expand Down Expand Up @@ -695,11 +710,29 @@ async def analyze_chat(file: UploadFile = File(...)):
remaining = df[~df['message'].isin(samples)].sample(n=min(sample_size - len(samples), len(df)))
samples.extend(remaining['message'].tolist())

prompt = f"""Analyze this WhatsApp chat and provide insights in the following format:
prompt = f"""Analyze this WhatsApp chat and provide comprehensive insights with the following structure:
1. Key topics discussed (max 5)
2. Three most memorable moments
3. A festive holiday greeting based on the chat context
4. Create a comedic rhyming poem (at least 8 lines) that tells a story about the group's memorable moments and inside jokes. Make it festive and entertaining!
5. Categorize messages into meaningful groups by analyzing:
- Type of interaction (celebration, milestone, discussion, etc.)
- Context and significance
- Participant dynamics
- Impact on team/organization
- Cultural significance
For each identified category, provide:
- Category name and subcategory
- Representative messages
- Context and significance
- Involved participants
- Impact score (0.0 to 1.0)
- Timestamp
Don't use predetermined categories - identify natural patterns and groupings that emerge from the content.
Consider message context, participant engagement, long-term significance, and cultural dynamics.
Chat sample: {' '.join(samples)}"""

Expand Down Expand Up @@ -879,7 +912,7 @@ def process_message_threads(messages_df: pd.DataFrame) -> List[ViralMessage]:
word_counts_converted = {k: int(v) for k, v in word_counts.items()}
activity_converted = {k: int(v) for k, v in activity.items()}

# Create summary with properly structured data
# Create summary with properly structured data including message categories
summary = ChatSummary(
most_active_users=[UserActivity(name=k, count=v) for k, v in most_active_converted.items()],
popular_topics=response.popular_topics,
Expand All @@ -894,7 +927,8 @@ def process_message_threads(messages_df: pd.DataFrame) -> List[ViralMessage]:
viral_messages=viral_messages,
shared_links=shared_links,
chat_poem=response.chat_poem,
media_stats=media_stats
media_stats=media_stats,
message_categories=response.message_categories if hasattr(response, 'message_categories') else []
)

analysis_time = time.time() - analysis_start
Expand Down

0 comments on commit 8aadfe8

Please sign in to comment.