Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import re
|
| 3 |
import logging
|
|
@@ -122,20 +123,42 @@ def extract_captions_from_docx(docx_file):
|
|
| 122 |
captions[current_post].append(text)
|
| 123 |
return {post: " ".join(lines) for post, lines in captions.items() if lines}
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# Streamlit app
|
| 126 |
st.title("AI-Powered Activism Message Analyzer")
|
| 127 |
|
| 128 |
-
st.write("Enter text or upload a DOCX file for analysis:")
|
| 129 |
|
| 130 |
# Text input
|
| 131 |
input_text = st.text_area("Input Text", height=200)
|
| 132 |
|
| 133 |
-
# File upload
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Initialize output dictionary
|
| 137 |
output_data = {}
|
| 138 |
|
|
|
|
| 139 |
if input_text:
|
| 140 |
output_data["Manual Input"] = {
|
| 141 |
"Full Caption": input_text,
|
|
@@ -146,8 +169,9 @@ if input_text:
|
|
| 146 |
}
|
| 147 |
st.success("Analysis completed for text input.")
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
|
|
|
| 151 |
for caption, text in captions.items():
|
| 152 |
output_data[caption] = {
|
| 153 |
"Full Caption": text,
|
|
@@ -156,7 +180,16 @@ if uploaded_file:
|
|
| 156 |
"Hashtags": extract_hashtags(text),
|
| 157 |
"Frames": extract_frames(text),
|
| 158 |
}
|
| 159 |
-
st.success(f"Analysis completed for {len(captions)} posts.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Display results
|
| 162 |
if output_data:
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
import streamlit as st
|
| 3 |
import re
|
| 4 |
import logging
|
|
|
|
| 123 |
captions[current_post].append(text)
|
| 124 |
return {post: " ".join(lines) for post, lines in captions.items() if lines}
|
| 125 |
|
| 126 |
+
# Extract metadata from Excel file
|
| 127 |
+
def extract_metadata_from_excel(excel_file):
|
| 128 |
+
try:
|
| 129 |
+
df = pd.read_excel(excel_file)
|
| 130 |
+
# Assuming the Excel sheet has columns: 'Post Number', 'Likes', 'Comments', 'Media Type'
|
| 131 |
+
metadata = df.set_index("Post Number").to_dict(orient="index")
|
| 132 |
+
return metadata
|
| 133 |
+
except Exception as e:
|
| 134 |
+
logging.error(f"Error reading Excel file: {e}")
|
| 135 |
+
return {}
|
| 136 |
+
|
| 137 |
+
# Merge metadata from Excel with the generated data
|
| 138 |
+
def merge_metadata_with_generated_data(generated_data, excel_metadata):
|
| 139 |
+
for post, metadata in excel_metadata.items():
|
| 140 |
+
if post in generated_data:
|
| 141 |
+
generated_data[post].update(metadata)
|
| 142 |
+
return generated_data
|
| 143 |
+
|
| 144 |
# Streamlit app
|
| 145 |
st.title("AI-Powered Activism Message Analyzer")
|
| 146 |
|
| 147 |
+
st.write("Enter text or upload a DOCX/Excel file for analysis:")
|
| 148 |
|
| 149 |
# Text input
|
| 150 |
input_text = st.text_area("Input Text", height=200)
|
| 151 |
|
| 152 |
+
# File upload (DOCX)
|
| 153 |
+
uploaded_docx = st.file_uploader("Upload a DOCX file", type=["docx"])
|
| 154 |
+
|
| 155 |
+
# File upload (Excel)
|
| 156 |
+
uploaded_excel = st.file_uploader("Upload an Excel file", type=["xlsx"])
|
| 157 |
|
| 158 |
# Initialize output dictionary
|
| 159 |
output_data = {}
|
| 160 |
|
| 161 |
+
# Process Text Input
|
| 162 |
if input_text:
|
| 163 |
output_data["Manual Input"] = {
|
| 164 |
"Full Caption": input_text,
|
|
|
|
| 169 |
}
|
| 170 |
st.success("Analysis completed for text input.")
|
| 171 |
|
| 172 |
+
# Process DOCX file
|
| 173 |
+
if uploaded_docx:
|
| 174 |
+
captions = extract_captions_from_docx(uploaded_docx)
|
| 175 |
for caption, text in captions.items():
|
| 176 |
output_data[caption] = {
|
| 177 |
"Full Caption": text,
|
|
|
|
| 180 |
"Hashtags": extract_hashtags(text),
|
| 181 |
"Frames": extract_frames(text),
|
| 182 |
}
|
| 183 |
+
st.success(f"Analysis completed for {len(captions)} posts from DOCX.")
|
| 184 |
+
|
| 185 |
+
# Process Excel file
|
| 186 |
+
if uploaded_excel:
|
| 187 |
+
excel_metadata = extract_metadata_from_excel(uploaded_excel)
|
| 188 |
+
st.success(f"Excel metadata extracted with {len(excel_metadata)} posts.")
|
| 189 |
+
|
| 190 |
+
# Merge and display final data
|
| 191 |
+
if uploaded_excel:
|
| 192 |
+
output_data = merge_metadata_with_generated_data(output_data, excel_metadata)
|
| 193 |
|
| 194 |
# Display results
|
| 195 |
if output_data:
|