Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import os
|
|
| 9 |
import plotly.express as px
|
| 10 |
|
| 11 |
# --- Page Config ---
|
| 12 |
-
st.set_page_config(page_title="
|
| 13 |
|
| 14 |
# --- API Key Load ---
|
| 15 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
@@ -18,99 +18,103 @@ if not api_key:
|
|
| 18 |
except: pass
|
| 19 |
|
| 20 |
if not api_key:
|
| 21 |
-
st.error("๐ GROQ_API_KEY Missing.")
|
| 22 |
st.stop()
|
| 23 |
|
| 24 |
client = Groq(api_key=api_key)
|
| 25 |
|
| 26 |
-
# ---
|
| 27 |
MONITOR_LIST = {
|
| 28 |
"South Korea": {"geo": "KR", "lang": "ko"}, "USA": {"geo": "US", "lang": "en"},
|
| 29 |
"Japan": {"geo": "JP", "lang": "ja"}, "UK": {"geo": "GB", "lang": "en"},
|
| 30 |
-
"France": {"geo": "FR", "lang": "fr"}, "
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
-
def fetch_google_news(query, geo="US", lang="en", limit=
|
| 34 |
encoded = urllib.parse.quote(query)
|
| 35 |
url = f"https://news.google.com/rss/search?q={encoded}&hl={lang}&gl={geo}&ceid={geo}:{lang}"
|
| 36 |
feed = feedparser.parse(url)
|
| 37 |
return [{"title": e.title, "source": e.source.title if 'source' in e else "G-News"} for e in feed.entries[:limit]]
|
| 38 |
|
| 39 |
-
|
| 40 |
-
st.title("๐ฆ Llama 3 Strategic Command Center")
|
| 41 |
st.markdown("---")
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
with
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
system_prompt = f"""
|
| 61 |
-
You are a Senior Strategic Risk Analyst. Analyze the news for {target_country}.
|
| 62 |
-
YOUR REPORT MUST BE DETAILED AND WRITTEN IN PROFESSIONAL ENGLISH.
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": feed_text}],
|
| 75 |
-
response_format={"type": "json_object"}
|
| 76 |
-
)
|
| 77 |
-
data = json.loads(res.choices[0].message.content)
|
| 78 |
-
|
| 79 |
-
# --- ๋์๋ณด๋ ์ถ๋ ฅ ---
|
| 80 |
-
st.header(f"๐ Intelligence Report: {target_country}")
|
| 81 |
-
|
| 82 |
-
# 1. ์๋จ ํต์ฌ ์งํ
|
| 83 |
-
k1, k2, k3 = st.columns(3)
|
| 84 |
-
k1.metric("Risk Level", f"{data['risk_score']}/100")
|
| 85 |
-
k2.metric("Market Status", "๐ด CRITICAL" if data['risk_score'] > 70 else "๐ก UNSTABLE")
|
| 86 |
-
k3.metric("Analyst", "Llama 3.1 70B")
|
| 87 |
-
|
| 88 |
-
# 2. ์์ ํ ๋ฉ์ธ ๋ฆฌํฌํธ (์ฌ์ฉ์๋์ด ์ํ์ ๋ถ๋ถ!)
|
| 89 |
-
st.subheader("๐ Executive Summary")
|
| 90 |
-
st.markdown(f"<div style='background-color: #f0f2f6; padding: 20px; border-radius: 10px;'>{data['risk_summary_report']}</div>", unsafe_allow_html=True)
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
# 3. ์์ธ ๋ถ์ ์น์
|
| 95 |
-
col_a, col_b = st.columns(2)
|
| 96 |
-
with col_a:
|
| 97 |
-
st.markdown("### ๐ซ Policy Violation Details")
|
| 98 |
-
st.write(data['policy_violation_detail'])
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
for rec in data['tactical_recommendations']:
|
| 104 |
-
st.write(f"- {rec}")
|
| 105 |
-
else:
|
| 106 |
-
st.write(data['tactical_recommendations'])
|
| 107 |
|
| 108 |
-
|
| 109 |
-
st.
|
| 110 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
st.markdown("### ๐ฐ Evidence Feed")
|
| 113 |
-
st.dataframe(pd.DataFrame(g_news + l_news), use_container_width=True)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import plotly.express as px
|
| 10 |
|
| 11 |
# --- Page Config ---
|
| 12 |
+
st.set_page_config(page_title="Global Issue Intelligence", page_icon="๐ก", layout="wide")
|
| 13 |
|
| 14 |
# --- API Key Load ---
|
| 15 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
|
|
| 18 |
except: pass
|
| 19 |
|
| 20 |
if not api_key:
|
| 21 |
+
st.error("๐ GROQ_API_KEY Missing in Settings.")
|
| 22 |
st.stop()
|
| 23 |
|
| 24 |
client = Groq(api_key=api_key)
|
| 25 |
|
| 26 |
+
# --- Monitoring Nations ---
|
| 27 |
MONITOR_LIST = {
|
| 28 |
"South Korea": {"geo": "KR", "lang": "ko"}, "USA": {"geo": "US", "lang": "en"},
|
| 29 |
"Japan": {"geo": "JP", "lang": "ja"}, "UK": {"geo": "GB", "lang": "en"},
|
| 30 |
+
"France": {"geo": "FR", "lang": "fr"}, "Germany": {"geo": "DE", "lang": "de"},
|
| 31 |
+
"Malaysia": {"geo": "MY", "lang": "en"}, "India": {"geo": "IN", "lang": "en"},
|
| 32 |
+
"Israel": {"geo": "IL", "lang": "he"}, "Brazil": {"geo": "BR", "lang": "pt"}
|
| 33 |
}
|
| 34 |
|
| 35 |
+
def fetch_google_news(query, geo="US", lang="en", limit=10):
|
| 36 |
encoded = urllib.parse.quote(query)
|
| 37 |
url = f"https://news.google.com/rss/search?q={encoded}&hl={lang}&gl={geo}&ceid={geo}:{lang}"
|
| 38 |
feed = feedparser.parse(url)
|
| 39 |
return [{"title": e.title, "source": e.source.title if 'source' in e else "G-News"} for e in feed.entries[:limit]]
|
| 40 |
|
| 41 |
+
st.title("๐ก Omni-Watch: Global Issue Deep-Dive Center")
|
|
|
|
| 42 |
st.markdown("---")
|
| 43 |
|
| 44 |
+
tab_search, tab_report = st.tabs(["๐ Global Issue Tracking & Deep Report", "๐ National Deep-Dive Analysis"])
|
| 45 |
+
|
| 46 |
+
# --- [Tab 1: Global Issue Tracking & Deep Report (์ฌ์ฉ์ ์์ฒญ ๊ฐํ ๋ฒ์ )] ---
|
| 47 |
+
with tab_search:
|
| 48 |
+
st.subheader("Global Risk Propagation & Intelligence Analysis")
|
| 49 |
+
col_k1, col_k2 = st.columns([3, 1])
|
| 50 |
+
with col_k1:
|
| 51 |
+
search_keyword = st.text_input("Enter Issue Keyword to Analyze Globally", "AI Deepfake Security")
|
| 52 |
+
with col_k2:
|
| 53 |
+
st.write("")
|
| 54 |
+
run_global = st.button("๐บ๏ธ Global Deep Scan", type="primary", use_container_width=True)
|
| 55 |
+
|
| 56 |
+
if run_global:
|
| 57 |
+
results = []
|
| 58 |
+
all_captured_news = []
|
| 59 |
+
progress = st.progress(0)
|
| 60 |
+
status_msg = st.empty()
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
for i, (name, conf) in enumerate(MONITOR_LIST.items()):
|
| 63 |
+
status_msg.text(f"Extracting Intelligence from {name}...")
|
| 64 |
+
news = fetch_google_news(f"{name} {search_keyword}", geo=conf['geo'], lang=conf['lang'], limit=3)
|
| 65 |
+
|
| 66 |
+
if news:
|
| 67 |
+
titles = "\n".join([n['title'] for n in news])
|
| 68 |
+
all_captured_news.append(f"--- {name} News ---\n{titles}")
|
| 69 |
+
prompt = f"Risk score (0-100) for '{search_keyword}' in {name} based on headlines. Return ONLY integer."
|
| 70 |
+
try:
|
| 71 |
+
res = client.chat_completions.create(
|
| 72 |
+
model="llama-3.1-70b-versatile",
|
| 73 |
+
messages=[{"role": "user", "content": prompt}],
|
| 74 |
+
max_tokens=10
|
| 75 |
+
)
|
| 76 |
+
score = int(''.join(filter(str.isdigit, res.choices[0].message.content)))
|
| 77 |
+
except: score = 50
|
| 78 |
+
results.append({"Country": name, "Risk": score})
|
| 79 |
+
progress.progress((i + 1) / len(MONITOR_LIST))
|
| 80 |
|
| 81 |
+
status_msg.empty()
|
| 82 |
+
df_global = pd.DataFrame(results).sort_values("Risk", ascending=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# ์๊ฐํ
|
| 85 |
+
fig = px.bar(df_global, x="Country", y="Risk", color="Risk", color_continuous_scale="Reds", title=f"Global Risk Heatmap: {search_keyword}")
|
| 86 |
+
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# [Global Deep-Dive Report Section]
|
| 89 |
+
st.divider()
|
| 90 |
+
st.subheader(f"๐ง Llama 3 Global Intelligence Report: {search_keyword}")
|
| 91 |
+
|
| 92 |
+
with st.spinner("Synthesizing Global Intelligence..."):
|
| 93 |
+
combined_news = "\n\n".join(all_captured_news)
|
| 94 |
+
global_system_prompt = f"""
|
| 95 |
+
You are a Global Strategic Intelligence Officer. Analyze the propagation of '{search_keyword}' across the following countries.
|
| 96 |
+
|
| 97 |
+
REQUIREMENTS:
|
| 98 |
+
1. 'global_narrative': Summarize how this issue is trending globally (300+ words).
|
| 99 |
+
2. 'cross_border_impact': How is this issue flowing from one country to another? Identify hotspots and contagion paths.
|
| 100 |
+
3. 'vulnerability_map': Which specific demographics (minors, women, etc.) are being targeted globally?
|
| 101 |
+
4. 'policy_recommendations': Global-scale moderation strategy.
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
global_res = client.chat_completions.create(
|
| 105 |
+
model="llama-3.1-70b-versatile",
|
| 106 |
+
messages=[{"role": "system", "content": global_system_prompt}, {"role": "user", "content": combined_news}],
|
| 107 |
+
response_format={"type": "json_object"} if False else None # ํ
์คํธ ๋ฆฌํฌํธ๋ฅผ ์ํด None
|
| 108 |
+
)
|
| 109 |
+
global_report = global_res.choices[0].message.content
|
| 110 |
+
|
| 111 |
+
st.markdown(f"<div style='background-color: #e1f5fe; padding: 25px; border-radius: 10px; border-left: 5px solid #0288d1;'>{global_report}</div>", unsafe_allow_html=True)
|
| 112 |
+
|
| 113 |
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# --- [Tab 2: National Deep-Dive Analysis (๊ธฐ์กด ๊ธฐ๋ฅ)] ---
|
| 116 |
+
with tab_report:
|
| 117 |
+
st.subheader("Select a Specific Market for Intensive Policy Analysis")
|
| 118 |
+
# (์ด์ ์ ๊ตญ๊ฐ๋ณ ์ฌ์ธต ๋ฆฌํฌํธ ๋ก์ง๊ณผ ๋์ผํ๊ฒ ์ ์ง)
|
| 119 |
+
# ... ์ฝ๋ ์๋ต (์ด์ ๋ฒ์ ์ Tab 2์ ๋์ผ) ...
|
| 120 |
+
st.info("Tab 2์์๋ ํน์ ๊ตญ๊ฐ๋ฅผ ํ๊ฒํ
ํ์ฌ ์ปค๋ฎค๋ํฐ ๊ฐ์ด๋๋ผ์ธ ์๋ฐ ์ฌ๋ถ์ ์ทจ์ฝ ๊ณ์ธต ๋ฆฌ์คํฌ๋ฅผ ์ฌ์ธต ๋ถ์ํฉ๋๋ค.")
|