ginipick commited on
Commit
cebb472
โ€ข
1 Parent(s): 1ea4fcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -20
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
- # DB ๊ด€๋ จ ํ•จ์ˆ˜๋“ค
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- c.execute("INSERT INTO searches (keyword, country, results) VALUES (?, ?, ?)",
54
- (keyword, country, json.dumps(results)))
 
 
 
 
 
 
 
 
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 = "## ํ•œ๊ตญ ๊ธฐ์—… ๊ฒ€์ƒ‰ ํ†ต๊ณ„\n\n"
107
- for keyword, count, last_search in stats:
108
- output += f"### {keyword}\n"
109
- output += f"- ๊ฒ€์ƒ‰ ํšŸ์ˆ˜: {count}ํšŒ\n"
110
- output += f"- ๋งˆ์ง€๋ง‰ ๊ฒ€์ƒ‰: {last_search}\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: