ginipick commited on
Commit
e12dec3
ยท
verified ยท
1 Parent(s): 79f481e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -2
app.py CHANGED
@@ -10,7 +10,74 @@ from requests.packages.urllib3.util.retry import Retry
10
  from openai import OpenAI
11
  from bs4 import BeautifulSoup
12
  import re # re ๋ชจ๋“ˆ ์ถ”๊ฐ€
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
 
14
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
15
  if not ACCESS_TOKEN:
16
  raise ValueError("HF_TOKEN environment variable is not set")
@@ -1081,10 +1148,32 @@ def continue_writing(history, system_message, max_tokens, temperature, top_p):
1081
  yield new_history
1082
 
1083
  return history
1084
-
1085
  with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="NewsAI ์„œ๋น„์Šค") as iface:
 
 
1086
  with gr.Tabs():
1087
- # ๊ตญ๊ฐ€๋ณ„ ํƒญ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1088
  with gr.Tab("๊ตญ๊ฐ€๋ณ„"):
1089
  gr.Markdown("๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์›ํ•˜๋Š” ๊ตญ๊ฐ€(ํ•œ๊ตญ ์ œ์™ธ)๋ฅผ๋ฅผ ์„ ํƒํ•˜๋ฉด, ๊ฒ€์ƒ‰์–ด์™€ ์ผ์น˜ํ•˜๋Š” 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค๋ฅผ ์ตœ๋Œ€ 100๊ฐœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.")
1090
  gr.Markdown("๊ตญ๊ฐ€ ์„ ํƒํ›„ ๊ฒ€์ƒ‰์–ด์— 'ํ•œ๊ธ€'์„ ์ž…๋ ฅํ•˜๋ฉด ํ˜„์ง€ ์–ธ์–ด๋กœ ๋ฒˆ์—ญ๋˜์–ด ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ: 'Taiwan' ๊ตญ๊ฐ€ ์„ ํƒํ›„ '์‚ผ์„ฑ' ์ž…๋ ฅ์‹œ 'ไธ‰ๆ˜Ÿ'์œผ๋กœ ์ž๋™ ๊ฒ€์ƒ‰")
 
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)
23
+ c = conn.cursor()
24
+ c.execute('''CREATE TABLE IF NOT EXISTS searches
25
+ (id INTEGER PRIMARY KEY AUTOINCREMENT,
26
+ keyword TEXT,
27
+ country TEXT,
28
+ results TEXT,
29
+ timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
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()
37
+ c.execute("INSERT INTO searches (keyword, country, results) VALUES (?, ?, ?)",
38
+ (keyword, country, json.dumps(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()
46
+ c.execute("SELECT results, timestamp FROM searches WHERE keyword=? AND country=? ORDER BY timestamp DESC LIMIT 1",
47
+ (keyword, country))
48
+ result = c.fetchone()
49
+ conn.close()
50
+ if result:
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):
73
+ output += f"### {idx}. {article['title']}\n"
74
+ output += f"์ถœ์ฒ˜: {article['channel']}\n"
75
+ output += f"์‹œ๊ฐ„: {article['time']}\n"
76
+ output += f"๋งํฌ: {article['link']}\n"
77
+ output += f"์š”์•ฝ: {article['snippet']}\n\n"
78
+ return output
79
 
80
+
81
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
82
  if not ACCESS_TOKEN:
83
  raise ValueError("HF_TOKEN environment variable is not set")
 
1148
  yield new_history
1149
 
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' ๊ตญ๊ฐ€ ์„ ํƒํ›„ '์‚ผ์„ฑ' ์ž…๋ ฅ์‹œ 'ไธ‰ๆ˜Ÿ'์œผ๋กœ ์ž๋™ ๊ฒ€์ƒ‰")