benjamin5607 commited on
Commit
12a7d13
ยท
verified ยท
1 Parent(s): bc7fbf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -21
app.py CHANGED
@@ -11,7 +11,7 @@ import plotly.express as px
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")
16
  if not api_key:
17
  try: api_key = st.secrets["GROQ_API_KEY"]
@@ -21,6 +21,7 @@ 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 ---
@@ -39,16 +40,17 @@ def fetch_google_news(query, geo="US", lang="en", limit=10):
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)
@@ -66,14 +68,16 @@ with tab_search:
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))
@@ -89,32 +93,85 @@ with tab_search:
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์—์„œ๋Š” ํŠน์ • ๊ตญ๊ฐ€๋ฅผ ํƒ€๊ฒŸํŒ…ํ•˜์—ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ฐ€์ด๋“œ๋ผ์ธ ์œ„๋ฐ˜ ์—ฌ๋ถ€์™€ ์ทจ์•ฝ ๊ณ„์ธต ๋ฆฌ์Šคํฌ๋ฅผ ์‹ฌ์ธต ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค.")
 
 
 
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")
16
  if not api_key:
17
  try: api_key = st.secrets["GROQ_API_KEY"]
 
21
  st.error("๐Ÿ”‘ GROQ_API_KEY Missing in Settings.")
22
  st.stop()
23
 
24
+ # Groq ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
25
  client = Groq(api_key=api_key)
26
 
27
  # --- Monitoring Nations ---
 
40
  return [{"title": e.title, "source": e.source.title if 'source' in e else "G-News"} for e in feed.entries[:limit]]
41
 
42
  st.title("๐Ÿ“ก Omni-Watch: Global Issue Deep-Dive Center")
43
+ st.caption("Strategic Intelligence for Community Guidelines & Risk Mitigation")
44
  st.markdown("---")
45
 
46
  tab_search, tab_report = st.tabs(["๐ŸŒ Global Issue Tracking & Deep Report", "๐Ÿ” National Deep-Dive Analysis"])
47
 
48
+ # --- [Tab 1: Global Issue Tracking & Deep Report] ---
49
  with tab_search:
50
  st.subheader("Global Risk Propagation & Intelligence Analysis")
51
  col_k1, col_k2 = st.columns([3, 1])
52
  with col_k1:
53
+ search_keyword = st.text_input("Enter Issue Keyword to Analyze Globally", "Cyber Attacks")
54
  with col_k2:
55
  st.write("")
56
  run_global = st.button("๐Ÿ—บ๏ธ Global Deep Scan", type="primary", use_container_width=True)
 
68
  if news:
69
  titles = "\n".join([n['title'] for n in news])
70
  all_captured_news.append(f"--- {name} News ---\n{titles}")
71
+
72
+ # ์ˆ˜์ •๋œ ๋ถ€๋ถ„: client.chat.completions.create
73
  try:
74
+ res = client.chat.completions.create(
75
  model="llama-3.1-70b-versatile",
76
+ messages=[{"role": "user", "content": f"Risk score (0-100) for '{search_keyword}' in {name} news: {titles}. Return ONLY the integer."}],
77
  max_tokens=10
78
  )
79
+ score_text = res.choices[0].message.content
80
+ score = int(''.join(filter(str.isdigit, score_text)))
81
  except: score = 50
82
  results.append({"Country": name, "Risk": score})
83
  progress.progress((i + 1) / len(MONITOR_LIST))
 
93
  st.divider()
94
  st.subheader(f"๐Ÿง  Llama 3 Global Intelligence Report: {search_keyword}")
95
 
96
+
97
+
98
  with st.spinner("Synthesizing Global Intelligence..."):
99
  combined_news = "\n\n".join(all_captured_news)
