Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -123,7 +123,6 @@ hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=HF_
|
|
123 |
|
124 |
def summarize_article(title, snippet):
|
125 |
try:
|
126 |
-
# 기사 제목과 스니펫을 기반으로 요약 생성
|
127 |
prompt = f"다음 뉴스 제목과 요약을 바탕으로 한국어로 3문장으로 요약하세요:\n제목: {title}\n요약: {snippet}"
|
128 |
summary = hf_client.text_generation(prompt, max_new_tokens=500)
|
129 |
return summary
|
@@ -146,6 +145,9 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI 서비스") as
|
|
146 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="국가", value="South Korea")
|
147 |
search_button = gr.Button("검색")
|
148 |
|
|
|
|
|
|
|
149 |
# 최대 10개의 기사에 대한 컴포넌트를 미리 생성합니다.
|
150 |
article_components = []
|
151 |
for i in range(10):
|
@@ -165,93 +167,84 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI 서비스") as
|
|
165 |
'info': info,
|
166 |
'analyze_button': analyze_button,
|
167 |
'summary_output': summary_output,
|
|
|
168 |
})
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
for comp in article_components:
|
177 |
-
outputs.extend([
|
178 |
-
gr.update(visible=False), # group
|
179 |
-
gr.update(), # title
|
180 |
-
gr.update(), # image
|
181 |
-
gr.update(), # snippet
|
182 |
-
gr.update(), # info
|
183 |
-
gr.update(), # analyze_button
|
184 |
-
gr.update(visible=False), # summary_output
|
185 |
-
])
|
186 |
-
return outputs
|
187 |
-
else:
|
188 |
-
# 기사 컴포넌트 업데이트
|
189 |
-
for idx, comp in enumerate(article_components):
|
190 |
-
if idx < len(articles):
|
191 |
-
article = articles[idx]
|
192 |
-
comp['group'].visible = True
|
193 |
-
comp['title'].value = f"### [{article['title']}]({article['link']})"
|
194 |
-
if article['image_url'] and not article['image_url'].startswith("data:image"):
|
195 |
-
comp['image'].value = article['image_url']
|
196 |
-
comp['image'].visible = True
|
197 |
-
else:
|
198 |
-
comp['image'].visible = False
|
199 |
-
comp['snippet'].value = f"**요약:** {article['snippet']}"
|
200 |
-
comp['info'].value = f"**출처:** {article['channel']} | **시간:** {article['time']}"
|
201 |
-
comp['summary_output'].visible = False # 초기에는 요약 숨김
|
202 |
-
|
203 |
-
# 분석 버튼 클릭 이벤트 정의
|
204 |
-
def create_analyze_function(article_title, article_snippet):
|
205 |
-
def analyze_article():
|
206 |
-
summary = summarize_article(article_title, article_snippet)
|
207 |
-
return gr.update(value=summary, visible=True)
|
208 |
-
return analyze_article
|
209 |
-
|
210 |
-
comp['analyze_button'].click(
|
211 |
-
create_analyze_function(article['title'], article['snippet']),
|
212 |
-
inputs=[],
|
213 |
-
outputs=comp['summary_output']
|
214 |
-
)
|
215 |
-
|
216 |
-
outputs.extend([
|
217 |
-
gr.update(visible=True), # group
|
218 |
-
gr.update(), # title
|
219 |
-
gr.update(), # image
|
220 |
-
gr.update(), # snippet
|
221 |
-
gr.update(), # info
|
222 |
-
gr.update(), # analyze_button
|
223 |
-
gr.update(visible=False), # summary_output
|
224 |
-
])
|
225 |
-
else:
|
226 |
-
# 남은 컴포넌트 숨기기
|
227 |
-
comp['group'].visible = False
|
228 |
outputs.extend([
|
229 |
gr.update(visible=False), # group
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
gr.update(), # analyze_button
|
235 |
gr.update(visible=False), # summary_output
|
236 |
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
return outputs
|
238 |
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
|
257 |
iface.launch(auth=("gini", "pick"))
|
|
|
123 |
|
124 |
def summarize_article(title, snippet):
|
125 |
try:
|
|
|
126 |
prompt = f"다음 뉴스 제목과 요약을 바탕으로 한국어로 3문장으로 요약하세요:\n제목: {title}\n요약: {snippet}"
|
127 |
summary = hf_client.text_generation(prompt, max_new_tokens=500)
|
128 |
return summary
|
|
|
145 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="국가", value="South Korea")
|
146 |
search_button = gr.Button("검색")
|
147 |
|
148 |
+
# 기사 데이터를 저장할 상태 변수
|
149 |
+
articles_state = gr.State()
|
150 |
+
|
151 |
# 최대 10개의 기사에 대한 컴포넌트를 미리 생성합니다.
|
152 |
article_components = []
|
153 |
for i in range(10):
|
|
|
167 |
'info': info,
|
168 |
'analyze_button': analyze_button,
|
169 |
'summary_output': summary_output,
|
170 |
+
'index': i,
|
171 |
})
|
172 |
|
173 |
+
def search_and_display(query, country):
|
174 |
+
error_message, articles = serphouse_search(query, country)
|
175 |
+
outputs = []
|
176 |
+
if error_message:
|
177 |
+
outputs.append(gr.update(value=error_message, visible=True))
|
178 |
+
for comp in article_components:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
outputs.extend([
|
180 |
gr.update(visible=False), # group
|
181 |
+
None, # title
|
182 |
+
None, # image
|
183 |
+
None, # snippet
|
184 |
+
None, # info
|
|
|
185 |
gr.update(visible=False), # summary_output
|
186 |
])
|
187 |
+
outputs.append(gr.update(value=[])) # articles_state 초기화
|
188 |
+
else:
|
189 |
+
outputs.append(gr.update(value="", visible=False))
|
190 |
+
for idx, comp in enumerate(article_components):
|
191 |
+
if idx < len(articles):
|
192 |
+
article = articles[idx]
|
193 |
+
outputs.extend([
|
194 |
+
gr.update(visible=True), # group
|
195 |
+
gr.update(value=f"### [{article['title']}]({article['link']})"), # title
|
196 |
+
gr.update(value=article['image_url'] if article['image_url'] else None, visible=bool(article['image_url'])), # image
|
197 |
+
gr.update(value=f"**요약:** {article['snippet']}"), # snippet
|
198 |
+
gr.update(value=f"**출처:** {article['channel']} | **시간:** {article['time']}"), # info
|
199 |
+
gr.update(visible=False), # summary_output
|
200 |
+
])
|
201 |
+
else:
|
202 |
+
outputs.extend([
|
203 |
+
gr.update(visible=False), # group
|
204 |
+
None, # title
|
205 |
+
None, # image
|
206 |
+
None, # snippet
|
207 |
+
None, # info
|
208 |
+
gr.update(visible=False), # summary_output
|
209 |
+
])
|
210 |
+
outputs.append(gr.update(value=articles)) # articles_state 업데이트
|
211 |
return outputs
|
212 |
|
213 |
+
# search_button 클릭 시 업데이트될 출력 컴포넌트 목록 생성
|
214 |
+
search_outputs = []
|
215 |
+
error_output = gr.Markdown(visible=False)
|
216 |
+
search_outputs.append(error_output)
|
217 |
+
for comp in article_components:
|
218 |
+
search_outputs.append(comp['group'])
|
219 |
+
search_outputs.append(comp['title'])
|
220 |
+
search_outputs.append(comp['image'])
|
221 |
+
search_outputs.append(comp['snippet'])
|
222 |
+
search_outputs.append(comp['info'])
|
223 |
+
search_outputs.append(comp['summary_output'])
|
224 |
+
search_outputs.append(articles_state)
|
225 |
+
|
226 |
+
search_button.click(
|
227 |
+
search_and_display,
|
228 |
+
inputs=[query, country],
|
229 |
+
outputs=search_outputs
|
230 |
+
)
|
231 |
+
|
232 |
+
# 분석 버튼 클릭 이벤트 설정
|
233 |
+
for idx, comp in enumerate(article_components):
|
234 |
+
def create_analyze_function(index):
|
235 |
+
def analyze_article(articles):
|
236 |
+
if index < len(articles):
|
237 |
+
article = articles[index]
|
238 |
+
summary = summarize_article(article['title'], article['snippet'])
|
239 |
+
return gr.update(value=summary, visible=True)
|
240 |
+
else:
|
241 |
+
return gr.update(value="기사 정보를 찾을 수 없습니다.", visible=True)
|
242 |
+
return analyze_article
|
243 |
+
|
244 |
+
comp['analyze_button'].click(
|
245 |
+
create_analyze_function(idx),
|
246 |
+
inputs=[articles_state],
|
247 |
+
outputs=comp['summary_output']
|
248 |
+
)
|
249 |
|
250 |
iface.launch(auth=("gini", "pick"))
|