-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarize.py
66 lines (53 loc) · 2.04 KB
/
summarize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
import pandas as pd
import pandas as pd
import csv
import json
# 네이버 클라우드 플랫폼에서 발급받은 클라이언트 아이디와 시크릿
client_id = "CLIENT-ID"
client_secret = "CLIENT-SECRET"
# API 엔드포인트 URL
url = "https://naveropenapi.apigw.ntruss.com/text-summary/v1/summarize"
# 요청 헤더 설정
headers = {
"X-NCP-APIGW-API-KEY-ID": client_id,
"X-NCP-APIGW-API-KEY": client_secret,
"Content-Type": "application/json"
}
# Content만 빼서 json형태로 바꾸는 함수 지정
def to_json_and_summary_comments(csv_path):
data_contents=pd.read_csv(csv_path)
data_contents=data_contents['본문']
data_contents.to_json('comments.json', orient = 'index', indent = 4)
json_path='comments.json'
with open(json_path, 'r') as f:
json_data = json.load(f)
summarized_data = []
for i in range(len(json_data)):
index=i
specific_row = json_data[str(index)]
data = {
"document": {
"content": specific_row
},
"option": {
"language": "ko",
"model": "general",
"tone": 0,
"summaryCount": 3
}
}
# POST 요청 보내기
response = requests.post(url, headers=headers, json=data)
# 응답 처리
if response.status_code == 200:
summary_result = response.json()
summarized_text = summary_result.get("summary", "") # 요약 결과에서 실제 요약 텍스트 추출 (필요에 따라 수정)
summarized_data.append({"Text": summarized_text})
else:
print("요청 실패: 상태 코드", response.status_code)
print("응답 내용:", response.text)
df_summarized = pd.DataFrame(summarized_data)
with open('comments_summary.json', 'w', encoding='utf-8') as f:
json.dump(df_summarized.to_dict(orient='records'), f, ensure_ascii=False, indent=4)
to_json_and_summary_comments('news_summary/comments_many_news_content.csv')