100
  global_system_prompt = f"""
101
+ You are a Global Strategic Intelligence Officer. Analyze the propagation of '{search_keyword}' across multiple countries.
102
+ Your report must be long, professional, and insightful.
103
 
104
+ REQUIREMENTS (Output in English):
105
+ 1. 'Global Narrative': Detailed summary of how this issue is trending (300+ words).
106
+ 2. 'Contagion & Cross-Border Impact': How the issue flows between nations.
107
+ 3. 'Social Vulnerability Map': Identify risks to minors, women, and ethnic groups globally.
108
+ 4. 'Policy Recommendations': High-level moderation and guideline strategies.
109
  """
110
 
111
+ # ์ˆ˜์ •๋œ ๋ถ€๋ถ„: client.chat.completions.create
112
+ global_res = client.chat.completions.create(
113
  model="llama-3.1-70b-versatile",
114
  messages=[{"role": "system", "content": global_system_prompt}, {"role": "user", "content": combined_news}],
115
+ max_tokens=3000,
116
+ temperature=0.3
117
  )
118
  global_report = global_res.choices[0].message.content
119
 
120
  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)
121
+
122
+ # --- [Tab 2: National Deep-Dive Analysis] ---
123
+ with tab_report:
124
+ col_d1, col_d2 = st.columns([1, 4])
125
+ with col_d1:
126
+ st.subheader("Target Selection")
127
+ target_country = st.selectbox("Market", list(MONITOR_LIST.keys()))
128
+ data_vol = st.slider("Intel Depth", 10, 60, 30)
129
+ run_deep = st.button("๐Ÿš€ Analyze Market", use_container_width=True)
130
+
131
+ if run_deep:
132
+ conf = MONITOR_LIST[target_country]
133
+ with st.status(f"Llama 3 Deep-Dive: {target_country}...", expanded=True):
134
+ g_news = fetch_google_news(f"{target_country} social controversy", limit=data_vol//2)
135
+ l_news = fetch_google_news(target_country, geo=conf['geo'], lang=conf['lang'], limit=data_vol//2)
136
+ feed_text = "\n".join([f"- {n['title']}" for n in g_news + l_news])
137
+
138
+
139
+
140
+ deep_system_prompt = f"""
141
+ Identify Community Guideline violations and risks for {target_country}.
142
+ Structure your report as a JSON object:
143
+ {{
144
+ "risk_score": 0-100,
145
+ "executive_report": "300+ word detailed English report",
146
+ "policy_impact": "Details on which guidelines are violated and why",
147
+ "vulnerability_analysis": "Risk assessment for minors, gender, and ethnicity",
148
+ "action_plan": "5 specific tactical steps"
149
+ }}
150
+ """
151
+
152
+ # ์ˆ˜์ •๋œ ๋ถ€๋ถ„: client.chat.completions.create + response_format
153
+ deep_res = client.chat.completions.create(
154
+ model="llama-3.1-70b-versatile",
155
+ messages=[{"role": "system", "content": deep_system_prompt}, {"role": "user", "content": feed_text}],
156
+ response_format={"type": "json_object"}
157
+ )
158
+ data = json.loads(deep_res.choices[0].message.content)
159
+
160
+ # UI ์ถœ๋ ฅ
161
+ st.header(f"๐Ÿ“‘ National Intelligence: {target_country}")
162
+ m1, m2 = st.columns([1, 4])
163
+ m1.metric("Risk Score", f"{data['risk_score']}/100")
164
 
165
+ st.subheader("๐Ÿ“ Executive Summary")
166
+ st.markdown(f"<div style='background-color: #f0f2f6; padding: 20px; border-radius: 10px;'>{data['executive_report']}</div>", unsafe_allow_html=True)
167
+
168
 
169
 
170
+ c_a, c_b = st.columns(2)
171
+ with c_a:
172
+ st.markdown("### ๐Ÿšซ Policy & Vulnerability")
173
+ st.write(data['policy_impact'])
174
+ st.warning(data['vulnerability_analysis'])
175
+ with c_b:
176
+ st.markdown("### ๐Ÿ›ก๏ธ Tactical Action Plan")
177
+ st.success(data['action_plan'])