ahm14 commited on
Commit
1be98f5
·
verified ·
1 Parent(s): 749417d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
app.py CHANGED
@@ -88,16 +88,30 @@ def extract_tone_fallback(text):
88
  def extract_hashtags(text):
89
  return re.findall(r"#\w+", text)
90
 
91
- # Extract frames using Groq API (or fallback)
92
  def extract_frames(text):
93
  try:
94
- response = llm.chat([{"role": "system", "content": "Classify the following text into relevant activism frames and assign Major, Significant, or Minor focus."},
 
95
  {"role": "user", "content": text}])
96
- return response["choices"][0]["message"]["content"]
97
  except Exception as e:
98
  logging.error(f"Groq API error: {e}")
99
  return extract_frames_fallback(text)
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # Fallback method for frame extraction (with categorization of Major, Significant, Minor)
102
  def extract_frames_fallback(text):
103
  detected_frames = set()
@@ -239,27 +253,21 @@ if uploaded_excel:
239
  tone_filter = st.multiselect("Filter by Tone", options=["Emotional", "Harsh", "Somber", "Motivational", "Informative", "Positive", "Angry", "Fearful", "Sarcastic", "Hopeful"])
240
  frame_filter = st.multiselect("Filter by Frame", options=["Human Rights & Justice", "Political & State Accountability", "Gender & Patriarchy", "Religious Freedom & Persecution", "Grassroots Mobilization", "Environmental Crisis & Activism", "Anti-Extremism & Anti-Violence", "Social Inequality & Economic Disparities", "Activism & Advocacy", "Systemic Oppression", "Intersectionality", "Call to Action", "Empowerment & Resistance", "Climate Justice", "Human Rights Advocacy"])
241
 
242
- # Filtered output
243
- filtered_data = output_data
244
-
245
- # Apply filters
246
- if tone_filter:
247
- filtered_data = {key: value for key, value in filtered_data.items() if any(tone in tone_filter for tone in value["Tone"])}
248
- if frame_filter:
249
- filtered_data = {key: value for key, value in filtered_data.items() if any(frame in frame_filter for frame in value["Frames"])}
250
 
251
- # Display results in collapsible sections for better UI
252
- if filtered_data:
253
- for post_number, data in filtered_data.items():
254
- with st.expander(post_number):
255
- for key, value in data.items():
256
- st.write(f"**{key}:** {value}")
257
 
258
- # Create DOCX file for download
259
  if filtered_data:
260
- doc = create_docx_from_data(filtered_data)
261
  docx_io = io.BytesIO()
262
- doc.save(docx_io)
263
  docx_io.seek(0)
264
  st.download_button(label="Download Analysis as DOCX", data=docx_io, file_name="analysis.docx")
265
  else:
 
88
  def extract_hashtags(text):
89
  return re.findall(r"#\w+", text)
90
 
91
+ # Extract frames using Groq API (with categorization: Major Focus, Significant Focus, Minor Mention)
92
  def extract_frames(text):
93
  try:
94
+ # Prompt Groq to categorize frames and their focus
95
+ response = llm.chat([{"role": "system", "content": "Classify the following text into relevant activism frames and categorize each frame as Major Focus, Significant Focus, or Minor Mention."},
96
  {"role": "user", "content": text}])
97
+ return parse_frames(response["choices"][0]["message"]["content"])
98
  except Exception as e:
99
  logging.error(f"Groq API error: {e}")
100
  return extract_frames_fallback(text)
101
 
102
+ # Function to parse Groq response and categorize frames
103
+ def parse_frames(response_text):
104
+ frame_data = {}
105
+ lines = response_text.splitlines()
106
+ for line in lines:
107
+ if "Major Focus" in line or "Significant Focus" in line or "Minor Mention" in line:
108
+ category = line.split(":")[0].strip()
109
+ frame = line.split(":")[1].strip()
110
+ if category not in frame_data:
111
+ frame_data[category] = []
112
+ frame_data[category].append(frame)
113
+ return frame_data
114
+
115
  # Fallback method for frame extraction (with categorization of Major, Significant, Minor)
116
  def extract_frames_fallback(text):
117
  detected_frames = set()
 
253
  tone_filter = st.multiselect("Filter by Tone", options=["Emotional", "Harsh", "Somber", "Motivational", "Informative", "Positive", "Angry", "Fearful", "Sarcastic", "Hopeful"])
254
  frame_filter = st.multiselect("Filter by Frame", options=["Human Rights & Justice", "Political & State Accountability", "Gender & Patriarchy", "Religious Freedom & Persecution", "Grassroots Mobilization", "Environmental Crisis & Activism", "Anti-Extremism & Anti-Violence", "Social Inequality & Economic Disparities", "Activism & Advocacy", "Systemic Oppression", "Intersectionality", "Call to Action", "Empowerment & Resistance", "Climate Justice", "Human Rights Advocacy"])
255
 
256
+ filtered_data = {post_number: data for post_number, data in output_data.items() if
257
+ (not tone_filter or any(tone in data["Tone"] for tone in tone_filter)) and
258
+ (not frame_filter or any(frame in data["Frames"] for frame in frame_filter))}
 
 
 
 
 
259
 
260
+ # Displaying the posts in collapsible sections
261
+ for post_number, data in filtered_data.items():
262
+ with st.expander(post_number):
263
+ for key, value in data.items():
264
+ st.write(f"**{key}:** {value}")
 
265
 
266
+ # Allow downloading the DOCX file
267
  if filtered_data:
268
+ docx_output = create_docx_from_data(filtered_data)
269
  docx_io = io.BytesIO()
270
+ docx_output.save(docx_io)
271
  docx_io.seek(0)
272
  st.download_button(label="Download Analysis as DOCX", data=docx_io, file_name="analysis.docx")
273
  else: