ahm14 commited on
Commit
08ecbc9
·
verified ·
1 Parent(s): dc1177c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -63
app.py CHANGED
@@ -141,14 +141,14 @@ def get_frame_category_mapping(text):
141
 
142
  def format_frame_categories_table(mapping):
143
  """
144
- Returns a markdown-formatted table that displays each frame along with four columns:
145
  Major Focus, Significant Focus, Minor Mention, and Not Applicable.
146
- A tick (✓) is shown only in the column corresponding to the assigned category.
147
  """
148
  header = "| Frame | Major Focus | Significant Focus | Minor Mention | Not Applicable |\n"
149
  header += "| --- | --- | --- | --- | --- |\n"
150
- rows = ""
151
  tick = "✓"
 
152
  for frame, category in mapping.items():
153
  major = tick if category == "Major Focus" else ""
154
  significant = tick if category == "Significant Focus" else ""
@@ -161,7 +161,6 @@ def format_frame_categories_table(mapping):
161
  # Existing functions for file processing
162
  # -------------------------------------------------------------------
163
 
164
- # Extract captions from DOCX
165
  def extract_captions_from_docx(docx_file):
166
  doc = Document(docx_file)
167
  captions = {}
@@ -175,7 +174,6 @@ def extract_captions_from_docx(docx_file):
175
  captions[current_post].append(text)
176
  return {post: " ".join(lines) for post, lines in captions.items() if lines}
177
 
178
- # Extract metadata from Excel file
179
  def extract_metadata_from_excel(excel_file):
180
  try:
181
  df = pd.read_excel(excel_file)
@@ -185,7 +183,6 @@ def extract_metadata_from_excel(excel_file):
185
  logging.error(f"Error processing Excel file: {e}")
186
  return []
187
 
188
- # Merge metadata with generated analysis
189
  def merge_metadata_with_generated_data(generated_data, excel_metadata):
190
  for post_data in excel_metadata:
191
  post_number = f"Post {post_data.get('Post Number', len(generated_data) + 1)}"
@@ -195,55 +192,6 @@ def merge_metadata_with_generated_data(generated_data, excel_metadata):
195
  generated_data[post_number] = post_data
196
  return generated_data
197
 
198
-
199
-
200
- def add_frames_table(doc, mapping):
201
- """
202
- Adds a well-formatted table for the frames mapping into the given Document.
203
- The table has 5 columns with headers: Frame, Major Focus, Significant Focus,
204
- Minor Mention, and Not Applicable.
205
- """
206
- # Create a table with 1 header row and 5 columns.
207
- table = doc.add_table(rows=1, cols=5)
208
- table.style = "Table Grid"
209
-
210
- # Set header cells with bold, centered text.
211
- headers = ["Frame", "Major Focus", "Significant Focus", "Minor Mention", "Not Applicable"]
212
- hdr_cells = table.rows[0].cells
213
- for idx, header in enumerate(headers):
214
- hdr_cells[idx].text = header
215
- for paragraph in hdr_cells[idx].paragraphs:
216
- paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
217
- for run in paragraph.runs:
218
- run.font.bold = True
219
- run.font.size = Pt(11)
220
-
221
- tick = "✓"
222
- # Add a row for each frame.
223
- for frame, category in mapping.items():
224
- row_cells = table.add_row().cells
225
- row_cells[0].text = frame
226
- row_cells[1].text = tick if category == "Major Focus" else ""
227
- row_cells[2].text = tick if category == "Significant Focus" else ""
228
- row_cells[3].text = tick if category == "Minor Mention" else ""
229
- row_cells[4].text = tick if category == "Not Applicable" else ""
230
- # Center-align cells (except the first one, which is left-aligned).
231
- for idx, cell in enumerate(row_cells):
232
- for paragraph in cell.paragraphs:
233
- if idx == 0:
234
- paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT
235
- else:
236
- paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
237
- for run in paragraph.runs:
238
- run.font.size = Pt(11)
239
-
240
- # Optionally, set fixed column widths.
241
- col_widths = [Inches(2), Inches(1), Inches(1), Inches(1), Inches(1)]
242
- for row in table.rows:
243
- for idx, cell in enumerate(row.cells):
244
- cell.width = col_widths[idx]
245
- return table
246
-
247
  def create_docx_from_data(extracted_data):
