Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import pdfplumber
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 推荐用环境变量管理 API Key
|
| 7 |
+
API_KEY = os.environ.get("GEMINI_API_KEY", "你的_Gemini_API_Key")
|
| 8 |
+
|
| 9 |
+
def extract_text_from_pdf(pdf_file):
|
| 10 |
+
try:
|
| 11 |
+
with pdfplumber.open(pdf_file) as pdf:
|
| 12 |
+
text = ""
|
| 13 |
+
for page in pdf.pages:
|
| 14 |
+
page_text = page.extract_text()
|
| 15 |
+
if page_text:
|
| 16 |
+
text += page_text + "\n"
|
| 17 |
+
return text.strip()
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"PDF解析失败: {str(e)}"
|
| 20 |
+
|
| 21 |
+
def call_gemini(prompt, model="gemini-2.5-flash", api_key=API_KEY):
|
| 22 |
+
genai.configure(api_key=api_key)
|
| 23 |
+
model = genai.GenerativeModel(model)
|
| 24 |
+
response = model.generate_content(prompt)
|
| 25 |
+
return response.text.strip()
|
| 26 |
+
|
| 27 |
+
def generate_outline(text, api_key=API_KEY):
|
| 28 |
+
prompt = (
|
| 29 |
+
"请根据以下内容,提炼出结构化的复习大纲,分层级展示,使用有条理的短语或句子:\n"
|
| 30 |
+
f"{text}\n"
|
| 31 |
+
"请用Markdown格式输出,主标题用#,二级用##,三级用###,不要有多余解释。"
|
| 32 |
+
)
|
| 33 |
+
return call_gemini(prompt, api_key=api_key)
|
| 34 |
+
|
| 35 |
+
def generate_qa(text, max_questions=5, api_key=API_KEY):
|
| 36 |
+
prompt = (
|
| 37 |
+
f"请根据以下内容,生成{max_questions}道典型考题及标准答案,题型以简答题为主,内容覆盖重点知识点:\n"
|
| 38 |
+
f"{text}\n"
|
| 39 |
+
"请用如下格式输出:\n"
|
| 40 |
+
"【问题】xxx\n【答案】xxx\n"
|
| 41 |
+
)
|
| 42 |
+
return call_gemini(prompt, api_key=api_key)
|
| 43 |
+
|
| 44 |
+
def generate_mindmap(text, api_key=API_KEY):
|
| 45 |
+
prompt = (
|
| 46 |
+
"请根据以下内容,生成结构化的思维导图,使用Markdown格式,主标题用#,二级用##,三级用###,"
|
| 47 |
+
"内容要分层、条理清晰,突出知识点之间的关系。只输出Markdown,不要有多余解释。\n"
|
| 48 |
+
f"{text}"
|
| 49 |
+
)
|
| 50 |
+
return call_gemini(prompt, api_key=api_key)
|
| 51 |
+
|
| 52 |
+
def create_markmap_html(markdown_content):
|
| 53 |
+
html_content = f"""
|
| 54 |
+
<!DOCTYPE html>
|
| 55 |
+
<html>
|
| 56 |
+
<head>
|
| 57 |
+
<meta charset="UTF-8">
|
| 58 |
+
<script src="https://cdn.jsdelivr.net/npm/d3@6"></script>
|
| 59 |
+
<script src="https://cdn.jsdelivr.net/npm/markmap-lib@0.15.3/dist/browser/standalone.min.js"></script>
|
| 60 |
+
</head>
|
| 61 |
+
<body>
|
| 62 |
+
<svg id="mindmap" style="width:100%;height:600px"></svg>
|
| 63 |
+
<script>
|
| 64 |
+
window.markmap.Markmap.create(document.getElementById('mindmap'), null, `{markdown_content}`);
|
| 65 |
+
</script>
|
| 66 |
+
</body>
|
| 67 |
+
</html>
|
| 68 |
+
"""
|
| 69 |
+
return html_content
|
| 70 |
+
|
| 71 |
+
st.set_page_config(page_title="PDF自动复习助手", layout="wide")
|
| 72 |
+
st.title("📚 PDF自动复习助手:大纲 + 考题 + 思维导图 (Gemini)")
|
| 73 |
+
|
| 74 |
+
uploaded_file = st.file_uploader("上传PDF文件", type=["pdf"])
|
| 75 |
+
max_questions = st.slider("考题数量", 3, 20, value=5)
|
| 76 |
+
api_key = st.text_input("Gemini API Key(建议用环境变量,不建议明文填写)", value=API_KEY, type="password")
|
| 77 |
+
|
| 78 |
+
if uploaded_file is not None:
|
| 79 |
+
with st.spinner("正在解析PDF并生成内容..."):
|
| 80 |
+
text = extract_text_from_pdf(uploaded_file)
|
| 81 |
+
if not text or text.startswith("PDF解析失败"):
|
| 82 |
+
st.error(text)
|
| 83 |
+
else:
|
| 84 |
+
outline_md = generate_outline(text, api_key)
|
| 85 |
+
qa_md = generate_qa(text, max_questions, api_key)
|
| 86 |
+
mindmap_md = generate_mindmap(text, api_key)
|
| 87 |
+
st.subheader("📝 复习大纲")
|
| 88 |
+
st.text_area("大纲 Markdown", outline_md, height=200)
|
| 89 |
+
st.subheader("📚 考题与答案")
|
| 90 |
+
st.text_area("考题 Markdown", qa_md, height=200)
|
| 91 |
+
st.subheader("🌳 思维导图 Markdown")
|
| 92 |
+
st.text_area("思维导图 Markdown", mindmap_md, height=200)
|
| 93 |
+
st.download_button("⬇️ 下载思维导图 Markdown", mindmap_md, file_name="mindmap.md", mime="text/markdown")
|
| 94 |
+
st.subheader("🌳 交互式思维导图")
|
| 95 |
+
st.components.v1.html(create_markmap_html(mindmap_md), height=700, scrolling=True)
|