Update app.py
Browse files
app.py
CHANGED
|
@@ -72,6 +72,64 @@ frame_categories = {
|
|
| 72 |
"Human Rights Advocacy": ["human rights", "violations", "honor killing", "workplace discrimination", "law reform"]
|
| 73 |
}
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def extract_keywords(text):
|
| 76 |
# Initialize RAKE with default NLTK stopwords
|
| 77 |
r = Rake()
|
|
@@ -223,7 +281,8 @@ def create_docx_from_data(extracted_data):
|
|
| 223 |
para = doc.add_paragraph()
|
| 224 |
run = para.add_run(f"**{key}:** {value}")
|
| 225 |
run.font.size = Pt(11)
|
| 226 |
-
|
|
|
|
| 227 |
if "FramesMapping" in data:
|
| 228 |
doc.add_paragraph("Frames:")
|
| 229 |
mapping = data["FramesMapping"]
|
|
@@ -246,10 +305,33 @@ def create_docx_from_data(extracted_data):
|
|
| 246 |
else:
|
| 247 |
value = data.get("Frames", "N/A")
|
| 248 |
doc.add_paragraph(f"**Frames:** {value}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
doc.add_paragraph("\n")
|
| 250 |
return doc
|
| 251 |
|
| 252 |
|
|
|
|
| 253 |
# -------------------------------------------------------------------
|
| 254 |
# Streamlit App UI
|
| 255 |
# -------------------------------------------------------------------
|
|
|
|
| 72 |
"Human Rights Advocacy": ["human rights", "violations", "honor killing", "workplace discrimination", "law reform"]
|
| 73 |
}
|
| 74 |
|
| 75 |
+
def suggest_themes(keywords):
|
| 76 |
+
"""
|
| 77 |
+
Suggest themes based on extracted keywords using a simple mapping.
|
| 78 |
+
You can adjust the mapping dictionary as needed.
|
| 79 |
+
"""
|
| 80 |
+
theme_mapping = {
|
| 81 |
+
"violence": "Conflict",
|
| 82 |
+
"crisis": "Conflict",
|
| 83 |
+
"repression": "Oppression",
|
| 84 |
+
"oppression": "Oppression",
|
| 85 |
+
"freedom": "Empowerment",
|
| 86 |
+
"hope": "Optimism",
|
| 87 |
+
"unity": "Solidarity",
|
| 88 |
+
"progress": "Advancement",
|
| 89 |
+
"justice": "Social Justice",
|
| 90 |
+
"rights": "Social Justice",
|
| 91 |
+
"equality": "Equality",
|
| 92 |
+
"exploitation": "Exploitation",
|
| 93 |
+
"mobilize": "Mobilization",
|
| 94 |
+
"protest": "Activism",
|
| 95 |
+
"environment": "Environmental",
|
| 96 |
+
"climate": "Environmental"
|
| 97 |
+
}
|
| 98 |
+
suggested = set()
|
| 99 |
+
for kw in keywords:
|
| 100 |
+
lower_kw = kw.lower()
|
| 101 |
+
for key, theme in theme_mapping.items():
|
| 102 |
+
if key in lower_kw:
|
| 103 |
+
suggested.add(theme)
|
| 104 |
+
return list(suggested)
|
| 105 |
+
|
| 106 |
+
def suggest_frames(themes):
|
| 107 |
+
"""
|
| 108 |
+
Suggest frames based on the suggested themes.
|
| 109 |
+
Adjust this mapping to reflect the relationship between themes and your framing categories.
|
| 110 |
+
"""
|
| 111 |
+
frame_mapping = {
|
| 112 |
+
"Conflict": "Anti-Extremism & Anti-Violence",
|
| 113 |
+
"Oppression": "Systemic Oppression",
|
| 114 |
+
"Empowerment": "Empowerment & Resistance",
|
| 115 |
+
"Optimism": "Hopeful",
|
| 116 |
+
"Solidarity": "Positive",
|
| 117 |
+
"Advancement": "Informative",
|
| 118 |
+
"Social Justice": "Human Rights & Justice",
|
| 119 |
+
"Equality": "Gender & Patriarchy",
|
| 120 |
+
"Exploitation": "Political & State Accountability",
|
| 121 |
+
"Mobilization": "Grassroots Mobilization",
|
| 122 |
+
"Activism": "Activism & Advocacy",
|
| 123 |
+
"Environmental": "Environmental Crisis & Activism"
|
| 124 |
+
}
|
| 125 |
+
suggested_frames = set()
|
| 126 |
+
for theme in themes:
|
| 127 |
+
for key, frame in frame_mapping.items():
|
| 128 |
+
if key.lower() in theme.lower():
|
| 129 |
+
suggested_frames.add(frame)
|
| 130 |
+
return list(suggested_frames)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
def extract_keywords(text):
|
| 134 |
# Initialize RAKE with default NLTK stopwords
|
| 135 |
r = Rake()
|
|
|
|
| 281 |
para = doc.add_paragraph()
|
| 282 |
run = para.add_run(f"**{key}:** {value}")
|
| 283 |
run.font.size = Pt(11)
|
| 284 |
+
|
| 285 |
+
# Existing code to add the Frames table (if present)
|
| 286 |
if "FramesMapping" in data:
|
| 287 |
doc.add_paragraph("Frames:")
|
| 288 |
mapping = data["FramesMapping"]
|
|
|
|
| 305 |
else:
|
| 306 |
value = data.get("Frames", "N/A")
|
| 307 |
doc.add_paragraph(f"**Frames:** {value}")
|
| 308 |
+
|
| 309 |
+
# --- New: Table for Keywords, Themes, and Frames ---
|
| 310 |
+
# Assume that 'Keywords' is already extracted and stored in data.
|
| 311 |
+
keywords = data.get("Keywords", [])
|
| 312 |
+
# Generate suggested themes and frames from keywords
|
| 313 |
+
themes = suggest_themes(keywords) if keywords else []
|
| 314 |
+
frames_from_themes = suggest_frames(themes) if themes else []
|
| 315 |
+
|
| 316 |
+
# Create a new table with 3 columns: Keywords, Themes, Frames
|
| 317 |
+
doc.add_paragraph("Summary Table:")
|
| 318 |
+
summary_table = doc.add_table(rows=1, cols=3)
|
| 319 |
+
summary_table.style = "Light List Accent 1"
|
| 320 |
+
hdr_cells = summary_table.rows[0].cells
|
| 321 |
+
hdr_cells[0].text = "Keywords"
|
| 322 |
+
hdr_cells[1].text = "Themes"
|
| 323 |
+
hdr_cells[2].text = "Frames"
|
| 324 |
+
|
| 325 |
+
row_cells = summary_table.add_row().cells
|
| 326 |
+
row_cells[0].text = ", ".join(keywords) if keywords else "N/A"
|
| 327 |
+
row_cells[1].text = ", ".join(themes) if themes else "N/A"
|
| 328 |
+
row_cells[2].text = ", ".join(frames_from_themes) if frames_from_themes else "N/A"
|
| 329 |
+
|
| 330 |
doc.add_paragraph("\n")
|
| 331 |
return doc
|
| 332 |
|
| 333 |
|
| 334 |
+
|
| 335 |
# -------------------------------------------------------------------
|
| 336 |
# Streamlit App UI
|
| 337 |
# -------------------------------------------------------------------
|