Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import requests
|
2 |
from bs4 import BeautifulSoup
|
3 |
-
import fitz
|
4 |
import os
|
5 |
import openai
|
6 |
import re
|
@@ -22,35 +22,40 @@ def extract_text_from_pdf(pdf_path):
|
|
22 |
text += page.get_text()
|
23 |
return text
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def summarize_paper(paper_id):
|
26 |
"""論文IDを基に論文の内容を日本語で要約する。"""
|
|
|
|
|
|
|
|
|
27 |
paper_url = f"https://arxiv.org/pdf/{paper_id}.pdf"
|
28 |
pdf_path = download_paper(paper_url)
|
29 |
text = extract_text_from_pdf(pdf_path)
|
30 |
-
summary = summarize_text_with_chat(text)
|
31 |
os.remove(pdf_path) # 一時ファイルを削除
|
32 |
-
return summary
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
response = requests.get(url)
|
37 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
38 |
-
# パターンの開始(^)と終了($)を指定して、完全一致を検出
|
39 |
-
pattern = re.compile(r'^/papers/\d+\.\d+$')
|
40 |
-
links = []
|
41 |
-
for a in soup.find_all('a', href=True):
|
42 |
-
href = a['href']
|
43 |
-
if pattern.match(href) and href not in links:
|
44 |
-
links.append(href)
|
45 |
-
return links
|
46 |
|
47 |
def summarize_text_with_chat(text, max_length=10000):
|
48 |
"""テキストをOpenAIのChat APIを使用して要約する。"""
|
49 |
openai.api_key = os.getenv('OPEN_AI_API_KEYS')
|
50 |
-
|
51 |
-
# テキストを指定の最大長に制限
|
52 |
trimmed_text = text[:max_length]
|
53 |
-
|
54 |
response = openai.chat.completions.create(
|
55 |
model="gpt-4-0125-preview",
|
56 |
messages=[
|
@@ -60,42 +65,11 @@ def summarize_text_with_chat(text, max_length=10000):
|
|
60 |
temperature=0.7,
|
61 |
max_tokens=1000
|
62 |
)
|
63 |
-
|
64 |
summary_text = response.choices[0].message.content
|
65 |
total_token = response.usage.total_tokens
|
66 |
return summary_text, total_token
|
67 |
|
68 |
-
|
69 |
-
papers_url = 'https://huggingface.co/papers' # デフォルトURL
|
70 |
-
paper_links = fetch_paper_links(papers_url)
|
71 |
-
paper_ids = set(link.split('/')[-1] for link in paper_links)
|
72 |
-
|
73 |
-
total_tokens_used = 0
|
74 |
-
summaries = []
|
75 |
-
|
76 |
-
for paper_id in paper_ids:
|
77 |
-
summary_info = ""
|
78 |
-
try:
|
79 |
-
summary, tokens_used = summarize_paper(paper_id)
|
80 |
-
total_tokens_used += tokens_used
|
81 |
-
paper_id_url = f"https://arxiv.org/pdf/{paper_id}.pdf"
|
82 |
-
summary_info += f'論文: {paper_id_url}\n{summary}\n'
|
83 |
-
except Exception as e:
|
84 |
-
summary_info += f"Error processing paper ID {paper_id}: {e}\n"
|
85 |
-
|
86 |
-
summaries.append(summary_info)
|
87 |
-
|
88 |
-
summaries_markdown = "\n---\n".join(summaries) # 要約を水平線で区切る
|
89 |
-
return summaries_markdown + f"\n全ての要約で使用されたトータルトークン数: {total_tokens_used}"
|
90 |
-
|
91 |
-
# Gradioインターフェースの設定
|
92 |
-
iface = gr.Interface(
|
93 |
-
fn=gradio_interface,
|
94 |
-
inputs=[], # 入力部分を削除
|
95 |
-
outputs=gr.Markdown(),
|
96 |
-
title="論文要約ツール",
|
97 |
-
description="[Daily Papers](https://huggingface.co/papers)に掲載された本日の論文を取得し、日本語で要約します。"
|
98 |
-
)
|
99 |
|
100 |
if __name__ == "__main__":
|
101 |
-
iface.launch()
|
|
|
1 |
import requests
|
2 |
from bs4 import BeautifulSoup
|
3 |
+
import fitz # pip install PyMuPDF
|
4 |
import os
|
5 |
import openai
|
6 |
import re
|
|
|
22 |
text += page.get_text()
|
23 |
return text
|
24 |
|
25 |
+
def check_summary_exists(paper_id):
|
26 |
+
"""指定した論文IDの要約が既に存在するか確認し、存在する場合はその内容を返す。"""
|
27 |
+
summary_path = f"summaries/{paper_id}.txt"
|
28 |
+
if os.path.exists(summary_path):
|
29 |
+
with open(summary_path, 'r', encoding='utf-8') as file:
|
30 |
+
return file.read()
|
31 |
+
return None
|
32 |
+
|
33 |
+
def save_summary(paper_id, summary):
|
34 |
+
"""指定した論文IDの要約をファイルに保存する。"""
|
35 |
+
os.makedirs('summaries', exist_ok=True)
|
36 |
+
summary_path = f"summaries/{paper_id}.txt"
|
37 |
+
with open(summary_path, 'w', encoding='utf-8') as file:
|
38 |
+
file.write(summary)
|
39 |
+
|
40 |
def summarize_paper(paper_id):
|
41 |
"""論文IDを基に論文の内容を日本語で要約する。"""
|
42 |
+
existing_summary = check_summary_exists(paper_id)
|
43 |
+
if existing_summary:
|
44 |
+
return existing_summary, 0 # トークン使用量を0として返す
|
45 |
+
|
46 |
paper_url = f"https://arxiv.org/pdf/{paper_id}.pdf"
|
47 |
pdf_path = download_paper(paper_url)
|
48 |
text = extract_text_from_pdf(pdf_path)
|
49 |
+
summary, tokens_used = summarize_text_with_chat(text)
|
50 |
os.remove(pdf_path) # 一時ファイルを削除
|
|
|
51 |
|
52 |
+
save_summary(paper_id, summary) # 新しい要約を保存
|
53 |
+
return summary, tokens_used
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
def summarize_text_with_chat(text, max_length=10000):
|
56 |
"""テキストをOpenAIのChat APIを使用して要約する。"""
|
57 |
openai.api_key = os.getenv('OPEN_AI_API_KEYS')
|
|
|
|
|
58 |
trimmed_text = text[:max_length]
|
|
|
59 |
response = openai.chat.completions.create(
|
60 |
model="gpt-4-0125-preview",
|
61 |
messages=[
|
|
|
65 |
temperature=0.7,
|
66 |
max_tokens=1000
|
67 |
)
|
|
|
68 |
summary_text = response.choices[0].message.content
|
69 |
total_token = response.usage.total_tokens
|
70 |
return summary_text, total_token
|
71 |
|
72 |
+
# Gradioインターフェースの設定とその他の関数は変更なし
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
+
iface.launch()
|