benjamin5607 commited on
Commit
df3a179
ยท
verified ยท
1 Parent(s): f458139

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -46
app.py CHANGED
@@ -1,15 +1,16 @@
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
  import plotly.express as px
 
10
 
11
  # --- Page Config ---
12
- st.set_page_config(page_title="Strategic Risk Intelligence", page_icon="๐Ÿ•ต๏ธ", layout="wide")
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
- api_key = os.environ.get("HF_KEY")
 
25
  if not api_key:
26
- try: api_key = st.secrets["HF_KEY"]
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("๐Ÿ•ต๏ธ Strategic Intelligence: Policy & Demographics Risk")
39
- st.markdown("---")
 
 
 
 
 
 
 
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 (e.g. 'Deepfake', 'Hate Speech', 'Child Safety')", "Child Safety")
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"Risk score (0-100) for '{search_keyword}' in {name}. Return ONLY integer."
53
  try:
54
- res = client.chat_completion(model="Qwen/Qwen2.5-72B-Instruct", messages=[{"role": "user", "content": prompt}], max_tokens=10)
 
 
 
 
55
  score = int(''.join(filter(str.isdigit, res.choices[0].message.content)))
56
  except: score = 50
57
  results.append({"Country": name, "Risk": score})
58
- st.plotly_chart(px.bar(pd.DataFrame(results), x="Country", y="Risk", color="Risk", color_continuous_scale="Viridis"))
 
59
 
60
- # --- [Tab 2: Policy & Deep Intelligence (์‚ฌ์šฉ์ž ์š”์ฒญ ๊ฐ•ํ™” ๋ฒ„์ „)] ---
61
  with tab_deep:
62
  col_d1, col_d2 = st.columns([1, 4])
63
  with col_d1:
64
- target_country = st.selectbox("Market Selection", list(MONITOR_LIST.keys()))
65
  data_vol = st.slider("Intel Volume", 10, 60, 30)
66
- run_deep = st.button("๐Ÿš€ Generate Strategic Report", use_container_width=True)
67
 
68
  if run_deep:
69
  conf = MONITOR_LIST[target_country]
70
- with st.status("Profiling Policy Risks & Vulnerable Groups...", expanded=True) as status:
71
- g_news = fetch_google_news(f"{target_country} controversy safety risk", limit=data_vol//2)
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 Global Trust & Safety Director. Analyze the following news for {target_country} and provide a high-level strategic report.
 
79
 
80
- REPORT REQUIREMENTS (MUST BE IN ENGLISH):
81
- 1. 'guideline_threats': Identify specific violations (e.g., Harassment, Dangerous Acts, Child Safety, Hate Speech).
82
- - Explain WHY and HOW it violates specific platform policies.
83
- - Assign a Threat Level (Critical/High/Medium).
84
- 2. 'vulnerable_demographics': Identify who is at risk:
85
- - Minors: Is there a risk of grooming, exploitation, or mental harm?
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
- res = client.chat_completion(
94
- model="Qwen/Qwen2.5-72B-Instruct",
 
95
  messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": feed_text}],
96
- max_tokens=3500
97
  )
98
- report_data = res.choices[0].message.content
99
- status.update(label="Analysis Complete", state="complete")
100
 
101
- # UI ์ถœ๋ ฅ
102
- st.markdown(f"## ๐Ÿ“‘ National Stability & Safety Report: {target_country}")
103
 
104
- # Qwen์ด ํ…์ŠคํŠธ๋กœ ๋ณด๋‚ผ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด ๋งˆํฌ๋‹ค์šด์œผ๋กœ ๊น”๋”ํ•˜๊ฒŒ ์ถœ๋ ฅ
105
- st.markdown(report_data)
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')}")