248
  doc = Document()
249
  for post_number, data in extracted_data.items():
@@ -257,17 +205,33 @@ def create_docx_from_data(extracted_data):
257
  value = data.get(key, "N/A")
258
  if key in ["Tone", "Hashtags"]:
259
  value = ", ".join(value) if isinstance(value, list) else value
260
- para = doc.add_paragraph(f"{key}: {value}")
261
- para.style.font.size = Pt(11)
262
-
263
- # If a FramesMapping exists, add a proper table.
264
  if "FramesMapping" in data:
265
  doc.add_paragraph("Frames:")
266
- add_frames_table(doc, data["FramesMapping"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
  else:
268
- # Fallback: add plain text.
269
- doc.add_paragraph(f"Frames: {data.get('Frames', 'N/A')}")
270
- doc.add_paragraph("")
271
  return doc
272
 
273
 
 
141
 
142
  def format_frame_categories_table(mapping):
143
  """
144
+ Returns a markdown-formatted table displaying each frame with columns:
145
  Major Focus, Significant Focus, Minor Mention, and Not Applicable.
146
+ A tick (✓) marks the assigned category.
147
  """
148
  header = "| Frame | Major Focus | Significant Focus | Minor Mention | Not Applicable |\n"
149
  header += "| --- | --- | --- | --- | --- |\n"
 
150
  tick = "✓"
151
+ rows = ""
152
  for frame, category in mapping.items():
153
  major = tick if category == "Major Focus" else ""
154
  significant = tick if category == "Significant Focus" else ""
 
161
  # Existing functions for file processing
162
  # -------------------------------------------------------------------
163
 
 
164
  def extract_captions_from_docx(docx_file):
165
  doc = Document(docx_file)
166
  captions = {}
 
174
  captions[current_post].append(text)
175
  return {post: " ".join(lines) for post, lines in captions.items() if lines}
176
 
 
177
  def extract_metadata_from_excel(excel_file):
178
  try:
179
  df = pd.read_excel(excel_file)
 
183
  logging.error(f"Error processing Excel file: {e}")
184
  return []
185
 
 
186
  def merge_metadata_with_generated_data(generated_data, excel_metadata):
187
  for post_data in excel_metadata:
188
  post_number = f"Post {post_data.get('Post Number', len(generated_data) + 1)}"
 
192
  generated_data[post_number] = post_data
193
  return generated_data
194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  def create_docx_from_data(extracted_data):
196
  doc = Document()
197
  for post_number, data in extracted_data.items():
 
205
  value = data.get(key, "N/A")
206
  if key in ["Tone", "Hashtags"]:
207
  value = ", ".join(value) if isinstance(value, list) else value
208
+ para = doc.add_paragraph()
209
+ run = para.add_run(f"**{key}:** {value}")
210
+ run.font.size = Pt(11)
211
+ # Add a proper table for Frames if a mapping is available.
212
  if "FramesMapping" in data:
213
  doc.add_paragraph("Frames:")
214
+ mapping = data["FramesMapping"]
215
+ table = doc.add_table(rows=1, cols=5)
216
+ table.style = "Light List Accent 1"
217
+ hdr_cells = table.rows[0].cells
218
+ hdr_cells[0].text = "Frame"
219
+ hdr_cells[1].text = "Major Focus"
220
+ hdr_cells[2].text = "Significant Focus"
221
+ hdr_cells[3].text = "Minor Mention"
222
+ hdr_cells[4].text = "Not Applicable"
223
+ tick = "✓"
224
+ for frame, category in mapping.items():
225
+ row_cells = table.add_row().cells
226
+ row_cells[0].text = frame
227
+ row_cells[1].text = tick if category == "Major Focus" else ""
228
+ row_cells[2].text = tick if category == "Significant Focus" else ""
229
+ row_cells[3].text = tick if category == "Minor Mention" else ""
230
+ row_cells[4].text = tick if category == "Not Applicable" else ""
231
  else:
232
+ value = data.get("Frames", "N/A")
233
+ doc.add_paragraph(f"**Frames:** {value}")
234
+ doc.add_paragraph("\n")
235
  return doc
236
 
237