benjamin5607 commited on
Commit
4e66bcd
Β·
verified Β·
1 Parent(s): 3fc7551

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -110
app.py CHANGED
@@ -1,14 +1,14 @@
1
  import streamlit as st
2
- from duckduckgo_search import DDGS
 
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 (Ultimate)", page_icon="🌍", layout="wide")
12
 
13
  # --- CSS ---
14
  st.markdown("""
@@ -19,42 +19,67 @@ st.markdown("""
19
  </style>
20
  """, unsafe_allow_html=True)
21
 
22
- # --- 🌍 Ultimate Country Database ---
23
- # None으둜 ν‘œμ‹œλœ κ΅­κ°€λŠ” μ „μš© 검색 μ½”λ“œκ°€ μ—†μ–΄ 'Global(wt-wt)'둜 검색 ν›„ ν‚€μ›Œλ“œλ‘œ ν•„ν„°λ§ν•©λ‹ˆλ‹€.
 
24
  GLOBAL_ZONES = {
25
  "🌏 Asia Pacific": {
26
- "South Korea": "kr-kr", "Japan": "jp-jp", "China": "cn-zh", "Taiwan": "tw-tzh",
27
- "India": "in-en", "Indonesia": "id-id", "Vietnam": "vn-vi", "Thailand": "th-th",
28
- "Philippines": "ph-en", "Malaysia": "my-en", "Singapore": "sg-en", "Australia": "au-en",
29
- "New Zealand": "nz-en", "Hong Kong": "hk-tzh", "Pakistan": None, "Bangladesh": None,
30
- "North Korea": None, "Mongolia": None, "Sri Lanka": None, "Myanmar": None,
31
- "Cambodia": None, "Laos": None, "Nepal": None
 
 
 
 
32
  },
33
  "🏰 Europe": {
34
- "United Kingdom": "uk-en", "France": "fr-fr", "Germany": "de-de", "Italy": "it-it",
35
- "Spain": "es-es", "Russia": "ru-ru", "Ukraine": "ua-uk", "Netherlands": "nl-nl",
36
- "Poland": "pl-pl", "Sweden": "se-sv", "Norway": "no-no", "Denmark": "dk-da",
37
- "Finland": "fi-fi", "Switzerland": "ch-de", "Belgium": "be-fr", "Austria": "at-de",
38
- "Ireland": "ie-en", "Portugal": "pt-pt", "Greece": "gr-gr", "Turkey": "tr-tr",
39
- "Czech Republic": "cz-cs", "Hungary": "hu-hu", "Romania": "ro-ro", "Slovakia": "sk-sk",
40
- "Bulgaria": "bg-bg", "Croatia": "hr-hr", "Estonia": "ee-et", "Latvia": "lv-lv",
41
- "Lithuania": "lt-lt", "Slovenia": "sl-sl", "Iceland": "is-is"
42
  },
43
- "πŸ—½ North & South America": {
44
- "USA": "us-en", "Canada": "ca-en", "Mexico": "mx-es", "Brazil": "br-pt",
45
- "Argentina": "ar-es", "Chile": "cl-es", "Colombia": "co-es", "Peru": "pe-es",
46
- "Venezuela": "ve-es", "Ecuador": None, "Bolivia": None, "Paraguay": None,
47
- "Uruguay": None, "Costa Rica": None, "Cuba": None, "Panama": None,
48
- "Guatemala": None, "Honduras": None
49
  },
50
- "πŸ•Œ Middle East & Africa": {
51
- "Israel": "il-en", "Saudi Arabia": "sa-ar", "UAE": "ae-en", "South Africa": "za-en",
52
- "Egypt": None, "Iran": None, "Iraq": None, "Qatar": None, "Kuwait": None,
53
- "Jordan": None, "Lebanon": None, "Oman": None, "Nigeria": None, "Kenya": None,
54
- "Ethiopia": None, "Morocco": None, "Algeria": None, "Ghana": None, "Tanzania": None
 
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("🌍 Global Watch")
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 Nation")
73
  zone = st.selectbox("Region", list(GLOBAL_ZONES.keys()))
74
  country = st.selectbox("Country", list(GLOBAL_ZONES[zone].keys()))
75
- target_region = GLOBAL_ZONES[zone][country] if GLOBAL_ZONES[zone][country] else "wt-wt"
 
 
76
 
77
  st.markdown("---")
78
- st.subheader("βš™οΈ Scan Config")
79
- data_vol = st.slider("Depth (Articles)", 10, 60, 30, 10)
80
- st.info("Human-like stealth scan enabled.")
81
- st.caption("Language: English Output Forced")
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
- with DDGS() as ddgs:
101
- # 1. Global Search
102
- status_text.text(f"πŸ“‘ Scanning Global News for {country}...")
103
- g_gen = ddgs.news(f"{country} crisis", region="wt-wt", safesearch="off", timelimit="d", max_results=split_vol)
104
- for r in g_gen:
105
- r['stream'] = 'Global 🌎'
106
- all_news.append(r)
107
- progress_bar.progress(40)
108
-
109
- # πŸ€– Human Random Delay
110
- sleep_time = random.uniform(2.0, 4.0)
111
- status_text.text(f"β˜• Avoiding bot detection (Waiting {sleep_time:.1f}s)...")
112
- time.sleep(sleep_time)
113
-
114
- # 2. Local Search
115
- l_query = f"{country}" if target_region != "wt-wt" else f"{country} society politics"
116
- status_text.text(f"πŸ“‘ Scanning Local/Deep News ({target_region})...")
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"Scan Error: {e}")
126
  st.stop()
127
 
128
- df = pd.DataFrame(all_news).drop_duplicates(subset=['title'])
129
 
130
  if df.empty:
131
- st.warning("No news found in the last 24h.")
132
  st.stop()
133
 
134
- st.success(f"βœ… Mined {len(df)} articles.")
 
 
 
135
 
136
  # [Step 2] AI Analysis (Force English)
137
- status_text.text(f"🧠 Qwen-72B analyzing risks (English Output)...")
138
-
139
- news_feed = "\n".join([f"- [{row['stream']}] {row['title']}" for i, row in df.iterrows()])
140
-
141
- system_prompt = f"""
142
- You are a Global Risk Analyst. Analyze the {len(df)} news headlines from TODAY ({country}).
143
-
144
- CRITICAL: OUTPUT MUST BE IN ENGLISH ONLY.
145
-
146
- OUTPUT FORMAT (Strict JSON):
147
- {{
148
- "viral_velocity": 0-100 (Integer),
149
- "top_risks": [
150
- {{
151
- "title": "Headline (English)",
152
- "risk_score": 0-100,
153
- "platform": "TikTok/YouTube/Meta",
154
- "violation": "Hate/Violence/Misinfo",
155
- "action": "Action (English)"
 
 
 
 
 
 
 
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
- except Exception as e:
182
- st.error(f"Analysis Failed: {e}")
183
- st.stop()
 
 
 
 
 
 
 
 
 
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 from 100+ countries and Start Scan.")
 
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).")