ginipick commited on
Commit
184478e
ยท
verified ยท
1 Parent(s): 7c3b41f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -39
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 # re ๋ชจ๋“ˆ ์ถ”๊ฐ€
13
- import json
14
- import os
15
- from datetime import datetime
16
- import sqlite3
17
  import pathlib
 
18
 
19
- # DB ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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() # DB ์ดˆ๊ธฐํ™”
1154
 
1155
  with gr.Tabs():
1156
  # DB ์ €์žฅ/๋ถˆ๋Ÿฌ์˜ค๊ธฐ ํƒญ
1157
  with gr.Tab("DB ๊ฒ€์ƒ‰"):
1158
- gr.Markdown("์‚ผ์„ฑ/๋ฏธ๊ตญ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ DB์— ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.")
 
1159
 
1160
- with gr.Row():
1161
- search_button = gr.Button("๊ฒ€์ƒ‰: samsung/๋ฏธ๊ตญ", variant="primary")
1162
- load_button = gr.Button("์ถœ๋ ฅ: samsung/๋ฏธ๊ตญ", variant="secondary")
1163
-
1164
- results_display = gr.Markdown()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1165
 
1166
- # ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
1167
- search_button.click(
1168
- fn=search_samsung_us,
1169
- outputs=results_display
1170
- )
1171
 
1172
- load_button.click(
1173
- fn=load_samsung_us,
1174
- outputs=results_display
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' ๊ตญ๊ฐ€ ์„ ํƒํ›„ '์‚ผ์„ฑ' ์ž…๋ ฅ์‹œ 'ไธ‰ๆ˜Ÿ'์œผ๋กœ ์ž๋™ ๊ฒ€์ƒ‰")