Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,16 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import feedparser
|
| 3 |
import urllib.parse
|
| 4 |
-
from
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
import os
|
| 9 |
import plotly.express as px
|
|
|
|
| 10 |
|
| 11 |
# --- Page Config ---
|
| 12 |
-
st.set_page_config(page_title="
|
| 13 |
|
| 14 |
# --- ๐ Global Monitor List ---
|
| 15 |
MONITOR_LIST = {
|
|
@@ -20,91 +21,100 @@ MONITOR_LIST = {
|
|
| 20 |
"Vietnam": {"geo": "VN", "lang": "vi"}, "Ukraine": {"geo": "UA", "lang": "uk"}
|
| 21 |
}
|
| 22 |
|
| 23 |
-
# --- API Load ---
|
| 24 |
-
|
|
|
|
| 25 |
if not api_key:
|
| 26 |
-
try: api_key = st.secrets["
|
| 27 |
except: pass
|
| 28 |
|
|
|
|
| 29 |
def fetch_google_news(query, geo="US", lang="en", limit=10):
|
| 30 |
encoded = urllib.parse.quote(query)
|
| 31 |
url = f"https://news.google.com/rss/search?q={encoded}&hl={lang}&gl={geo}&ceid={geo}:{lang}"
|
| 32 |
feed = feedparser.parse(url)
|
| 33 |
return [{"title": e.title, "source": e.source.title if 'source' in e else "G-News"} for e in feed.entries[:limit]]
|
| 34 |
|
| 35 |
-
client = InferenceClient(api_key=api_key)
|
| 36 |
-
|
| 37 |
# --- UI ---
|
| 38 |
-
st.title("
|
| 39 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
tab_global, tab_deep = st.tabs(["๐ Propagation Tracker", "๐ Policy & Vulnerability Analysis"])
|
| 42 |
|
| 43 |
# --- [Tab 1: Global Keyword Tracker] ---
|
| 44 |
with tab_global:
|
| 45 |
-
search_keyword = st.text_input("Enter Issue Keyword
|
| 46 |
if st.button("๐บ๏ธ Analyze Global Propagation", type="primary"):
|
| 47 |
-
# (๊ธฐ์กด ๊ธ๋ก๋ฒ ์ค์บ ๋ก์ง - ์๊ฐํ ํฌํจ)
|
| 48 |
results = []
|
|
|
|
| 49 |
for name, conf in MONITOR_LIST.items():
|
|
|
|
| 50 |
news = fetch_google_news(f"{name} {search_keyword}", geo=conf['geo'], lang=conf['lang'], limit=5)
|
| 51 |
if news:
|
| 52 |
-
prompt = f"
|
| 53 |
try:
|
| 54 |
-
res = client.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
score = int(''.join(filter(str.isdigit, res.choices[0].message.content)))
|
| 56 |
except: score = 50
|
| 57 |
results.append({"Country": name, "Risk": score})
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
# --- [Tab 2:
|
| 61 |
with tab_deep:
|
| 62 |
col_d1, col_d2 = st.columns([1, 4])
|
| 63 |
with col_d1:
|
| 64 |
-
target_country = st.selectbox("Market
|
| 65 |
data_vol = st.slider("Intel Volume", 10, 60, 30)
|
| 66 |
-
run_deep = st.button("๐
|
| 67 |
|
| 68 |
if run_deep:
|
| 69 |
conf = MONITOR_LIST[target_country]
|
| 70 |
-
with st.status("
|
| 71 |
-
g_news = fetch_google_news(f"{target_country} controversy safety
|
| 72 |
l_news = fetch_google_news(target_country, geo=conf['geo'], lang=conf['lang'], limit=data_vol//2)
|
| 73 |
feed_text = "\n".join([f"- {n['title']}" for n in g_news + l_news])
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
system_prompt = f"""
|
| 78 |
-
You are a
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
1. 'guideline_threats':
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
- Gender: Is there gender-based violence or misogynistic content risk?
|
| 87 |
-
- Race/Ethnicity/Nationality: Is there a risk of ethnic conflict or localized hate?
|
| 88 |
-
3. 'propagation_mechanism': How will this spread? (e.g. coordinated bot attacks, organic viral outrage).
|
| 89 |
-
4. 'mitigation_strategy': What should moderators do? (Keyword blocking, temporary shadowban, fact-check labels).
|
| 90 |
-
5. 'visual_forensics': Describe images or symbols to watch out for.
|
| 91 |
"""
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": feed_text}],
|
| 96 |
-
|
| 97 |
)
|
| 98 |
-
report_data = res.choices[0].message.content
|
| 99 |
-
status.update(label="Analysis Complete", state="complete")
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
st.markdown(f"## ๐ National Stability & Safety Report: {target_country}")
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
st.
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
st.divider()
|
| 110 |
-
st.caption(f"Sources captured from {target_country} local and global streams.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import feedparser
|
| 3 |
import urllib.parse
|
| 4 |
+
from groq import Groq # Groq ์ ์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
import os
|
| 9 |
import plotly.express as px
|
| 10 |
+
import random
|
| 11 |
|
| 12 |
# --- Page Config ---
|
| 13 |
+
st.set_page_config(page_title="Llama3 Risk Intelligence", page_icon="๐ฆ", layout="wide")
|
| 14 |
|
| 15 |
# --- ๐ Global Monitor List ---
|
| 16 |
MONITOR_LIST = {
|
|
|
|
| 21 |
"Vietnam": {"geo": "VN", "lang": "vi"}, "Ukraine": {"geo": "UA", "lang": "uk"}
|
| 22 |
}
|
| 23 |
|
| 24 |
+
# --- 1. API Key Load (Groq) ---
|
| 25 |
+
# ํ๊น
ํ์ด์ค Secrets์ 'GROQ_API_KEY'๋ก ์ ์ฅํด๋์ธ์.
|
| 26 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 27 |
if not api_key:
|
| 28 |
+
try: api_key = st.secrets["GROQ_API_KEY"]
|
| 29 |
except: pass
|
| 30 |
|
| 31 |
+
# --- Helper: News Fetcher ---
|
| 32 |
def fetch_google_news(query, geo="US", lang="en", limit=10):
|
| 33 |
encoded = urllib.parse.quote(query)
|
| 34 |
url = f"https://news.google.com/rss/search?q={encoded}&hl={lang}&gl={geo}&ceid={geo}:{lang}"
|
| 35 |
feed = feedparser.parse(url)
|
| 36 |
return [{"title": e.title, "source": e.source.title if 'source' in e else "G-News"} for e in feed.entries[:limit]]
|
| 37 |
|
|
|
|
|
|
|
| 38 |
# --- UI ---
|
| 39 |
+
st.title("๐ฆ Llama 3: Strategic Risk Intelligence")
|
| 40 |
+
st.caption("Powered by Llama 3.1 70B (via Groq) | Zero-Cost Infrastructure")
|
| 41 |
+
|
| 42 |
+
if not api_key:
|
| 43 |
+
st.error("๐ GROQ_API_KEY not found. Please set it in Settings.")
|
| 44 |
+
st.stop()
|
| 45 |
+
|
| 46 |
+
# Groq ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 47 |
+
client = Groq(api_key=api_key)
|
| 48 |
|
| 49 |
tab_global, tab_deep = st.tabs(["๐ Propagation Tracker", "๐ Policy & Vulnerability Analysis"])
|
| 50 |
|
| 51 |
# --- [Tab 1: Global Keyword Tracker] ---
|
| 52 |
with tab_global:
|
| 53 |
+
search_keyword = st.text_input("Enter Issue Keyword", "Digital Harassment")
|
| 54 |
if st.button("๐บ๏ธ Analyze Global Propagation", type="primary"):
|
|
|
|
| 55 |
results = []
|
| 56 |
+
status_msg = st.empty()
|
| 57 |
for name, conf in MONITOR_LIST.items():
|
| 58 |
+
status_msg.text(f"Scanning {name} for '{search_keyword}'...")
|
| 59 |
news = fetch_google_news(f"{name} {search_keyword}", geo=conf['geo'], lang=conf['lang'], limit=5)
|
| 60 |
if news:
|
| 61 |
+
prompt = f"Assess the risk score (0-100) for '{search_keyword}' in {name} based on the headlines. RETURN ONLY THE INTEGER VALUE.\n{news[0]['title']}"
|
| 62 |
try:
|
| 63 |
+
res = client.chat.completions.create(
|
| 64 |
+
model="llama-3.1-70b-versatile",
|
| 65 |
+
messages=[{"role": "user", "content": prompt}],
|
| 66 |
+
max_tokens=10
|
| 67 |
+
)
|
| 68 |
score = int(''.join(filter(str.isdigit, res.choices[0].message.content)))
|
| 69 |
except: score = 50
|
| 70 |
results.append({"Country": name, "Risk": score})
|
| 71 |
+
status_msg.empty()
|
| 72 |
+
st.plotly_chart(px.bar(pd.DataFrame(results), x="Country", y="Risk", color="Risk", color_continuous_scale="Reds"))
|
| 73 |
|
| 74 |
+
# --- [Tab 2: Deep Intelligence (Llama 3 Logic)] ---
|
| 75 |
with tab_deep:
|
| 76 |
col_d1, col_d2 = st.columns([1, 4])
|
| 77 |
with col_d1:
|
| 78 |
+
target_country = st.selectbox("Market", list(MONITOR_LIST.keys()))
|
| 79 |
data_vol = st.slider("Intel Volume", 10, 60, 30)
|
| 80 |
+
run_deep = st.button("๐ Analyze Situation", use_container_width=True)
|
| 81 |
|
| 82 |
if run_deep:
|
| 83 |
conf = MONITOR_LIST[target_country]
|
| 84 |
+
with st.status(f"Llama 3.1 70B is profiling {target_country}...", expanded=True):
|
| 85 |
+
g_news = fetch_google_news(f"{target_country} controversy safety", limit=data_vol//2)
|
| 86 |
l_news = fetch_google_news(target_country, geo=conf['geo'], lang=conf['lang'], limit=data_vol//2)
|
| 87 |
feed_text = "\n".join([f"- {n['title']}" for n in g_news + l_news])
|
| 88 |
|
|
|
|
|
|
|
| 89 |
system_prompt = f"""
|
| 90 |
+
You are a Strategic Risk Intelligence Officer specializing in Llama 3 Policy Analysis.
|
| 91 |
+
Target Market: {target_country}. OUTPUT MUST BE IN ENGLISH ONLY.
|
| 92 |
|
| 93 |
+
Analyze the following news and provide a JSON report:
|
| 94 |
+
1. 'guideline_threats': Specific violations (Harassment, Dangerous Acts, Hate Speech, etc.) with WHY/HOW.
|
| 95 |
+
2. 'vulnerable_demographics': Risks to Minors, Gender-based issues, or Ethnic targeting.
|
| 96 |
+
3. 'propagation_mechanism': How it spreads (Viral, Bot, etc.)
|
| 97 |
+
4. 'visual_threat': Visual content or symbols to watch out for.
|
| 98 |
+
5. 'mitigation_strategy': Precise action for moderators.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
"""
|
| 100 |
|
| 101 |
+
# Llama 3 ํธ์ถ
|
| 102 |
+
res = client.chat.completions.create(
|
| 103 |
+
model="llama-3.1-70b-versatile",
|
| 104 |
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": feed_text}],
|
| 105 |
+
response_format={"type": "json_object"} # JSON ๋ชจ๋ ๊ฐ์
|
| 106 |
)
|
| 107 |
+
report_data = json.loads(res.choices[0].message.content)
|
|
|
|
| 108 |
|
| 109 |
+
st.markdown(f"## ๐ Llama 3 Strategic Report: {target_country}")
|
|
|
|
| 110 |
|
| 111 |
+
# ์์ธ ๋ฐ์ดํฐ ๋ ๋๋ง
|
| 112 |
+
c1, c2 = st.columns(2)
|
| 113 |
+
with c1:
|
| 114 |
+
st.error(f"**๐ซ Guideline Threats:**\n{report_data.get('guideline_threats')}")
|
| 115 |
+
st.warning(f"**๐ฅ Vulnerable Groups:**\n{report_data.get('vulnerable_demographics')}")
|
| 116 |
+
with c2:
|
| 117 |
+
st.info(f"**โ๏ธ Propagation:** {report_data.get('propagation_mechanism')}")
|
| 118 |
+
st.info(f"**๐ธ Visual Warning:** {report_data.get('visual_threat')}")
|
| 119 |
|
| 120 |
+
st.success(f"**๐ก๏ธ Strategic Mitigation:** {report_data.get('mitigation_strategy')}")
|
|
|
|
|
|