Spaces:
Building
Building
Update app.py
Browse files
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' ๊ตญ๊ฐ ์ ํํ '์ผ์ฑ' ์
๋ ฅ์ 'ไธๆ'์ผ๋ก ์๋ ๊ฒ์")
|