Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,36 @@ from requests.adapters import HTTPAdapter
|
|
| 9 |
from requests.packages.urllib3.util.retry import Retry
|
| 10 |
from openai import OpenAI
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
-
import re
|
| 13 |
-
import json
|
| 14 |
-
import os
|
| 15 |
-
from datetime import datetime
|
| 16 |
-
import sqlite3
|
| 17 |
import pathlib
|
|
|
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def init_db():
|
| 21 |
db_path = pathlib.Path("search_results.db")
|
| 22 |
conn = sqlite3.connect(db_path)
|
|
@@ -30,7 +52,6 @@ def init_db():
|
|
| 30 |
conn.commit()
|
| 31 |
conn.close()
|
| 32 |
|
| 33 |
-
# ๊ฒ์ ๊ฒฐ๊ณผ ์ ์ฅ ํจ์
|
| 34 |
def save_to_db(keyword, country, results):
|
| 35 |
conn = sqlite3.connect("search_results.db")
|
| 36 |
c = conn.cursor()
|
|
@@ -39,7 +60,6 @@ def save_to_db(keyword, country, results):
|
|
| 39 |
conn.commit()
|
| 40 |
conn.close()
|
| 41 |
|
| 42 |
-
# DB์์ ๊ฒ์ ๊ฒฐ๊ณผ ๋ถ๋ฌ์ค๊ธฐ ํจ์
|
| 43 |
def load_from_db(keyword, country):
|
| 44 |
conn = sqlite3.connect("search_results.db")
|
| 45 |
c = conn.cursor()
|
|
@@ -51,22 +71,6 @@ def load_from_db(keyword, country):
|
|
| 51 |
return json.loads(result[0]), result[1]
|
| 52 |
return None, None
|
| 53 |
|
| 54 |
-
# ์ผ์ฑ/๋ฏธ๊ตญ ๊ฒ์ ํจ์
|
| 55 |
-
def search_samsung_us():
|
| 56 |
-
error_message, articles = serphouse_search("samsung", "United States")
|
| 57 |
-
if not error_message and articles:
|
| 58 |
-
save_to_db("samsung", "United States", articles)
|
| 59 |
-
return display_results(articles)
|
| 60 |
-
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
|
| 61 |
-
|
| 62 |
-
# DB์์ ์ผ์ฑ/๋ฏธ๊ตญ ๊ฒฐ๊ณผ ๋ถ๋ฌ์ค๊ธฐ ํจ์
|
| 63 |
-
def load_samsung_us():
|
| 64 |
-
results, timestamp = load_from_db("samsung", "United States")
|
| 65 |
-
if results:
|
| 66 |
-
return f"์ ์ฅ ์๊ฐ: {timestamp}\n\n" + display_results(results)
|
| 67 |
-
return "์ ์ฅ๋ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
|
| 68 |
-
|
| 69 |
-
# ๊ฒฐ๊ณผ ํ์ ํจ์
|
| 70 |
def display_results(articles):
|
| 71 |
output = ""
|
| 72 |
for idx, article in enumerate(articles, 1):
|
|
@@ -77,6 +81,45 @@ def display_results(articles):
|
|
| 77 |
output += f"์์ฝ: {article['snippet']}\n\n"
|
| 78 |
return output
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 82 |
if not ACCESS_TOKEN:
|
|
@@ -1150,30 +1193,67 @@ def continue_writing(history, system_message, max_tokens, temperature, top_p):
|
|
| 1150 |
return history
|
| 1151 |
|
| 1152 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="NewsAI ์๋น์ค") as iface:
|
| 1153 |
-
init_db()
|
| 1154 |
|
| 1155 |
with gr.Tabs():
|
| 1156 |
# DB ์ ์ฅ/๋ถ๋ฌ์ค๊ธฐ ํญ
|
| 1157 |
with gr.Tab("DB ๊ฒ์"):
|
| 1158 |
-
gr.Markdown("
|
|
|
|
| 1159 |
|
| 1160 |
-
with gr.
|
| 1161 |
-
|
| 1162 |
-
|
| 1163 |
-
|
| 1164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1165 |
|
| 1166 |
-
#
|
| 1167 |
-
|
| 1168 |
-
|
| 1169 |
-
|
| 1170 |
-
)
|
| 1171 |
|
| 1172 |
-
|
| 1173 |
-
fn=
|
| 1174 |
-
outputs=
|
| 1175 |
)
|
| 1176 |
|
|
|
|
| 1177 |
with gr.Tab("๊ตญ๊ฐ๋ณ"):
|
| 1178 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ(ํ๊ตญ ์ ์ธ)๋ฅผ๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 100๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
| 1179 |
gr.Markdown("๊ตญ๊ฐ ์ ํํ ๊ฒ์์ด์ 'ํ๊ธ'์ ์
๋ ฅํ๋ฉด ํ์ง ์ธ์ด๋ก ๋ฒ์ญ๋์ด ๊ฒ์ํฉ๋๋ค. ์: 'Taiwan' ๊ตญ๊ฐ ์ ํํ '์ผ์ฑ' ์
๋ ฅ์ 'ไธๆ'์ผ๋ก ์๋ ๊ฒ์")
|
|
|
|
| 9 |
from requests.packages.urllib3.util.retry import Retry
|
| 10 |
from openai import OpenAI
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
+
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
import pathlib
|
| 14 |
+
import sqlite3
|
| 15 |
|
| 16 |
+
# ํ๊ตญ ๊ธฐ์
๋ฆฌ์คํธ
|
| 17 |
+
KOREAN_COMPANIES = [
|
| 18 |
+
"SAMSUNG",
|
| 19 |
+
"HYNIX",
|
| 20 |
+
"Celltrion",
|
| 21 |
+
"KIA",
|
| 22 |
+
"KB",
|
| 23 |
+
"NAVER",
|
| 24 |
+
"SHINHAN",
|
| 25 |
+
"HYUNDAI",
|
| 26 |
+
"POSCO",
|
| 27 |
+
"HANA",
|
| 28 |
+
"KT&G",
|
| 29 |
+
"WOORI",
|
| 30 |
+
"LG",
|
| 31 |
+
"IBK",
|
| 32 |
+
"POSCO",
|
| 33 |
+
"KT",
|
| 34 |
+
"SKT",
|
| 35 |
+
"DOOSAN",
|
| 36 |
+
"KAKAO",
|
| 37 |
+
"HANWHA",
|
| 38 |
+
"SK"
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# DB ๊ด๋ จ ํจ์๋ค
|
| 42 |
def init_db():
|
| 43 |
db_path = pathlib.Path("search_results.db")
|
| 44 |
conn = sqlite3.connect(db_path)
|
|
|
|
| 52 |
conn.commit()
|
| 53 |
conn.close()
|
| 54 |
|
|
|
|
| 55 |
def save_to_db(keyword, country, results):
|
| 56 |
conn = sqlite3.connect("search_results.db")
|
| 57 |
c = conn.cursor()
|
|
|
|
| 60 |
conn.commit()
|
| 61 |
conn.close()
|
| 62 |
|
|
|
|
| 63 |
def load_from_db(keyword, country):
|
| 64 |
conn = sqlite3.connect("search_results.db")
|
| 65 |
c = conn.cursor()
|
|
|
|
| 71 |
return json.loads(result[0]), result[1]
|
| 72 |
return None, None
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def display_results(articles):
|
| 75 |
output = ""
|
| 76 |
for idx, article in enumerate(articles, 1):
|
|
|
|
| 81 |
output += f"์์ฝ: {article['snippet']}\n\n"
|
| 82 |
return output
|
| 83 |
|
| 84 |
+
def search_company(company):
|
| 85 |
+
error_message, articles = serphouse_search(company, "United States")
|
| 86 |
+
if not error_message and articles:
|
| 87 |
+
save_to_db(company, "United States", articles)
|
| 88 |
+
return display_results(articles)
|
| 89 |
+
return f"{company}์ ๋ํ ๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
|
| 90 |
+
|
| 91 |
+
def load_company(company):
|
| 92 |
+
results, timestamp = load_from_db(company, "United States")
|
| 93 |
+
if results:
|
| 94 |
+
return f"### {company} ๊ฒ์ ๊ฒฐ๊ณผ\n์ ์ฅ ์๊ฐ: {timestamp}\n\n" + display_results(results)
|
| 95 |
+
return f"{company}์ ๋ํ ์ ์ฅ๋ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
|
| 96 |
+
|
| 97 |
+
def show_stats():
|
| 98 |
+
conn = sqlite3.connect("search_results.db")
|
| 99 |
+
c = conn.cursor()
|
| 100 |
+
c.execute("""
|
| 101 |
+
SELECT keyword, COUNT(*) as count,
|
| 102 |
+
MAX(timestamp) as last_search
|
| 103 |
+
FROM searches
|
| 104 |
+
WHERE keyword IN ({})
|
| 105 |
+
GROUP BY keyword
|
| 106 |
+
ORDER BY count DESC, last_search DESC
|
| 107 |
+
""".format(','.join(['?']*len(KOREAN_COMPANIES))), KOREAN_COMPANIES)
|
| 108 |
+
stats = c.fetchall()
|
| 109 |
+
conn.close()
|
| 110 |
+
|
| 111 |
+
output = "## ํ๊ตญ ๊ธฐ์
๊ฒ์ ํต๊ณ\n\n"
|
| 112 |
+
for keyword, count, last_search in stats:
|
| 113 |
+
output += f"### {keyword}\n"
|
| 114 |
+
output += f"- ๊ฒ์ ํ์: {count}ํ\n"
|
| 115 |
+
output += f"- ๋ง์ง๋ง ๊ฒ์: {last_search}\n\n"
|
| 116 |
+
return output
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
|
| 124 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
| 125 |
if not ACCESS_TOKEN:
|
|
|
|
| 1193 |
return history
|
| 1194 |
|
| 1195 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="NewsAI ์๋น์ค") as iface:
|
| 1196 |
+
init_db()
|
| 1197 |
|
| 1198 |
with gr.Tabs():
|
| 1199 |
# DB ์ ์ฅ/๋ถ๋ฌ์ค๊ธฐ ํญ
|
| 1200 |
with gr.Tab("DB ๊ฒ์"):
|
| 1201 |
+
gr.Markdown("## ํ๊ตญ ์ฃผ์ ๊ธฐ์
๋ฏธ๊ตญ ๋ด์ค DB")
|
| 1202 |
+
gr.Markdown("๊ฐ ๊ธฐ์
์ ๋ฏธ๊ตญ ๋ด์ค๋ฅผ ๊ฒ์ํ์ฌ DB์ ์ ์ฅํ๊ณ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.")
|
| 1203 |
|
| 1204 |
+
with gr.Column():
|
| 1205 |
+
for i in range(0, len(KOREAN_COMPANIES), 2):
|
| 1206 |
+
with gr.Row():
|
| 1207 |
+
# ์ฒซ ๋ฒ์งธ ์ด
|
| 1208 |
+
with gr.Column():
|
| 1209 |
+
company = KOREAN_COMPANIES[i]
|
| 1210 |
+
with gr.Group():
|
| 1211 |
+
gr.Markdown(f"### {company}")
|
| 1212 |
+
with gr.Row():
|
| 1213 |
+
search_btn = gr.Button(f"๊ฒ์", variant="primary")
|
| 1214 |
+
load_btn = gr.Button(f"์ถ๋ ฅ", variant="secondary")
|
| 1215 |
+
result_display = gr.Markdown()
|
| 1216 |
+
|
| 1217 |
+
search_btn.click(
|
| 1218 |
+
fn=lambda c=company: search_company(c),
|
| 1219 |
+
outputs=result_display
|
| 1220 |
+
)
|
| 1221 |
+
load_btn.click(
|
| 1222 |
+
fn=lambda c=company: load_company(c),
|
| 1223 |
+
outputs=result_display
|
| 1224 |
+
)
|
| 1225 |
+
|
| 1226 |
+
# ๋ ๋ฒ์งธ ์ด
|
| 1227 |
+
if i + 1 < len(KOREAN_COMPANIES):
|
| 1228 |
+
with gr.Column():
|
| 1229 |
+
company = KOREAN_COMPANIES[i + 1]
|
| 1230 |
+
with gr.Group():
|
| 1231 |
+
gr.Markdown(f"### {company}")
|
| 1232 |
+
with gr.Row():
|
| 1233 |
+
search_btn = gr.Button(f"๊ฒ์", variant="primary")
|
| 1234 |
+
load_btn = gr.Button(f"์ถ๋ ฅ", variant="secondary")
|
| 1235 |
+
result_display = gr.Markdown()
|
| 1236 |
+
|
| 1237 |
+
search_btn.click(
|
| 1238 |
+
fn=lambda c=company: search_company(c),
|
| 1239 |
+
outputs=result_display
|
| 1240 |
+
)
|
| 1241 |
+
load_btn.click(
|
| 1242 |
+
fn=lambda c=company: load_company(c),
|
| 1243 |
+
outputs=result_display
|
| 1244 |
+
)
|
| 1245 |
|
| 1246 |
+
# ์ ์ฒด ๊ฒ์ ํต๊ณ
|
| 1247 |
+
with gr.Row():
|
| 1248 |
+
stats_btn = gr.Button("์ ์ฒด ๊ฒ์ ํต๊ณ ๋ณด๊ธฐ", variant="secondary")
|
| 1249 |
+
stats_display = gr.Markdown()
|
|
|
|
| 1250 |
|
| 1251 |
+
stats_btn.click(
|
| 1252 |
+
fn=show_stats,
|
| 1253 |
+
outputs=stats_display
|
| 1254 |
)
|
| 1255 |
|
| 1256 |
+
|
| 1257 |
with gr.Tab("๊ตญ๊ฐ๋ณ"):
|
| 1258 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ(ํ๊ตญ ์ ์ธ)๋ฅผ๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 100๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
| 1259 |
gr.Markdown("๊ตญ๊ฐ ์ ํํ ๊ฒ์์ด์ 'ํ๊ธ'์ ์
๋ ฅํ๋ฉด ํ์ง ์ธ์ด๋ก ๋ฒ์ญ๋์ด ๊ฒ์ํฉ๋๋ค. ์: 'Taiwan' ๊ตญ๊ฐ ์ ํํ '์ผ์ฑ' ์
๋ ฅ์ 'ไธๆ'์ผ๋ก ์๋ ๊ฒ์")
|