Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,120 +1,116 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import feedparser
|
| 3 |
import urllib.parse
|
| 4 |
-
from groq import 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
|
| 14 |
|
| 15 |
-
# ---
|
| 16 |
-
MONITOR_LIST = {
|
| 17 |
-
"South Korea": {"geo": "KR", "lang": "ko"}, "USA": {"geo": "US", "lang": "en"},
|
| 18 |
-
"Japan": {"geo": "JP", "lang": "ja"}, "UK": {"geo": "GB", "lang": "en"},
|
| 19 |
-
"France": {"geo": "FR", "lang": "fr"}, "Germany": {"geo": "DE", "lang": "de"},
|
| 20 |
-
"India": {"geo": "IN", "lang": "en"}, "Malaysia": {"geo": "MY", "lang": "en"},
|
| 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 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 40 |
-
st.
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
st.
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 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 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 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 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
st.
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import feedparser
|
| 3 |
import urllib.parse
|
| 4 |
+
from groq import Groq
|
| 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="Llama3 Intelligence Center", 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"]
|
| 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"}, "Malaysia": {"geo": "MY", "lang": "en"}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
def fetch_google_news(query, geo="US", lang="en", limit=15):
|
| 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 |
# --- UI ---
|
| 40 |
+
st.title("π¦ Llama 3 Strategic Command Center")
|
| 41 |
+
st.markdown("---")
|
| 42 |
|
| 43 |
+
col_d1, col_d2 = st.columns([1, 4])
|
| 44 |
+
with col_d1:
|
| 45 |
+
target_country = st.selectbox("Market Selection", list(MONITOR_LIST.keys()))
|
| 46 |
+
data_vol = st.slider("Intel Depth", 10, 60, 30)
|
| 47 |
+
run_deep = st.button("π Generate Strategic Report", use_container_width=True)
|
| 48 |
|
| 49 |
+
if run_deep:
|
| 50 |
+
conf = MONITOR_LIST[target_country]
|
| 51 |
+
with st.status(f"Llama 3.1 70B: Deep Analyzing {target_country}...", expanded=True):
|
| 52 |
+
# λ°μ΄ν° μμ§
|
| 53 |
+
g_news = fetch_google_news(f"{target_country} social controversy risk", limit=data_vol//2)
|
| 54 |
+
l_news = fetch_google_news(target_country, geo=conf['geo'], lang=conf['lang'], limit=data_vol//2)
|
| 55 |
+
feed_text = "\n".join([f"- {n['title']}" for n in g_news + l_news])
|
| 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 |
+
OUTPUT STRUCTURE (JSON):
|
| 65 |
+
1. 'risk_summary_report': A long, detailed executive report (at least 200 words). Discuss the context, the psychological impact on the public, and the specific socio-political nuances.
|
| 66 |
+
2. 'policy_violation_detail': Detailed breakdown of which Community Guidelines are at risk and WHY.
|
| 67 |
+
3. 'vulnerability_analysis': Detailed analysis on how this affects Minors, Women, and Ethnic groups.
|
| 68 |
+
4. 'tactical_recommendations': At least 5 specific steps for platform moderators.
|
| 69 |
+
5. 'risk_score': 0-100 integer.
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
res = client.chat.completions.create(
|
| 73 |
+
model="llama-3.1-70b-versatile",
|
| 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 |
+
st.markdown("### π‘οΈ Tactical Recommendations")
|
| 101 |
+
# 리μ€νΈ ννλ‘ μΆλ ₯
|
| 102 |
+
if isinstance(data['tactical_recommendations'], list):
|
| 103 |
+
for rec in data['tactical_recommendations']:
|
| 104 |
+
st.write(f"- {rec}")
|
| 105 |
+
else:
|
| 106 |
+
st.write(data['tactical_recommendations'])
|
| 107 |
+
|
| 108 |
+
with col_b:
|
| 109 |
+
st.markdown("### π₯ Vulnerability Analysis")
|
| 110 |
+
st.warning(data['vulnerability_analysis'])
|
| 111 |
|
| 112 |
+
st.markdown("### π° Evidence Feed")
|
| 113 |
+
st.dataframe(pd.DataFrame(g_news + l_news), use_container_width=True)
|
| 114 |
+
|
| 115 |
+
else:
|
| 116 |
+
st.info("π Select a market and press the button to generate a deep-dive report.")
|