Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ from bs4 import BeautifulSoup
|
|
| 12 |
import re
|
| 13 |
import pathlib
|
| 14 |
import sqlite3
|
|
|
|
| 15 |
|
| 16 |
# 한국 기업 리스트
|
| 17 |
KOREAN_COMPANIES = [
|
|
@@ -33,7 +34,52 @@ KOREAN_COMPANIES = [
|
|
| 33 |
"Celltrion"
|
| 34 |
]
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def init_db():
|
| 38 |
db_path = pathlib.Path("search_results.db")
|
| 39 |
conn = sqlite3.connect(db_path)
|
|
@@ -47,14 +93,24 @@ def init_db():
|
|
| 47 |
conn.commit()
|
| 48 |
conn.close()
|
| 49 |
|
|
|
|
| 50 |
def save_to_db(keyword, country, results):
|
| 51 |
conn = sqlite3.connect("search_results.db")
|
| 52 |
c = conn.cursor()
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
conn.commit()
|
| 56 |
conn.close()
|
| 57 |
|
|
|
|
| 58 |
def load_from_db(keyword, country):
|
| 59 |
conn = sqlite3.connect("search_results.db")
|
| 60 |
c = conn.cursor()
|
|
@@ -63,9 +119,10 @@ def load_from_db(keyword, country):
|
|
| 63 |
result = c.fetchone()
|
| 64 |
conn.close()
|
| 65 |
if result:
|
| 66 |
-
return json.loads(result[0]), result[1]
|
| 67 |
return None, None
|
| 68 |
|
|
|
|
| 69 |
def display_results(articles):
|
| 70 |
output = ""
|
| 71 |
for idx, article in enumerate(articles, 1):
|
|
@@ -76,6 +133,7 @@ def display_results(articles):
|
|
| 76 |
output += f"요약: {article['snippet']}\n\n"
|
| 77 |
return output
|
| 78 |
|
|
|
|
| 79 |
def search_company(company):
|
| 80 |
error_message, articles = serphouse_search(company, "United States")
|
| 81 |
if not error_message and articles:
|
|
@@ -83,38 +141,54 @@ def search_company(company):
|
|
| 83 |
return display_results(articles)
|
| 84 |
return f"{company}에 대한 검색 결과가 없습니다."
|
| 85 |
|
|
|
|
| 86 |
def load_company(company):
|
| 87 |
results, timestamp = load_from_db(company, "United States")
|
| 88 |
if results:
|
| 89 |
return f"### {company} 검색 결과\n저장 시간: {timestamp}\n\n" + display_results(results)
|
| 90 |
return f"{company}에 대한 저장된 결과가 없습니다."
|
| 91 |
|
|
|
|
| 92 |
def show_stats():
|
| 93 |
conn = sqlite3.connect("search_results.db")
|
| 94 |
c = conn.cursor()
|
| 95 |
-
c.execute("""
|
| 96 |
-
SELECT keyword, COUNT(*) as count,
|
| 97 |
-
MAX(timestamp) as last_search
|
| 98 |
-
FROM searches
|
| 99 |
-
WHERE keyword IN ({})
|
| 100 |
-
GROUP BY keyword
|
| 101 |
-
ORDER BY count DESC, last_search DESC
|
| 102 |
-
""".format(','.join(['?']*len(KOREAN_COMPANIES))), KOREAN_COMPANIES)
|
| 103 |
-
stats = c.fetchall()
|
| 104 |
-
conn.close()
|
| 105 |
|
| 106 |
-
output = "## 한국 기업
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
return output
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 120 |
if not ACCESS_TOKEN:
|
|
|
|
| 12 |
import re
|
| 13 |
import pathlib
|
| 14 |
import sqlite3
|
| 15 |
+
import pytz
|
| 16 |
|
| 17 |
# 한국 기업 리스트
|
| 18 |
KOREAN_COMPANIES = [
|
|
|
|
| 34 |
"Celltrion"
|
| 35 |
]
|
| 36 |
|
| 37 |
+
def convert_to_seoul_time(timestamp_str):
|
| 38 |
+
try:
|
| 39 |
+
utc_time = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')
|
| 40 |
+
utc_time = pytz.utc.localize(utc_time)
|
| 41 |
+
seoul_tz = pytz.timezone('Asia/Seoul')
|
| 42 |
+
seoul_time = utc_time.astimezone(seoul_tz)
|
| 43 |
+
return seoul_time.strftime('%Y-%m-%d %H:%M:%S KST')
|
| 44 |
+
except:
|
| 45 |
+
return timestamp_str
|
| 46 |
+
|
| 47 |
+
def analyze_sentiment_batch(articles, client):
|
| 48 |
+
try:
|
| 49 |
+
# 모든 기사의 제목과 내용을 하나의 텍스트로 결합
|
| 50 |
+
combined_text = "\n\n".join([
|
| 51 |
+
f"제목: {article.get('title', '')}\n내용: {article.get('snippet', '')}"
|
| 52 |
+
for article in articles
|
| 53 |
+
])
|
| 54 |
+
|
| 55 |
+
prompt = f"""다음 뉴스 모음에 대해 전반적인 감성 분석을 수행하세요:
|
| 56 |
+
|
| 57 |
+
뉴스 내용:
|
| 58 |
+
{combined_text}
|
| 59 |
+
|
| 60 |
+
다음 형식으로 분석해주세요:
|
| 61 |
+
1. 전반적 감성: [긍정/부정/중립]
|
| 62 |
+
2. 주요 긍정적 요소:
|
| 63 |
+
- [항목1]
|
| 64 |
+
- [항목2]
|
| 65 |
+
3. 주요 부정적 요소:
|
| 66 |
+
- [항목1]
|
| 67 |
+
- [항목2]
|
| 68 |
+
4. 종합 평가: [상세 설명]
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
response = client.chat.completions.create(
|
| 72 |
+
model="CohereForAI/c4ai-command-r-plus-08-2024",
|
| 73 |
+
messages=[{"role": "user", "content": prompt}],
|
| 74 |
+
temperature=0.3,
|
| 75 |
+
max_tokens=1000
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return response.choices[0].message.content
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"감성 분석 실패: {str(e)}"
|
| 81 |
+
|
| 82 |
+
# DB 초기화 함수
|
| 83 |
def init_db():
|
| 84 |
db_path = pathlib.Path("search_results.db")
|
| 85 |
conn = sqlite3.connect(db_path)
|
|
|
|
| 93 |
conn.commit()
|
| 94 |
conn.close()
|
| 95 |
|
| 96 |
+
# 검색 결과 저장 함수
|
| 97 |
def save_to_db(keyword, country, results):
|
| 98 |
conn = sqlite3.connect("search_results.db")
|
| 99 |
c = conn.cursor()
|
| 100 |
+
|
| 101 |
+
# 서울 시간으로 변환
|
| 102 |
+
seoul_tz = pytz.timezone('Asia/Seoul')
|
| 103 |
+
seoul_time = datetime.now(seoul_tz)
|
| 104 |
+
|
| 105 |
+
c.execute("""INSERT INTO searches
|
| 106 |
+
(keyword, country, results, timestamp)
|
| 107 |
+
VALUES (?, ?, ?, ?)""",
|
| 108 |
+
(keyword, country, json.dumps(results),
|
| 109 |
+
seoul_time.strftime('%Y-%m-%d %H:%M:%S')))
|
| 110 |
conn.commit()
|
| 111 |
conn.close()
|
| 112 |
|
| 113 |
+
# DB에서 검색 결과 불러오기 함수
|
| 114 |
def load_from_db(keyword, country):
|
| 115 |
conn = sqlite3.connect("search_results.db")
|
| 116 |
c = conn.cursor()
|
|
|
|
| 119 |
result = c.fetchone()
|
| 120 |
conn.close()
|
| 121 |
if result:
|
| 122 |
+
return json.loads(result[0]), convert_to_seoul_time(result[1])
|
| 123 |
return None, None
|
| 124 |
|
| 125 |
+
# 결과 표시 함수
|
| 126 |
def display_results(articles):
|
| 127 |
output = ""
|
| 128 |
for idx, article in enumerate(articles, 1):
|
|
|
|
| 133 |
output += f"요약: {article['snippet']}\n\n"
|
| 134 |
return output
|
| 135 |
|
| 136 |
+
# 기업 검색 함수
|
| 137 |
def search_company(company):
|
| 138 |
error_message, articles = serphouse_search(company, "United States")
|
| 139 |
if not error_message and articles:
|
|
|
|
| 141 |
return display_results(articles)
|
| 142 |
return f"{company}에 대한 검색 결과가 없습니다."
|
| 143 |
|
| 144 |
+
# 기업 결과 불러오기 함수
|
| 145 |
def load_company(company):
|
| 146 |
results, timestamp = load_from_db(company, "United States")
|
| 147 |
if results:
|
| 148 |
return f"### {company} 검색 결과\n저장 시간: {timestamp}\n\n" + display_results(results)
|
| 149 |
return f"{company}에 대한 저장된 결과가 없습니다."
|
| 150 |
|
| 151 |
+
# 통계 분석 함수
|
| 152 |
def show_stats():
|
| 153 |
conn = sqlite3.connect("search_results.db")
|
| 154 |
c = conn.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
output = "## 한국 기업 뉴스 분석 리포트\n\n"
|
| 157 |
+
|
| 158 |
+
for company in KOREAN_COMPANIES:
|
| 159 |
+
c.execute("""
|
| 160 |
+
SELECT results, timestamp
|
| 161 |
+
FROM searches
|
| 162 |
+
WHERE keyword = ?
|
| 163 |
+
ORDER BY timestamp DESC
|
| 164 |
+
LIMIT 1
|
| 165 |
+
""", (company,))
|
| 166 |
+
|
| 167 |
+
result = c.fetchone()
|
| 168 |
+
if result:
|
| 169 |
+
results_json, timestamp = result
|
| 170 |
+
articles = json.loads(results_json)
|
| 171 |
+
seoul_time = convert_to_seoul_time(timestamp)
|
| 172 |
+
|
| 173 |
+
output += f"### {company}\n"
|
| 174 |
+
output += f"- 마지막 업데이트: {seoul_time}\n"
|
| 175 |
+
output += f"- 저장된 기사 수: {len(articles)}건\n\n"
|
| 176 |
+
|
| 177 |
+
if articles:
|
| 178 |
+
# 전체 기사에 대한 감성 분석
|
| 179 |
+
sentiment_analysis = analyze_sentiment_batch(articles, client)
|
| 180 |
+
output += "#### 뉴스 감성 분석\n"
|
| 181 |
+
output += f"{sentiment_analysis}\n\n"
|
| 182 |
+
|
| 183 |
+
output += "---\n\n"
|
| 184 |
+
|
| 185 |
+
conn.close()
|
| 186 |
return output
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
|
|
|
| 192 |
|
| 193 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 194 |
if not ACCESS_TOKEN:
|