AAT1 / app.py
ahm14's picture
Update app.py
cd20d74 verified
Raw
History Blame Contribute Delete
4.51 kB
import gradio as gr
import re
import logging
from langdetect import detect
from transformers import pipeline
from docx import Document
import io
from dotenv import load_dotenv
from groq import ChatGroq
# Load environment variables
load_dotenv()
# Initialize logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# Initialize LLM
llm = ChatGroq(temperature=0.5, groq_api_key="GROQ_API_KEY", model_name="llama3-8b-8192")
# Updated tone categories
tone_categories = {
"Emotional": ["urgent", "violence", "disappearances", "forced", "killing", "crisis", "concern"],
"Harsh": ["corrupt", "oppression", "failure", "repression", "exploit", "unjust", "authoritarian"],
"Somber": ["tragedy", "loss", "pain", "sorrow", "mourning", "grief", "devastation"],
"Motivational": ["rise", "resist", "mobilize", "inspire", "courage", "change", "determination"],
"Informative": ["announcement", "event", "scheduled", "update", "details", "protest", "statement"],
"Positive": ["progress", "unity", "hope", "victory", "together", "solidarity", "uplifting"],
"Happy": ["joy", "celebration", "cheer", "success", "smile", "gratitude", "harmony"],
"Angry": ["rage", "injustice", "fury", "resentment", "outrage", "betrayal"],
"Fearful": ["threat", "danger", "terror", "panic", "risk", "warning"],
"Sarcastic": ["brilliant", "great job", "amazing", "what a surprise", "well done", "as expected"],
"Hopeful": ["optimism", "better future", "faith", "confidence", "looking forward"]
}
# Updated frame categories
frame_categories = {
"Human Rights & Justice": ["rights", "law", "justice", "legal", "humanitarian"],
"Political & State Accountability": ["government", "policy", "state", "corruption", "accountability"],
"Gender & Patriarchy": ["gender", "women", "violence", "patriarchy", "equality"],
"Religious Freedom & Persecution": ["religion", "persecution", "minorities", "intolerance", "faith"],
"Grassroots Mobilization": ["activism", "community", "movement", "local", "mobilization"],
"Environmental Crisis & Activism": ["climate", "deforestation", "water", "pollution", "sustainability"],
"Anti-Extremism & Anti-Violence": ["extremism", "violence", "hate speech", "radicalism", "mob attack"],
"Social Inequality & Economic Disparities": ["class privilege", "labor rights", "economic", "discrimination"],
"Activism & Advocacy": ["justice", "rights", "demand", "protest", "march", "campaign", "freedom of speech"],
"Systemic Oppression": ["discrimination", "oppression", "minorities", "marginalized", "exclusion"],
"Intersectionality": ["intersecting", "women", "minorities", "struggles", "multiple oppression"],
"Call to Action": ["join us", "sign petition", "take action", "mobilize", "support movement"],
"Empowerment & Resistance": ["empower", "resist", "challenge", "fight for", "stand up"],
"Climate Justice": ["environment", "climate change", "sustainability", "biodiversity", "pollution"],
"Human Rights Advocacy": ["human rights", "violations", "honor killing", "workplace discrimination", "law reform"]
}
# Detect language
def detect_language(text):
try:
return detect(text)
except Exception as e:
logging.error(f"Error detecting language: {e}")
return "unknown"
# Analyze tone based on predefined categories
def analyze_tone(text):
response = llm.chat(text, system_prompt="Identify the primary tones in this text based on the given tone categories.")
return response
# Extract frames based on predefined categories
def extract_frames(text):
response = llm.chat(text, system_prompt="Identify the primary frames in this text based on the given frame categories.")
return response
# Categorize frame importance
def categorize_frame_importance(text):
response = llm.chat(text, system_prompt="Categorize the identified frames as Major Frame, Significant Frame, or Minor Mention based on their relevance to the text.")
return response
# Gradio Interface
def analyze_text(text):
language = detect_language(text)
tone = analyze_tone(text)
frames = extract_frames(text)
frame_importance = categorize_frame_importance(text)
return f"Language: {language}\nTones: {tone}\nFrames: {frames}\nFrame Importance: {frame_importance}"
demo = gr.Interface(
fn=analyze_text,
inputs="text",
outputs="text",
title="AI-Powered Activism Message Analyzer with Intersectionality"
)
demo.launch()