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:
|