Update pages/facebook_extractor.py
Browse files- pages/facebook_extractor.py +124 -123
pages/facebook_extractor.py
CHANGED
|
@@ -681,135 +681,136 @@ def main():
|
|
| 681 |
st.session_state.chat_history = []
|
| 682 |
st.rerun()
|
| 683 |
|
| 684 |
-
# Main content
|
| 685 |
-
|
| 686 |
|
| 687 |
-
|
| 688 |
-
st.
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
st.warning("π **Demo Data** - Realistic simulation (Facebook restrictions)")
|
| 698 |
-
else:
|
| 699 |
-
st.success("β
**Real Data** - Successfully extracted")
|
| 700 |
-
|
| 701 |
-
# Show processing mode
|
| 702 |
-
if st.session_state.processing_mode == "simple":
|
| 703 |
-
st.info("π§ **Simple Analysis Mode** - Rule-based processing")
|
| 704 |
-
else:
|
| 705 |
-
st.info("π€ **AI Analysis Mode** - Embedding-based processing")
|
| 706 |
-
|
| 707 |
-
# Metrics
|
| 708 |
-
col1, col2, col3 = st.columns(3)
|
| 709 |
-
with col1:
|
| 710 |
-
st.metric("Content Blocks", len(content_blocks))
|
| 711 |
-
with col2:
|
| 712 |
-
st.metric("Data Source", source.upper())
|
| 713 |
-
with col3:
|
| 714 |
-
st.metric("Analysis Mode", "AI" if st.session_state.processing_mode == "ai" else "Simple")
|
| 715 |
-
|
| 716 |
-
# Page info
|
| 717 |
-
st.subheader("π·οΈ Page Information")
|
| 718 |
-
st.write(f"**Title:** {page_info['title']}")
|
| 719 |
-
st.write(f"**URL Type:** {data['url_type']}")
|
| 720 |
-
st.write(f"**Description:** {page_info.get('description', 'No description')}")
|
| 721 |
-
|
| 722 |
-
if page_info.get('member_count'):
|
| 723 |
-
st.write(f"**Members:** {page_info['member_count']}")
|
| 724 |
-
elif page_info.get('follower_count'):
|
| 725 |
-
st.write(f"**Followers:** {page_info['follower_count']}")
|
| 726 |
-
|
| 727 |
-
st.write(f"**Access:** {page_info.get('access_note', 'Public content')}")
|
| 728 |
-
|
| 729 |
-
# Content samples
|
| 730 |
-
st.subheader("π Content Analysis")
|
| 731 |
-
for i, block in enumerate(content_blocks):
|
| 732 |
-
with st.expander(f"Content {i+1} - {block['content_type']} ({block['word_count']} words)"):
|
| 733 |
-
st.write(block['content'])
|
| 734 |
-
st.caption(f"Public: {block['is_public_content']}")
|
| 735 |
|
|
|
|
|
|
|
|
|
|
| 736 |
else:
|
| 737 |
-
st.info(""
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 758 |
|
| 759 |
-
|
| 760 |
-
st.
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 794 |
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
|
|
|
| 808 |
|
| 809 |
-
|
| 810 |
-
st.
|
| 811 |
-
|
| 812 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
if __name__ == "__main__":
|
| 815 |
main()
|
|
|
|
| 681 |
st.session_state.chat_history = []
|
| 682 |
st.rerun()
|
| 683 |
|
| 684 |
+
# Main content - RESTRUCTURED LAYOUT
|
| 685 |
+
st.header("π Extraction Results")
|
| 686 |
|
| 687 |
+
if st.session_state.facebook_data:
|
| 688 |
+
data = st.session_state.facebook_data
|
| 689 |
+
page_info = data['page_info']
|
| 690 |
+
content_blocks = data['content_blocks']
|
| 691 |
+
source = data.get('source', 'unknown')
|
| 692 |
+
|
| 693 |
+
if source == 'demo':
|
| 694 |
+
st.warning("π **Demo Data** - Realistic simulation (Facebook restrictions)")
|
| 695 |
+
else:
|
| 696 |
+
st.success("β
**Real Data** - Successfully extracted")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
|
| 698 |
+
# Show processing mode
|
| 699 |
+
if st.session_state.processing_mode == "simple":
|
| 700 |
+
st.info("π§ **Simple Analysis Mode** - Rule-based processing")
|
| 701 |
else:
|
| 702 |
+
st.info("π€ **AI Analysis Mode** - Embedding-based processing")
|
| 703 |
+
|
| 704 |
+
# Metrics
|
| 705 |
+
col1, col2, col3 = st.columns(3)
|
| 706 |
+
with col1:
|
| 707 |
+
st.metric("Content Blocks", len(content_blocks))
|
| 708 |
+
with col2:
|
| 709 |
+
st.metric("Data Source", source.upper())
|
| 710 |
+
with col3:
|
| 711 |
+
st.metric("Analysis Mode", "AI" if st.session_state.processing_mode == "ai" else "Simple")
|
| 712 |
+
|
| 713 |
+
# Page info
|
| 714 |
+
st.subheader("π·οΈ Page Information")
|
| 715 |
+
st.write(f"**Title:** {page_info['title']}")
|
| 716 |
+
st.write(f"**URL Type:** {data['url_type']}")
|
| 717 |
+
st.write(f"**Description:** {page_info.get('description', 'No description')}")
|
| 718 |
+
|
| 719 |
+
if page_info.get('member_count'):
|
| 720 |
+
st.write(f"**Members:** {page_info['member_count']}")
|
| 721 |
+
elif page_info.get('follower_count'):
|
| 722 |
+
st.write(f"**Followers:** {page_info['follower_count']}")
|
| 723 |
+
|
| 724 |
+
st.write(f"**Access:** {page_info.get('access_note', 'Public content')}")
|
| 725 |
+
|
| 726 |
+
# Content samples
|
| 727 |
+
st.subheader("π Content Analysis")
|
| 728 |
+
for i, block in enumerate(content_blocks):
|
| 729 |
+
with st.expander(f"Content {i+1} - {block['content_type']} ({block['word_count']} words)"):
|
| 730 |
+
st.write(block['content'])
|
| 731 |
+
st.caption(f"Public: {block['is_public_content']}")
|
| 732 |
|
| 733 |
+
else:
|
| 734 |
+
st.info("""
|
| 735 |
+
## π Facebook Data Extractor
|
| 736 |
+
|
| 737 |
+
**University Project Feature**
|
| 738 |
+
|
| 739 |
+
**How it works:**
|
| 740 |
+
1. Enter any Facebook URL
|
| 741 |
+
2. System tries real data extraction
|
| 742 |
+
3. If blocked, uses **realistic demo data**
|
| 743 |
+
4. Choose between AI or Simple analysis
|
| 744 |
+
|
| 745 |
+
**Analysis Modes:**
|
| 746 |
+
- π€ **AI Analysis**: Uses embeddings and Mistral AI
|
| 747 |
+
- π§ **Simple Analysis**: Rule-based (works without embeddings)
|
| 748 |
+
|
| 749 |
+
**Perfect for demonstrating:**
|
| 750 |
+
- Social media data extraction concepts
|
| 751 |
+
- AI analysis capabilities
|
| 752 |
+
- Platform integration
|
| 753 |
+
- Error handling strategies
|
| 754 |
+
""")
|
| 755 |
+
|
| 756 |
+
# Chat section - SEPARATE from main content
|
| 757 |
+
st.markdown("---")
|
| 758 |
+
st.header("π¬ Analysis Chat")
|
| 759 |
+
|
| 760 |
+
if st.session_state.chatbot and st.session_state.facebook_data:
|
| 761 |
+
# Display chat history
|
| 762 |
+
for chat in st.session_state.chat_history:
|
| 763 |
+
if chat["role"] == "user":
|
| 764 |
+
with st.chat_message("user"):
|
| 765 |
+
st.write(chat['content'])
|
| 766 |
+
elif chat["role"] == "assistant":
|
| 767 |
+
with st.chat_message("assistant"):
|
| 768 |
+
st.write(chat['content'])
|
| 769 |
+
|
| 770 |
+
# Suggested questions when no history
|
| 771 |
+
if not st.session_state.chat_history:
|
| 772 |
+
st.subheader("π‘ Try asking:")
|
| 773 |
+
suggestions = [
|
| 774 |
+
"What is this Facebook group/page about?",
|
| 775 |
+
"Summarize the main content and purpose",
|
| 776 |
+
"What kind of community is this?",
|
| 777 |
+
"Analyze the engagement and activity level"
|
| 778 |
+
]
|
| 779 |
|
| 780 |
+
cols = st.columns(len(suggestions))
|
| 781 |
+
for i, suggestion in enumerate(suggestions):
|
| 782 |
+
with cols[i]:
|
| 783 |
+
if st.button(suggestion, key=f"fb_suggest_{suggestion}", use_container_width=True):
|
| 784 |
+
st.info(f"Type: '{suggestion}' in the chat below")
|
| 785 |
+
|
| 786 |
+
elif st.session_state.facebook_data:
|
| 787 |
+
st.info("π¬ Start chatting about the Facebook data")
|
| 788 |
+
else:
|
| 789 |
+
st.info("π Extract Facebook data to enable analysis")
|
| 790 |
+
|
| 791 |
+
# CHAT INPUT - AT THE VERY BOTTOM, OUTSIDE ALL CONTAINERS
|
| 792 |
+
if st.session_state.chatbot and st.session_state.facebook_data:
|
| 793 |
+
user_input = st.chat_input("Ask about the Facebook data...")
|
| 794 |
|
| 795 |
+
if user_input:
|
| 796 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
| 797 |
+
|
| 798 |
+
with st.spinner("π€ Analyzing..."):
|
| 799 |
+
try:
|
| 800 |
+
if st.session_state.chatbot == "simple":
|
| 801 |
+
# Use simple analysis
|
| 802 |
+
response = simple_chat_analysis(user_input, st.session_state.facebook_data)
|
| 803 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 804 |
+
else:
|
| 805 |
+
# Use AI chatbot
|
| 806 |
+
response = st.session_state.chatbot.invoke({"question": user_input})
|
| 807 |
+
answer = response.get("answer", "I couldn't generate a response.")
|
| 808 |
+
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
| 809 |
+
st.rerun()
|
| 810 |
+
except Exception as e:
|
| 811 |
+
error_msg = f"Analysis Error: {str(e)}"
|
| 812 |
+
st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
|
| 813 |
+
st.rerun()
|
| 814 |
|
| 815 |
if __name__ == "__main__":
|
| 816 |
main()
|