Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import pandas as pd
|
| 5 |
import json
|
| 6 |
import time
|
| 7 |
import os
|
| 8 |
-
import random
|
| 9 |
|
| 10 |
# --- Page Config ---
|
| 11 |
-
st.set_page_config(page_title="Global Risk Watch (
|
| 12 |
|
| 13 |
# --- CSS ---
|
| 14 |
st.markdown("""
|
|
@@ -19,42 +19,67 @@ st.markdown("""
|
|
| 19 |
</style>
|
| 20 |
""", unsafe_allow_html=True)
|
| 21 |
|
| 22 |
-
# --- π
|
| 23 |
-
#
|
|
|
|
| 24 |
GLOBAL_ZONES = {
|
| 25 |
"π Asia Pacific": {
|
| 26 |
-
"South Korea": "
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
},
|
| 33 |
"π° Europe": {
|
| 34 |
-
"United Kingdom": "
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
"
|
| 42 |
},
|
| 43 |
-
"π½
|
| 44 |
-
"USA": "
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
},
|
| 50 |
-
"π
|
| 51 |
-
"Israel": "
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
|
|
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# --- 1. API Key ---
|
| 59 |
api_key = os.environ.get("HF_KEY")
|
| 60 |
if not api_key:
|
|
@@ -63,22 +88,24 @@ if not api_key:
|
|
| 63 |
|
| 64 |
# --- Sidebar ---
|
| 65 |
with st.sidebar:
|
| 66 |
-
st.header("
|
| 67 |
if api_key: st.success("π Key Loaded")
|
| 68 |
else:
|
| 69 |
st.warning("β οΈ No Key")
|
| 70 |
api_key = st.text_input("HF Token", type="password")
|
| 71 |
|
| 72 |
-
st.subheader("Target
|
| 73 |
zone = st.selectbox("Region", list(GLOBAL_ZONES.keys()))
|
| 74 |
country = st.selectbox("Country", list(GLOBAL_ZONES[zone].keys()))
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
st.markdown("---")
|
| 78 |
-
st.subheader("βοΈ
|
| 79 |
-
|
| 80 |
-
st.
|
| 81 |
-
st.
|
| 82 |
|
| 83 |
refresh_btn = st.button("π Start Scan", type="primary", use_container_width=True)
|
| 84 |
|
|
@@ -90,97 +117,89 @@ if not api_key: st.stop()
|
|
| 90 |
if refresh_btn:
|
| 91 |
client = InferenceClient(api_key=api_key)
|
| 92 |
|
| 93 |
-
# [Step 1] Stealth Data Collection
|
| 94 |
status_text = st.empty()
|
| 95 |
progress_bar = st.progress(0)
|
| 96 |
all_news = []
|
| 97 |
split_vol = data_vol // 2
|
| 98 |
|
| 99 |
try:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
l_gen = ddgs.news(l_query, region=target_region, safesearch="off", timelimit="d", max_results=split_vol)
|
| 119 |
-
for r in l_gen:
|
| 120 |
-
r['stream'] = 'Local π '
|
| 121 |
-
all_news.append(r)
|
| 122 |
-
progress_bar.progress(80)
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
-
st.error(f"
|
| 126 |
st.stop()
|
| 127 |
|
| 128 |
-
df = pd.DataFrame(all_news)
|
| 129 |
|
| 130 |
if df.empty:
|
| 131 |
-
st.warning("No news found
|
| 132 |
st.stop()
|
| 133 |
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# [Step 2] AI Analysis (Force English)
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
}}
|
| 157 |
-
],
|
| 158 |
-
"deep_report": {{
|
| 159 |
-
"executive_summary": "Summary (English).",
|
| 160 |
-
"local_vs_global": "Contrast (English).",
|
| 161 |
-
"platform_strategy": {{ "Video": "Strategy (English)", "Text": "Strategy (English)" }},
|
| 162 |
-
"critical_keywords": ["Tag1", "Tag2"]
|
| 163 |
}}
|
| 164 |
-
|
| 165 |
-
"""
|
| 166 |
-
|
| 167 |
-
try:
|
| 168 |
-
response = client.chat_completion(
|
| 169 |
-
model="Qwen/Qwen2.5-72B-Instruct",
|
| 170 |
-
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": news_feed}],
|
| 171 |
-
max_tokens=2500, temperature=0.25
|
| 172 |
-
)
|
| 173 |
-
content = response.choices[0].message.content.replace("```json", "").replace("```", "").strip()
|
| 174 |
-
data = json.loads(content)
|
| 175 |
-
|
| 176 |
-
progress_bar.progress(100)
|
| 177 |
-
time.sleep(0.5)
|
| 178 |
-
status_text.empty()
|
| 179 |
-
progress_bar.empty()
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
# --- [UI Render] ---
|
| 186 |
|
|
@@ -216,4 +235,4 @@ if refresh_btn:
|
|
| 216 |
with t4: st.dataframe(df[['stream', 'title', 'source']], use_container_width=True)
|
| 217 |
|
| 218 |
else:
|
| 219 |
-
st.info("π Select
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import feedparser
|
| 3 |
+
import urllib.parse
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
# --- Page Config ---
|
| 11 |
+
st.set_page_config(page_title="Global Risk Watch (Google Engine)", page_icon="π‘", layout="wide")
|
| 12 |
|
| 13 |
# --- CSS ---
|
| 14 |
st.markdown("""
|
|
|
|
| 19 |
</style>
|
| 20 |
""", unsafe_allow_html=True)
|
| 21 |
|
| 22 |
+
# --- π Country & Language Config (Google News) ---
|
| 23 |
+
# Format: "Country": {"code": "CountryCode", "lang": "LanguageCode"}
|
| 24 |
+
# μ: KR (Country), ko (Language)
|
| 25 |
GLOBAL_ZONES = {
|
| 26 |
"π Asia Pacific": {
|
| 27 |
+
"South Korea": {"geo": "KR", "lang": "ko"},
|
| 28 |
+
"Japan": {"geo": "JP", "lang": "ja"},
|
| 29 |
+
"China": {"geo": "CN", "lang": "zh-CN"},
|
| 30 |
+
"India": {"geo": "IN", "lang": "en"},
|
| 31 |
+
"Indonesia": {"geo": "ID", "lang": "id"},
|
| 32 |
+
"Vietnam": {"geo": "VN", "lang": "vi"},
|
| 33 |
+
"Thailand": {"geo": "TH", "lang": "th"},
|
| 34 |
+
"Philippines": {"geo": "PH", "lang": "en"},
|
| 35 |
+
"Malaysia": {"geo": "MY", "lang": "en"}, # λ§λ μ΄μμ μΆκ°
|
| 36 |
+
"Australia": {"geo": "AU", "lang": "en"}
|
| 37 |
},
|
| 38 |
"π° Europe": {
|
| 39 |
+
"United Kingdom": {"geo": "GB", "lang": "en"},
|
| 40 |
+
"France": {"geo": "FR", "lang": "fr"},
|
| 41 |
+
"Germany": {"geo": "DE", "lang": "de"},
|
| 42 |
+
"Italy": {"geo": "IT", "lang": "it"},
|
| 43 |
+
"Spain": {"geo": "ES", "lang": "es"},
|
| 44 |
+
"Russia": {"geo": "RU", "lang": "ru"},
|
| 45 |
+
"Ukraine": {"geo": "UA", "lang": "uk"},
|
| 46 |
+
"Netherlands": {"geo": "NL", "lang": "nl"}
|
| 47 |
},
|
| 48 |
+
"π½ Americas": {
|
| 49 |
+
"USA": {"geo": "US", "lang": "en"},
|
| 50 |
+
"Canada": {"geo": "CA", "lang": "en"},
|
| 51 |
+
"Mexico": {"geo": "MX", "lang": "es"},
|
| 52 |
+
"Brazil": {"geo": "BR", "lang": "pt"},
|
| 53 |
+
"Argentina": {"geo": "AR", "lang": "es"}
|
| 54 |
},
|
| 55 |
+
"π ME & Africa": {
|
| 56 |
+
"Israel": {"geo": "IL", "lang": "he"},
|
| 57 |
+
"Saudi Arabia": {"geo": "SA", "lang": "ar"},
|
| 58 |
+
"UAE": {"geo": "AE", "lang": "ar"},
|
| 59 |
+
"South Africa": {"geo": "ZA", "lang": "en"},
|
| 60 |
+
"Turkey": {"geo": "TR", "lang": "tr"}
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
+
# --- Helper: Google News RSS Fetcher ---
|
| 65 |
+
def fetch_google_news(query, geo="US", lang="en", limit=10):
|
| 66 |
+
# Google News RSS URL Builder
|
| 67 |
+
# hl=Language, gl=Country, ceid=Country:Language
|
| 68 |
+
encoded_query = urllib.parse.quote(query)
|
| 69 |
+
rss_url = f"https://news.google.com/rss/search?q={encoded_query}&hl={lang}&gl={geo}&ceid={geo}:{lang}"
|
| 70 |
+
|
| 71 |
+
feed = feedparser.parse(rss_url)
|
| 72 |
+
articles = []
|
| 73 |
+
|
| 74 |
+
for entry in feed.entries[:limit]:
|
| 75 |
+
articles.append({
|
| 76 |
+
"title": entry.title,
|
| 77 |
+
"link": entry.link,
|
| 78 |
+
"source": entry.source.title if 'source' in entry else "Google News",
|
| 79 |
+
"date": entry.published if 'published' in entry else "Recent"
|
| 80 |
+
})
|
| 81 |
+
return articles
|
| 82 |
+
|
| 83 |
# --- 1. API Key ---
|
| 84 |
api_key = os.environ.get("HF_KEY")
|
| 85 |
if not api_key:
|
|
|
|
| 88 |
|
| 89 |
# --- Sidebar ---
|
| 90 |
with st.sidebar:
|
| 91 |
+
st.header("π‘ Risk Command (G-News)")
|
| 92 |
if api_key: st.success("π Key Loaded")
|
| 93 |
else:
|
| 94 |
st.warning("β οΈ No Key")
|
| 95 |
api_key = st.text_input("HF Token", type="password")
|
| 96 |
|
| 97 |
+
st.subheader("Target")
|
| 98 |
zone = st.selectbox("Region", list(GLOBAL_ZONES.keys()))
|
| 99 |
country = st.selectbox("Country", list(GLOBAL_ZONES[zone].keys()))
|
| 100 |
+
|
| 101 |
+
# Target Config
|
| 102 |
+
target_conf = GLOBAL_ZONES[zone][country]
|
| 103 |
|
| 104 |
st.markdown("---")
|
| 105 |
+
st.subheader("βοΈ Config")
|
| 106 |
+
# κ΅¬κΈ λ΄μ€λ μ°¨λ¨μ΄ κ±°μ μμ΄μ μ¬λΌμ΄λ κΈ°λ³Έκ°μ λμ¬λ λ©λλ€.
|
| 107 |
+
data_vol = st.slider("Data Volume", 10, 60, 30, 10)
|
| 108 |
+
st.info("Engine: Google News RSS (No Rate Limit)")
|
| 109 |
|
| 110 |
refresh_btn = st.button("π Start Scan", type="primary", use_container_width=True)
|
| 111 |
|
|
|
|
| 117 |
if refresh_btn:
|
| 118 |
client = InferenceClient(api_key=api_key)
|
| 119 |
|
|
|
|
| 120 |
status_text = st.empty()
|
| 121 |
progress_bar = st.progress(0)
|
| 122 |
all_news = []
|
| 123 |
split_vol = data_vol // 2
|
| 124 |
|
| 125 |
try:
|
| 126 |
+
# 1. Global Stream (English / US Perspective)
|
| 127 |
+
status_text.text(f"π‘ Fetching Global News (English)...")
|
| 128 |
+
# κ²μμ΄: "Country Name crisis"
|
| 129 |
+
global_articles = fetch_google_news(f"{country} controversy crisis", geo="US", lang="en", limit=split_vol)
|
| 130 |
+
for r in global_articles:
|
| 131 |
+
r['stream'] = 'Global π'
|
| 132 |
+
all_news.append(r)
|
| 133 |
+
progress_bar.progress(50)
|
| 134 |
+
|
| 135 |
+
# 2. Local Stream (Native Language Perspective)
|
| 136 |
+
status_text.text(f"π‘ Fetching Local News ({target_conf['lang']})...")
|
| 137 |
+
# κ²μμ΄: "Country Name" (νμ§ λ΄μ€μμλ κ΅κ°λͺ
μ΄λ μ¬ν μ΄μ ν€μλ)
|
| 138 |
+
# νμ§μ΄ λ΄μ€λ₯Ό κ°μ Έμ€κΈ° μν΄ geo/lang μ€μ λ³κ²½
|
| 139 |
+
local_articles = fetch_google_news(f"{country}", geo=target_conf['geo'], lang=target_conf['lang'], limit=split_vol)
|
| 140 |
+
for r in local_articles:
|
| 141 |
+
r['stream'] = 'Local π '
|
| 142 |
+
all_news.append(r)
|
| 143 |
+
progress_bar.progress(100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
+
st.error(f"News Fetch Error: {e}")
|
| 147 |
st.stop()
|
| 148 |
|
| 149 |
+
df = pd.DataFrame(all_news)
|
| 150 |
|
| 151 |
if df.empty:
|
| 152 |
+
st.warning("No news found. Google News might be empty for this query.")
|
| 153 |
st.stop()
|
| 154 |
|
| 155 |
+
status_text.text(f"β
Mined {len(df)} articles via Google News RSS.")
|
| 156 |
+
time.sleep(1)
|
| 157 |
+
status_text.empty()
|
| 158 |
+
progress_bar.empty()
|
| 159 |
|
| 160 |
# [Step 2] AI Analysis (Force English)
|
| 161 |
+
with st.spinner(f"π§ Qwen-72B analyzing {len(df)} articles..."):
|
| 162 |
+
|
| 163 |
+
news_feed = "\n".join([f"- [{row['stream']}] {row['title']} ({row['source']})" for i, row in df.iterrows()])
|
| 164 |
+
|
| 165 |
+
system_prompt = f"""
|
| 166 |
+
You are a Global Risk Analyst. Analyze the {len(df)} news headlines from TODAY ({country}).
|
| 167 |
+
|
| 168 |
+
CRITICAL: OUTPUT MUST BE IN ENGLISH ONLY. Translate any local news titles in your analysis.
|
| 169 |
+
|
| 170 |
+
OUTPUT FORMAT (Strict JSON):
|
| 171 |
+
{{
|
| 172 |
+
"viral_velocity": 0-100 (Integer),
|
| 173 |
+
"top_risks": [
|
| 174 |
+
{{
|
| 175 |
+
"title": "Headline (English)",
|
| 176 |
+
"risk_score": 0-100,
|
| 177 |
+
"platform": "TikTok/YouTube/Meta",
|
| 178 |
+
"violation": "Hate/Violence/Misinfo",
|
| 179 |
+
"action": "Action (English)"
|
| 180 |
+
}}
|
| 181 |
+
],
|
| 182 |
+
"deep_report": {{
|
| 183 |
+
"executive_summary": "Summary (English).",
|
| 184 |
+
"local_vs_global": "Contrast (English).",
|
| 185 |
+
"platform_strategy": {{ "Video": "Strategy (English)", "Text": "Strategy (English)" }},
|
| 186 |
+
"critical_keywords": ["Tag1", "Tag2"]
|
| 187 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
}}
|
| 189 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
try:
|
| 192 |
+
response = client.chat_completion(
|
| 193 |
+
model="Qwen/Qwen2.5-72B-Instruct",
|
| 194 |
+
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": news_feed}],
|
| 195 |
+
max_tokens=2500, temperature=0.25
|
| 196 |
+
)
|
| 197 |
+
content = response.choices[0].message.content.replace("```json", "").replace("```", "").strip()
|
| 198 |
+
data = json.loads(content)
|
| 199 |
+
|
| 200 |
+
except Exception as e:
|
| 201 |
+
st.error(f"AI Analysis Failed: {e}")
|
| 202 |
+
st.stop()
|
| 203 |
|
| 204 |
# --- [UI Render] ---
|
| 205 |
|
|
|
|
| 235 |
with t4: st.dataframe(df[['stream', 'title', 'source']], use_container_width=True)
|
| 236 |
|
| 237 |
else:
|
| 238 |
+
st.info("π Select Country and Start Scan (Google Engine Active).")
|