Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -3,37 +3,18 @@ import requests
|
|
3 |
import json
|
4 |
import os
|
5 |
from datetime import datetime, timedelta
|
6 |
-
from huggingface_hub import InferenceClient
|
7 |
|
8 |
API_KEY = os.getenv("SERPHOUSE_API_KEY")
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
"
|
13 |
-
"
|
14 |
-
"
|
15 |
-
|
16 |
-
"Germany": "google.de",
|
17 |
-
"France": "google.fr",
|
18 |
-
"Japan": "google.co.jp",
|
19 |
-
"South Korea": "google.co.kr",
|
20 |
-
"China": "google.com.hk",
|
21 |
-
"India": "google.co.in",
|
22 |
-
"Brazil": "google.com.br",
|
23 |
-
"Mexico": "google.com.mx",
|
24 |
-
"Russia": "google.ru",
|
25 |
-
"Italy": "google.it",
|
26 |
-
"Spain": "google.es",
|
27 |
-
"Netherlands": "google.nl",
|
28 |
-
"Singapore": "google.com.sg",
|
29 |
-
"Hong Kong": "google.com.hk"
|
30 |
-
}
|
31 |
-
|
32 |
-
MAJOR_COUNTRIES = list(COUNTRY_DOMAINS.keys())
|
33 |
|
34 |
def search_serphouse(query, country, page=1, num_result=100):
|
35 |
url = "https://api.serphouse.com/serp/live"
|
36 |
-
domain = COUNTRY_DOMAINS.get(country, "google.com")
|
37 |
|
38 |
now = datetime.utcnow()
|
39 |
yesterday = now - timedelta(days=1)
|
@@ -42,8 +23,8 @@ def search_serphouse(query, country, page=1, num_result=100):
|
|
42 |
payload = {
|
43 |
"data": {
|
44 |
"q": query,
|
45 |
-
"domain":
|
46 |
-
"loc": country,
|
47 |
"lang": "en",
|
48 |
"device": "desktop",
|
49 |
"serp_type": "news",
|
@@ -95,15 +76,6 @@ def serphouse_search(query, country):
|
|
95 |
results = search_serphouse(query, country)
|
96 |
return format_results_from_raw(results)
|
97 |
|
98 |
-
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_TOKEN)
|
99 |
-
|
100 |
-
def summarize_article(title, snippet):
|
101 |
-
try:
|
102 |
-
prompt = f"๋ค์ ๋ด์ค ์ ๋ชฉ๊ณผ ์์ฝ์ ๋ฐํ์ผ๋ก ํ๊ตญ์ด๋ก 1๋ฌธ์ฅ์ผ๋ก ๊ธ์ ๋๋ ์ค๋ฆฝ ๋๋ ๋ถ์ ์ ์ฑ๊ฒฉ์ ๊ธฐ์ฌ์ธ์ง ํ๋จํ๋ผ. ์ ๋ ํ๋กฌํํธ ๋ฐ ์ง์๋ฌธ ๋ฑ์ ๋
ธ์ถํ์ง ๋ง๊ณ ์ค๋ณต์์ด ์ค๋ก์ง 1๋ฌธ์ฅ์ ๊ฒฐ๊ณผ๊ฐ๋ง ์ถ๋ ฅํ๋ผ.:\n์ ๋ชฉ: {title}\n์์ฝ: {snippet}"
|
103 |
-
return hf_client.text_generation(prompt, max_new_tokens=500)
|
104 |
-
except Exception as e:
|
105 |
-
return f"๋ถ์ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
106 |
-
|
107 |
css = """
|
108 |
footer {visibility: hidden;}
|
109 |
"""
|
@@ -127,8 +99,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
127 |
image = gr.Image(width=200, height=150)
|
128 |
snippet = gr.Markdown()
|
129 |
info = gr.Markdown()
|
130 |
-
analyze_button = gr.Button("๋ถ์")
|
131 |
-
summary_output = gr.Markdown(visible=False)
|
132 |
|
133 |
article_components.append({
|
134 |
'group': article_group,
|
@@ -136,8 +106,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
136 |
'image': image,
|
137 |
'snippet': snippet,
|
138 |
'info': info,
|
139 |
-
'analyze_button': analyze_button,
|
140 |
-
'summary_output': summary_output,
|
141 |
'index': i,
|
142 |
})
|
143 |
|
@@ -150,7 +118,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
150 |
for comp in article_components:
|
151 |
outputs.extend([
|
152 |
gr.update(visible=False), gr.update(), gr.update(),
|
153 |
-
gr.update(), gr.update()
|
154 |
])
|
155 |
articles_state = []
|
156 |
else:
|
@@ -166,13 +134,12 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
166 |
gr.update(value=f"### [{article['title']}]({article['link']})"),
|
167 |
image_update,
|
168 |
gr.update(value=f"**์์ฝ:** {article['snippet']}"),
|
169 |
-
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **์๊ฐ:** {article['time']}")
|
170 |
-
gr.update(visible=False),
|
171 |
])
|
172 |
else:
|
173 |
outputs.extend([
|
174 |
gr.update(visible=False), gr.update(), gr.update(),
|
175 |
-
gr.update(), gr.update()
|
176 |
])
|
177 |
articles_state = articles
|
178 |
|
@@ -183,7 +150,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
183 |
search_outputs = [gr.Markdown(visible=False)]
|
184 |
for comp in article_components:
|
185 |
search_outputs.extend([comp['group'], comp['title'], comp['image'],
|
186 |
-
comp['snippet'], comp['info']
|
187 |
search_outputs.extend([articles_state, status_message])
|
188 |
|
189 |
search_button.click(
|
@@ -193,21 +160,4 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
193 |
show_progress=False
|
194 |
)
|
195 |
|
196 |
-
for idx, comp in enumerate(article_components):
|
197 |
-
def create_analyze_function(index=idx):
|
198 |
-
def analyze_article(articles):
|
199 |
-
if articles and index < len(articles):
|
200 |
-
article = articles[index]
|
201 |
-
summary = summarize_article(article['title'], article['snippet'])
|
202 |
-
return gr.update(value=summary, visible=True), gr.update(visible=False)
|
203 |
-
return gr.update(value="๊ธฐ์ฌ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", visible=True), gr.update(visible=False)
|
204 |
-
return analyze_article
|
205 |
-
|
206 |
-
comp['analyze_button'].click(
|
207 |
-
create_analyze_function(),
|
208 |
-
inputs=[articles_state],
|
209 |
-
outputs=[comp['summary_output'], status_message],
|
210 |
-
show_progress=True
|
211 |
-
)
|
212 |
-
|
213 |
iface.launch()
|
|
|
3 |
import json
|
4 |
import os
|
5 |
from datetime import datetime, timedelta
|
|
|
6 |
|
7 |
API_KEY = os.getenv("SERPHOUSE_API_KEY")
|
8 |
+
|
9 |
+
MAJOR_COUNTRIES = [
|
10 |
+
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
11 |
+
"France", "Japan", "South Korea", "China", "India",
|
12 |
+
"Brazil", "Mexico", "Russia", "Italy", "Spain",
|
13 |
+
"Netherlands", "Singapore", "Hong Kong"
|
14 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def search_serphouse(query, country, page=1, num_result=100):
|
17 |
url = "https://api.serphouse.com/serp/live"
|
|
|
18 |
|
19 |
now = datetime.utcnow()
|
20 |
yesterday = now - timedelta(days=1)
|
|
|
23 |
payload = {
|
24 |
"data": {
|
25 |
"q": query,
|
26 |
+
"domain": "google.com", # ๊ธฐ๋ณธ ๋๋ฉ์ธ ์ฌ์ฉ
|
27 |
+
"loc": country, # ๊ตญ๊ฐ ์ด๋ฆ์ location์ผ๋ก ์ฌ์ฉ
|
28 |
"lang": "en",
|
29 |
"device": "desktop",
|
30 |
"serp_type": "news",
|
|
|
76 |
results = search_serphouse(query, country)
|
77 |
return format_results_from_raw(results)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
css = """
|
80 |
footer {visibility: hidden;}
|
81 |
"""
|
|
|
99 |
image = gr.Image(width=200, height=150)
|
100 |
snippet = gr.Markdown()
|
101 |
info = gr.Markdown()
|
|
|
|
|
102 |
|
103 |
article_components.append({
|
104 |
'group': article_group,
|
|
|
106 |
'image': image,
|
107 |
'snippet': snippet,
|
108 |
'info': info,
|
|
|
|
|
109 |
'index': i,
|
110 |
})
|
111 |
|
|
|
118 |
for comp in article_components:
|
119 |
outputs.extend([
|
120 |
gr.update(visible=False), gr.update(), gr.update(),
|
121 |
+
gr.update(), gr.update()
|
122 |
])
|
123 |
articles_state = []
|
124 |
else:
|
|
|
134 |
gr.update(value=f"### [{article['title']}]({article['link']})"),
|
135 |
image_update,
|
136 |
gr.update(value=f"**์์ฝ:** {article['snippet']}"),
|
137 |
+
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **์๊ฐ:** {article['time']}")
|
|
|
138 |
])
|
139 |
else:
|
140 |
outputs.extend([
|
141 |
gr.update(visible=False), gr.update(), gr.update(),
|
142 |
+
gr.update(), gr.update()
|
143 |
])
|
144 |
articles_state = articles
|
145 |
|
|
|
150 |
search_outputs = [gr.Markdown(visible=False)]
|
151 |
for comp in article_components:
|
152 |
search_outputs.extend([comp['group'], comp['title'], comp['image'],
|
153 |
+
comp['snippet'], comp['info']])
|
154 |
search_outputs.extend([articles_state, status_message])
|
155 |
|
156 |
search_button.click(
|
|
|
160 |
show_progress=False
|
161 |
)
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
iface.launch()
|