Update app.py
Browse files
app.py
CHANGED
@@ -68,10 +68,8 @@ def detect_language(text):
|
|
68 |
# Extract tone using Groq API (or fallback method)
|
69 |
def extract_tone(text):
|
70 |
try:
|
71 |
-
response = llm.chat([
|
72 |
-
|
73 |
-
{"role": "user", "content": text}
|
74 |
-
])
|
75 |
return response["choices"][0]["message"]["content"].split(", ")
|
76 |
except Exception as e:
|
77 |
logging.error(f"Groq API error: {e}")
|
@@ -93,23 +91,34 @@ def extract_hashtags(text):
|
|
93 |
# Extract frames using Groq API (or fallback)
|
94 |
def extract_frames(text):
|
95 |
try:
|
96 |
-
response = llm.chat([
|
97 |
-
|
98 |
-
|
99 |
-
])
|
100 |
-
return response["choices"][0]["message"]["content"]
|
101 |
except Exception as e:
|
102 |
logging.error(f"Groq API error: {e}")
|
103 |
return extract_frames_fallback(text)
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
# Fallback method for frame extraction
|
106 |
def extract_frames_fallback(text):
|
107 |
-
detected_frames =
|
108 |
text_lower = text.lower()
|
109 |
for category, keywords in frame_categories.items():
|
110 |
if any(word in text_lower for word in keywords):
|
111 |
-
detected_frames
|
112 |
-
return
|
113 |
|
114 |
# Extract captions from DOCX
|
115 |
def extract_captions_from_docx(docx_file):
|
@@ -125,23 +134,15 @@ def extract_captions_from_docx(docx_file):
|
|
125 |
captions[current_post].append(text)
|
126 |
return {post: " ".join(lines) for post, lines in captions.items() if lines}
|
127 |
|
128 |
-
#
|
129 |
-
def
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
return {}
|
138 |
-
|
139 |
-
# Merge metadata from Excel with the generated data
|
140 |
-
def merge_metadata_with_generated_data(generated_data, excel_metadata):
|
141 |
-
for post, metadata in excel_metadata.items():
|
142 |
-
if post in generated_data:
|
143 |
-
generated_data[post].update(metadata)
|
144 |
-
return generated_data
|
145 |
|
146 |
# Streamlit app
|
147 |
st.title("AI-Powered Activism Message Analyzer")
|
@@ -196,3 +197,16 @@ if uploaded_excel:
|
|
196 |
# Display results
|
197 |
if output_data:
|
198 |
st.write(output_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Extract tone using Groq API (or fallback method)
|
69 |
def extract_tone(text):
|
70 |
try:
|
71 |
+
response = llm.chat([{"role": "system", "content": "Analyze the tone of the following text and provide descriptive tone labels."},
|
72 |
+
{"role": "user", "content": text}])
|
|
|
|
|
73 |
return response["choices"][0]["message"]["content"].split(", ")
|
74 |
except Exception as e:
|
75 |
logging.error(f"Groq API error: {e}")
|
|
|
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 categorize_frame_focus(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 |
+
# Categorize frame focus: Major, Significant, Minor
|
102 |
+
def categorize_frame_focus(frames_text):
|
103 |
+
frame_data = {}
|
104 |
+
frames = frames_text.split(", ")
|
105 |
+
for frame in frames:
|
106 |
+
if "Major" in frame:
|
107 |
+
frame_data[frame] = "Major Focus"
|
108 |
+
elif "Significant" in frame:
|
109 |
+
frame_data[frame] = "Significant Focus"
|
110 |
+
else:
|
111 |
+
frame_data[frame] = "Minor Mention"
|
112 |
+
return frame_data
|
113 |
+
|
114 |
# Fallback method for frame extraction
|
115 |
def extract_frames_fallback(text):
|
116 |
+
detected_frames = {}
|
117 |
text_lower = text.lower()
|
118 |
for category, keywords in frame_categories.items():
|
119 |
if any(word in text_lower for word in keywords):
|
120 |
+
detected_frames[category] = "Minor Mention"
|
121 |
+
return detected_frames
|
122 |
|
123 |
# Extract captions from DOCX
|
124 |
def extract_captions_from_docx(docx_file):
|
|
|
134 |
captions[current_post].append(text)
|
135 |
return {post: " ".join(lines) for post, lines in captions.items() if lines}
|
136 |
|
137 |
+
# Generate DOCX file for download
|
138 |
+
def generate_docx(data):
|
139 |
+
doc = Document()
|
140 |
+
for post, content in data.items():
|
141 |
+
doc.add_heading(post, level=1)
|
142 |
+
for key, value in content.items():
|
143 |
+
doc.add_paragraph(f"{key}: {value}")
|
144 |
+
doc.add_paragraph() # Add space between posts
|
145 |
+
return doc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
# Streamlit app
|
148 |
st.title("AI-Powered Activism Message Analyzer")
|
|
|
197 |
# Display results
|
198 |
if output_data:
|
199 |
st.write(output_data)
|
200 |
+
|
201 |
+
# Generate DOCX for download
|
202 |
+
doc = generate_docx(output_data)
|
203 |
+
doc_io = io.BytesIO()
|
204 |
+
doc.save(doc_io)
|
205 |
+
doc_io.seek(0)
|
206 |
+
|
207 |
+
st.download_button(
|
208 |
+
label="Download Extracted Data",
|
209 |
+
data=doc_io,
|
210 |
+
file_name="extracted_data.docx",
|
211 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
212 |
+
)
|