Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,320 +1,335 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import fitz # PyMuPDF
|
| 7 |
|
| 8 |
-
# =====================================
|
| 9 |
-
#
|
| 10 |
-
# =====================================
|
|
|
|
| 11 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
client = genai.Client(api_key=API_KEY)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
NOTE_FILE = os.path.join(PDF_DIR, "research_notes.txt")
|
| 17 |
|
| 18 |
-
os.
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
def load_pdf_text(file_path):
|
| 23 |
text = ""
|
|
|
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
text += page.get_text()
|
| 28 |
-
return text
|
| 29 |
-
except Exception as e:
|
| 30 |
-
print(f"Error loading {file_path}: {e}")
|
| 31 |
-
return ""
|
| 32 |
|
| 33 |
-
def update_knowledge_base():
|
| 34 |
-
global knowledge_base
|
| 35 |
-
try:
|
| 36 |
-
pdf_files = [f for f in os.listdir(PDF_DIR) if f.lower().endswith('.pdf') and os.path.isfile(os.path.join(PDF_DIR, f))]
|
| 37 |
-
except:
|
| 38 |
-
pdf_files = []
|
| 39 |
-
|
| 40 |
-
combined_text = ""
|
| 41 |
-
file_list_str = "【読み込み済みの論文一覧】\n"
|
| 42 |
-
|
| 43 |
-
if not pdf_files:
|
| 44 |
-
file_list_str += "・なし (論文PDFをアップロードしてください)\n"
|
| 45 |
-
|
| 46 |
-
for pdf in pdf_files:
|
| 47 |
-
file_list_str += f"・{pdf}\n"
|
| 48 |
-
combined_text += f"\n--- 論文: {pdf} ---\n"
|
| 49 |
-
combined_text += load_pdf_text(os.path.join(PDF_DIR, pdf))
|
| 50 |
-
|
| 51 |
-
knowledge_base = combined_text
|
| 52 |
-
|
| 53 |
-
warning = ""
|
| 54 |
-
if len(knowledge_base) > 200000:
|
| 55 |
-
warning = "\n⚠️ 注意: 読み込まれたテキスト量が非常に大きいため、回答の生成に時間がかかったり、API制限に達する可能性があります。"
|
| 56 |
-
|
| 57 |
-
status = f"現在、{len(pdf_files)} 個の論文PDFが読み込まれています。{warning}\n\n{file_list_str}"
|
| 58 |
-
return status
|
| 59 |
-
|
| 60 |
-
def clear_database():
|
| 61 |
-
"""データベースフォルダ内のPDFファイルをすべて削除して初期化する関数"""
|
| 62 |
-
try:
|
| 63 |
-
pdf_files = [f for f in os.listdir(PDF_DIR) if f.lower().endswith('.pdf') and os.path.isfile(os.path.join(PDF_DIR, f))]
|
| 64 |
-
for pdf in pdf_files:
|
| 65 |
-
os.remove(os.path.join(PDF_DIR, pdf))
|
| 66 |
except Exception as e:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
def
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
else:
|
| 112 |
-
mode_instruction = (
|
| 113 |
-
"あなたは研究をサポートする優秀なAIアシスタントです。提供された論文資料に基づき、"
|
| 114 |
-
"「実験設定、データセット、評価指標、結果、比較グラフの内容、考察」に特化して詳しく分析・回答してください。"
|
| 115 |
-
)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
ただし、資料に具体的な記述がない内容に関しては、必ず「提供された論文資料に該当する情報が見つからないため、一般的な知識に基づきGeminiが回答します。」と前置きした上で回答してください。
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
"""
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
)
|
| 164 |
-
else:
|
| 165 |
-
yield f"エラーが発生しました: {error_msg}"
|
| 166 |
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
def upload_new_papers(file_objects):
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
PDF_DIR,
|
| 184 |
-
filename
|
| 185 |
-
)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
def prepare_exp_download(history):
|
| 202 |
-
if not history: return None
|
| 203 |
-
return _generate_file(history, "実験内容・結果")
|
| 204 |
-
|
| 205 |
-
def _generate_file(history, mode_label):
|
| 206 |
-
"""共通のテキストファイル生成ロジック"""
|
| 207 |
-
custom_filename = f"chat_history_{mode_label}.txt"
|
| 208 |
-
|
| 209 |
-
temp_dir = tempfile.gettempdir()
|
| 210 |
-
file_path = os.path.join(temp_dir, custom_filename)
|
| 211 |
-
|
| 212 |
-
with open(file_path, "w", encoding="utf-8") as f:
|
| 213 |
-
f.write(f"==================================================\n")
|
| 214 |
-
f.write(f" 対話履歴ログ: {mode_label}\n")
|
| 215 |
-
f.write(f"==================================================\n\n")
|
| 216 |
-
|
| 217 |
-
for msg in history:
|
| 218 |
-
role_label = "【ユーザー】" if msg["role"] == "user" else "【AIアシスタント】"
|
| 219 |
-
pure_text = extract_pure_text(msg["content"])
|
| 220 |
-
f.write(f"{role_label}\n{pure_text}\n")
|
| 221 |
-
f.write("-" * 50 + "\n\n")
|
| 222 |
-
|
| 223 |
-
return file_path
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
# =====================================================================
|
| 227 |
-
# 4. Gradio UIの構築
|
| 228 |
-
# =====================================================================
|
| 229 |
-
with gr.Blocks(title="研究専用RAGアシスタント") as demo:
|
| 230 |
-
gr.Markdown("# 🎓 研究専用RAGアシスタント")
|
| 231 |
-
gr.Markdown("論文PDFの内容をベースに、Geminiがあなたの研究・論文読解をサポートします。")
|
| 232 |
-
|
| 233 |
-
with gr.Accordion("📚 論文データベース管理 (PDF追加・初期化)", open=True):
|
| 234 |
-
with gr.Row():
|
| 235 |
-
file_input = gr.File(label="論文PDFをアップロード", file_count="multiple", file_types=[".pdf"])
|
| 236 |
-
with gr.Column():
|
| 237 |
-
upload_btn = gr.Button("📂 選択した論文を追加", variant="primary")
|
| 238 |
-
clear_db_btn = gr.Button("🗑️ データベースを全削除して初期化", variant="stop")
|
| 239 |
-
status_output = gr.Textbox(value=init_status, label="現在のデータベースステータス", interactive=False, lines=6)
|
| 240 |
-
|
| 241 |
-
upload_btn.click(fn=upload_new_papers, inputs=file_input, outputs=[status_output, file_input])
|
| 242 |
-
clear_db_btn.click(fn=clear_database, inputs=None, outputs=[status_output, file_input])
|
| 243 |
-
|
| 244 |
-
gr.Markdown("### 🔍 論文調査")
|
| 245 |
-
|
| 246 |
-
with gr.Tabs():
|
| 247 |
-
# タブ1: 専門用語・手法用
|
| 248 |
-
with gr.TabItem("📖 専門用語・手法を調べる"):
|
| 249 |
-
gr.Markdown("*論文内の見慣れないキーワードや、提案手法のアルゴリズム、理論について質問してください。*")
|
| 250 |
-
chatbot_terms = gr.Chatbot(label="対話履歴(用語・手法)", elem_id="terms_chat")
|
| 251 |
-
msg_terms = gr.Textbox(placeholder="例: この論文で使われている○○という手法のメリットは何ですか?", label="質問を入力")
|
| 252 |
-
|
| 253 |
-
with gr.Row():
|
| 254 |
-
save_terms_btn = gr.Button("💾 対話履歴を保存 (TXT)", variant="primary")
|
| 255 |
-
clear_terms = gr.Button("🗑️ 対話履歴をクリア")
|
| 256 |
-
|
| 257 |
-
# 🌟修正:最初から visible=True (常に画面に枠を表示)
|
| 258 |
-
file_download_terms = gr.File(label="ダウンロード(保存ボタンを押すとここにファイルが出ます)", visible=True)
|
| 259 |
-
|
| 260 |
-
def user_terms(user_message, history):
|
| 261 |
-
return "", history + [{"role": "user", "content": user_message}]
|
| 262 |
-
|
| 263 |
-
def bot_terms(history):
|
| 264 |
-
user_msg = history[-1]["content"]
|
| 265 |
-
history_for_api = history[:-1]
|
| 266 |
-
history.append({"role": "assistant", "content": ""})
|
| 267 |
-
|
| 268 |
-
for current_response in rag_chat_stream(user_msg, history_for_api, mode="terms"):
|
| 269 |
-
history[-1]["content"] = current_response
|
| 270 |
-
yield history
|
| 271 |
-
|
| 272 |
-
msg_terms.submit(user_terms, [msg_terms, chatbot_terms], [msg_terms, chatbot_terms], queue=False).then(
|
| 273 |
-
bot_terms, chatbot_terms, chatbot_terms
|
| 274 |
)
|
| 275 |
-
clear_terms.click(lambda: ([], None), None, [chatbot_terms, file_download_terms], queue=False)
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
inputs=chatbot_terms,
|
| 280 |
-
outputs=file_download_terms
|
| 281 |
)
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
gr.Markdown("*論文内の実験環境、データセット、グラフの解釈、評価結果について質問してください。*")
|
| 286 |
-
chatbot_exp = gr.Chatbot(label="対話履歴(実験内容)", elem_id="exp_chat")
|
| 287 |
-
msg_exp = gr.Textbox(placeholder="例: 実験で使用されたデータセットの規模と、Baselineとの精度の差は?", label="質問を入力")
|
| 288 |
-
|
| 289 |
-
with gr.Row():
|
| 290 |
-
save_exp_btn = gr.Button("💾 対話履歴を保存 (TXT)", variant="primary")
|
| 291 |
-
clear_exp = gr.Button("🗑️ 対話履歴をクリア")
|
| 292 |
-
|
| 293 |
-
# 🌟修正:最初から visible=True (常に画面に枠を表示)
|
| 294 |
-
file_download_exp = gr.File(label="ダウンロード(保存ボタンを押すとここにファイルが出ます)", visible=True)
|
| 295 |
-
|
| 296 |
-
def user_exp(user_message, history):
|
| 297 |
-
return "", history + [{"role": "user", "content": user_message}]
|
| 298 |
-
|
| 299 |
-
def bot_exp(history):
|
| 300 |
-
user_msg = history[-1]["content"]
|
| 301 |
-
history_for_api = history[:-1]
|
| 302 |
-
history.append({"role": "assistant", "content": ""})
|
| 303 |
-
|
| 304 |
-
for current_response in rag_chat_stream(user_msg, history_for_api, mode="experiments"):
|
| 305 |
-
history[-1]["content"] = current_response
|
| 306 |
-
yield history
|
| 307 |
-
|
| 308 |
-
msg_exp.submit(user_exp, [msg_exp, chatbot_exp], [msg_exp, chatbot_exp], queue=False).then(
|
| 309 |
-
bot_exp, chatbot_exp, chatbot_exp
|
| 310 |
)
|
| 311 |
-
clear_exp.click(lambda: ([], None), None, [chatbot_exp, file_download_exp], queue=False)
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
inputs=
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
| 317 |
)
|
| 318 |
|
| 319 |
if __name__ == "__main__":
|
| 320 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
|
| 4 |
+
import fitz
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
from google import genai
|
| 8 |
from google.genai import types
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# =====================================
|
| 11 |
+
# Gemini
|
| 12 |
+
# =====================================
|
| 13 |
+
|
| 14 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 15 |
+
|
| 16 |
+
if not API_KEY:
|
| 17 |
+
raise ValueError("GEMINI_API_KEY が設定されていません")
|
| 18 |
+
|
| 19 |
client = genai.Client(api_key=API_KEY)
|
| 20 |
|
| 21 |
+
# =====================================
|
| 22 |
+
# Folder
|
| 23 |
+
# =====================================
|
| 24 |
+
|
| 25 |
+
PDF_FOLDER = "papers"
|
| 26 |
|
| 27 |
+
os.makedirs(PDF_FOLDER, exist_ok=True)
|
|
|
|
| 28 |
|
| 29 |
+
MEMO_FILE = os.path.join(
|
| 30 |
+
PDF_FOLDER,
|
| 31 |
+
"research_notes.txt"
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
# =====================================
|
| 35 |
+
# PDF読み込み
|
| 36 |
+
# =====================================
|
| 37 |
+
|
| 38 |
+
def extract_pdf_text(filepath):
|
| 39 |
|
|
|
|
| 40 |
text = ""
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
+
|
| 44 |
+
pdf = fitz.open(filepath)
|
| 45 |
+
|
| 46 |
+
for page in pdf:
|
| 47 |
text += page.get_text()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
+
|
| 51 |
+
print(e)
|
| 52 |
+
|
| 53 |
+
return text
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# =====================================
|
| 57 |
+
# 論文DB
|
| 58 |
+
# =====================================
|
| 59 |
+
|
| 60 |
+
def load_papers():
|
| 61 |
+
|
| 62 |
+
texts = []
|
| 63 |
+
|
| 64 |
+
files = []
|
| 65 |
+
|
| 66 |
+
for file in os.listdir(PDF_FOLDER):
|
| 67 |
+
|
| 68 |
+
if file.lower().endswith(".pdf"):
|
| 69 |
+
|
| 70 |
+
files.append(file)
|
| 71 |
+
|
| 72 |
+
path = os.path.join(
|
| 73 |
+
PDF_FOLDER,
|
| 74 |
+
file
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
paper_text = extract_pdf_text(path)
|
| 78 |
+
|
| 79 |
+
texts.append(
|
| 80 |
+
f"\n\n### {file}\n{paper_text}"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
return files, "\n".join(texts)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# =====================================
|
| 87 |
+
# 論文一覧
|
| 88 |
+
# =====================================
|
| 89 |
+
|
| 90 |
+
def show_paper_list():
|
| 91 |
+
|
| 92 |
+
files, _ = load_papers()
|
| 93 |
+
|
| 94 |
+
if len(files) == 0:
|
| 95 |
+
|
| 96 |
+
return "登録論文なし"
|
| 97 |
+
|
| 98 |
+
return "\n".join(files)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# =====================================
|
| 102 |
+
# PDF追加
|
| 103 |
+
# =====================================
|
| 104 |
+
|
| 105 |
+
def add_pdfs(files):
|
| 106 |
+
|
| 107 |
+
if files is None:
|
| 108 |
+
|
| 109 |
+
return "PDF未選択"
|
| 110 |
+
|
| 111 |
+
count = 0
|
| 112 |
+
|
| 113 |
+
for file in files:
|
| 114 |
+
|
| 115 |
+
dst = os.path.join(
|
| 116 |
+
PDF_FOLDER,
|
| 117 |
+
os.path.basename(file.name)
|
| 118 |
)
|
| 119 |
+
|
| 120 |
+
with open(file.name, "rb") as src:
|
| 121 |
+
|
| 122 |
+
with open(dst, "wb") as out:
|
| 123 |
+
|
| 124 |
+
out.write(src.read())
|
| 125 |
+
|
| 126 |
+
count += 1
|
| 127 |
+
|
| 128 |
+
return f"{count}件登録完了"
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# =====================================
|
| 132 |
+
# Gemini質問
|
| 133 |
+
# =====================================
|
| 134 |
+
|
| 135 |
+
def ask_paper(question, mode):
|
| 136 |
+
|
| 137 |
+
files, knowledge = load_papers()
|
| 138 |
+
|
| 139 |
+
if len(files) == 0:
|
| 140 |
+
|
| 141 |
+
return "論文が登録されていません"
|
| 142 |
+
|
| 143 |
+
if mode == "手法":
|
| 144 |
+
|
| 145 |
+
instruction = """
|
| 146 |
+
論文中の手法について説明してください。
|
| 147 |
+
|
| 148 |
+
以下を含めること
|
| 149 |
+
|
| 150 |
+
・概要
|
| 151 |
+
・特徴
|
| 152 |
+
・利点
|
| 153 |
+
・数式
|
| 154 |
+
・初心者向け解説
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
elif mode == "実験":
|
| 158 |
+
|
| 159 |
+
instruction = """
|
| 160 |
+
論文中の実験内容を整理してください。
|
| 161 |
+
|
| 162 |
+
以下を抽出
|
| 163 |
+
|
| 164 |
+
・データセット
|
| 165 |
+
・評価指標
|
| 166 |
+
・比較手法
|
| 167 |
+
・結果
|
| 168 |
+
・考察
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
instruction = """
|
| 174 |
+
論文内容を要約してください。
|
|
|
|
| 175 |
|
| 176 |
+
・研究背景
|
| 177 |
+
・目的
|
| 178 |
+
・提案手法
|
| 179 |
+
・結果
|
| 180 |
+
・今後の課題
|
| 181 |
"""
|
| 182 |
|
| 183 |
+
prompt = f"""
|
| 184 |
+
{instruction}
|
| 185 |
+
|
| 186 |
+
【論文情報】
|
| 187 |
+
|
| 188 |
+
{knowledge[:120000]}
|
| 189 |
+
|
| 190 |
+
【質問】
|
| 191 |
+
|
| 192 |
+
{question}
|
| 193 |
+
"""
|
| 194 |
+
|
| 195 |
+
response = client.models.generate_content(
|
| 196 |
+
model="gemini-2.5-flash",
|
| 197 |
+
contents=prompt
|
| 198 |
)
|
| 199 |
|
| 200 |
+
return response.text
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# =====================================
|
| 204 |
+
# メモ保存
|
| 205 |
+
# =====================================
|
| 206 |
+
|
| 207 |
+
def save_note(question, answer):
|
| 208 |
+
|
| 209 |
+
now = datetime.now().strftime(
|
| 210 |
+
"%Y-%m-%d %H:%M:%S"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
with open(
|
| 214 |
+
MEMO_FILE,
|
| 215 |
+
"a",
|
| 216 |
+
encoding="utf-8"
|
| 217 |
+
) as f:
|
| 218 |
+
|
| 219 |
+
f.write("\n")
|
| 220 |
+
f.write("=" * 60)
|
| 221 |
+
f.write("\n")
|
| 222 |
+
|
| 223 |
+
f.write(f"{now}\n\n")
|
| 224 |
+
|
| 225 |
+
f.write("質問\n")
|
| 226 |
+
f.write(question)
|
| 227 |
+
|
| 228 |
+
f.write("\n\n")
|
| 229 |
+
|
| 230 |
+
f.write("回答\n")
|
| 231 |
+
f.write(answer)
|
| 232 |
+
|
| 233 |
+
f.write("\n")
|
| 234 |
+
|
| 235 |
+
return "保存完了"
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
# =====================================
|
| 239 |
+
# UI
|
| 240 |
+
# =====================================
|
| 241 |
+
|
| 242 |
+
with gr.Blocks() as app:
|
| 243 |
+
|
| 244 |
+
gr.Markdown("# 研究論文サポートシステム")
|
| 245 |
+
|
| 246 |
+
with gr.Row():
|
| 247 |
+
|
| 248 |
+
with gr.Column():
|
| 249 |
+
|
| 250 |
+
upload_files = gr.File(
|
| 251 |
+
file_count="multiple",
|
| 252 |
+
file_types=[".pdf"],
|
| 253 |
+
label="論文追加"
|
| 254 |
)
|
|
|
|
|
|
|
| 255 |
|
| 256 |
+
upload_btn = gr.Button(
|
| 257 |
+
"PDF登録"
|
| 258 |
+
)
|
| 259 |
|
| 260 |
+
upload_result = gr.Textbox(
|
| 261 |
+
label="状態"
|
| 262 |
+
)
|
|
|
|
| 263 |
|
| 264 |
+
upload_btn.click(
|
| 265 |
+
add_pdfs,
|
| 266 |
+
inputs=upload_files,
|
| 267 |
+
outputs=upload_result
|
| 268 |
+
)
|
| 269 |
|
| 270 |
+
paper_list = gr.Textbox(
|
| 271 |
+
label="登録論文"
|
| 272 |
+
)
|
| 273 |
|
| 274 |
+
refresh_btn = gr.Button(
|
| 275 |
+
"論文一覧更新"
|
| 276 |
+
)
|
| 277 |
|
| 278 |
+
refresh_btn.click(
|
| 279 |
+
show_paper_list,
|
| 280 |
+
outputs=paper_list
|
| 281 |
+
)
|
| 282 |
|
| 283 |
+
with gr.Column():
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
+
mode = gr.Radio(
|
| 286 |
+
choices=[
|
| 287 |
+
"手法",
|
| 288 |
+
"実験",
|
| 289 |
+
"要約"
|
| 290 |
+
],
|
| 291 |
+
value="手法",
|
| 292 |
+
label="解析モード"
|
| 293 |
+
)
|
| 294 |
|
| 295 |
+
question = gr.Textbox(
|
| 296 |
+
label="質問"
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
answer = gr.Textbox(
|
| 300 |
+
label="回答",
|
| 301 |
+
lines=20
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
search_btn = gr.Button(
|
| 305 |
+
"解析開始"
|
| 306 |
+
)
|
| 307 |
|
| 308 |
+
search_btn.click(
|
| 309 |
+
ask_paper,
|
| 310 |
+
inputs=[
|
| 311 |
+
question,
|
| 312 |
+
mode
|
| 313 |
+
],
|
| 314 |
+
outputs=answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
)
|
|
|
|
| 316 |
|
| 317 |
+
save_btn = gr.Button(
|
| 318 |
+
"回答保存"
|
|
|
|
|
|
|
| 319 |
)
|
| 320 |
|
| 321 |
+
save_status = gr.Textbox(
|
| 322 |
+
label="保存状態"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
)
|
|
|
|
| 324 |
|
| 325 |
+
save_btn.click(
|
| 326 |
+
save_note,
|
| 327 |
+
inputs=[
|
| 328 |
+
question,
|
| 329 |
+
answer
|
| 330 |
+
],
|
| 331 |
+
outputs=save_status
|
| 332 |
)
|
| 333 |
|
| 334 |
if __name__ == "__main__":
|
| 335 |
+
app.launch()
|