Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@
|
|
2 |
import gradio as gr
|
3 |
import logging
|
4 |
|
5 |
-
# ==== ① 向量检索 & LLM ====
|
6 |
from langchain_community.vectorstores import Chroma
|
7 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
8 |
from langchain.chains import RetrievalQA
|
@@ -11,7 +10,7 @@ from langchain.llms import HuggingFacePipeline
|
|
11 |
|
12 |
logging.basicConfig(level=logging.INFO)
|
13 |
|
14 |
-
#
|
15 |
embedding_model = HuggingFaceEmbeddings(
|
16 |
model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
|
17 |
)
|
@@ -20,8 +19,7 @@ vector_store = Chroma(
|
|
20 |
embedding_function=embedding_model,
|
21 |
)
|
22 |
|
23 |
-
#
|
24 |
-
# 如果 7B 跑不动,可以先用 openchat-mini 试试
|
25 |
model_id = "openchat/openchat-3.5-0106"
|
26 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
27 |
model = AutoModelForCausalLM.from_pretrained(
|
@@ -39,17 +37,15 @@ gen_pipe = pipeline(
|
|
39 |
)
|
40 |
llm = HuggingFacePipeline(pipeline=gen_pipe)
|
41 |
|
42 |
-
#
|
43 |
qa_chain = RetrievalQA.from_chain_type(
|
44 |
llm=llm,
|
45 |
chain_type="stuff",
|
46 |
retriever=vector_store.as_retriever(search_kwargs={"k": 3}),
|
47 |
)
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
def simple_qa(user_query):
|
52 |
-
"""智能问答:检索 + 生成(单轮演示版)"""
|
53 |
if not user_query.strip():
|
54 |
return "⚠️ 请输入学习问题,例如:什么是定积分?"
|
55 |
try:
|
@@ -59,45 +55,57 @@ def simple_qa(user_query):
|
|
59 |
logging.error(f"问答失败: {e}")
|
60 |
return "抱歉,暂时无法回答,请稍后再试。"
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
def placeholder_fn(*args, **kwargs):
|
63 |
return "功能尚未实现,请等待后续更新。"
|
64 |
|
|
|
65 |
with gr.Blocks() as demo:
|
66 |
gr.Markdown("# 智能学习助手 v2.0\n— 大学生专业课学习助手 —")
|
67 |
|
68 |
with gr.Tabs():
|
69 |
-
#
|
70 |
with gr.TabItem("智能问答"):
|
71 |
-
gr.Markdown(">
|
72 |
chatbot = gr.Chatbot()
|
73 |
user_msg = gr.Textbox(placeholder="输入您的学习问题,然后按回车或点击发送")
|
74 |
send_btn = gr.Button("发送")
|
75 |
|
76 |
-
# 单轮:把问答显示在 Chatbot
|
77 |
def update_chat(message, chat_history):
|
78 |
reply = simple_qa(message)
|
79 |
chat_history.append((message, reply))
|
80 |
return "", chat_history
|
81 |
|
82 |
-
send_btn.click(
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
)
|
87 |
-
user_msg.submit(
|
88 |
-
fn=update_chat,
|
89 |
-
inputs=[user_msg, chatbot],
|
90 |
-
outputs=[user_msg, chatbot],
|
91 |
-
)
|
92 |
-
|
93 |
-
# ---------- 生成学习大纲 ----------
|
94 |
with gr.TabItem("生成学习大纲"):
|
95 |
-
gr.Markdown("
|
96 |
-
topic_input = gr.Textbox(label="
|
|
|
97 |
gen_outline_btn = gr.Button("生成大纲")
|
98 |
-
gen_outline_btn.click(
|
99 |
|
100 |
-
#
|
101 |
with gr.TabItem("自动出题"):
|
102 |
gr.Markdown("(出题模块,待开发)")
|
103 |
topic2 = gr.Textbox(label="知识点/主题", placeholder="如:高数 第三章 多元函数")
|
@@ -106,7 +114,7 @@ with gr.Blocks() as demo:
|
|
106 |
gen_q_btn = gr.Button("开始出题")
|
107 |
gen_q_btn.click(placeholder_fn, inputs=[topic2, difficulty2, count2], outputs=topic2)
|
108 |
|
109 |
-
#
|
110 |
with gr.TabItem("答案批改"):
|
111 |
gr.Markdown("(批改模块,待开发)")
|
112 |
std_ans = gr.Textbox(label="标准答案", lines=5)
|
|
|
2 |
import gradio as gr
|
3 |
import logging
|
4 |
|
|
|
5 |
from langchain_community.vectorstores import Chroma
|
6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
from langchain.chains import RetrievalQA
|
|
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
13 |
+
# === 加载向量库 ===
|
14 |
embedding_model = HuggingFaceEmbeddings(
|
15 |
model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
|
16 |
)
|
|
|
19 |
embedding_function=embedding_model,
|
20 |
)
|
21 |
|
22 |
+
# === 加载 LLM 模型(openchat) ===
|
|
|
23 |
model_id = "openchat/openchat-3.5-0106"
|
24 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
25 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
37 |
)
|
38 |
llm = HuggingFacePipeline(pipeline=gen_pipe)
|
39 |
|
40 |
+
# === 构建问答链 ===
|
41 |
qa_chain = RetrievalQA.from_chain_type(
|
42 |
llm=llm,
|
43 |
chain_type="stuff",
|
44 |
retriever=vector_store.as_retriever(search_kwargs={"k": 3}),
|
45 |
)
|
46 |
|
47 |
+
# === 智能问答函数 ===
|
|
|
48 |
def simple_qa(user_query):
|
|
|
49 |
if not user_query.strip():
|
50 |
return "⚠️ 请输入学习问题,例如:什么是定积分?"
|
51 |
try:
|
|
|
55 |
logging.error(f"问答失败: {e}")
|
56 |
return "抱歉,暂时无法回答,请稍后再试。"
|
57 |
|
58 |
+
# === 大纲生成函数 ===
|
59 |
+
def generate_outline(topic: str):
|
60 |
+
if not topic.strip():
|
61 |
+
return "⚠️ 请输入章节或主题,例如:高等数学 第六章 定积分"
|
62 |
+
try:
|
63 |
+
docs = vector_store.as_retriever(search_kwargs={"k": 3}).get_relevant_documents(topic)
|
64 |
+
snippet = "\n".join([doc.page_content for doc in docs])
|
65 |
+
prompt = (
|
66 |
+
f"根据以下内容,为“{topic}”生成大学本科层次的结构化学习大纲,格式如下:\n"
|
67 |
+
f"一、章节标题\n 1. 节标题\n (1)要点描述\n...\n\n"
|
68 |
+
f"文档内容:\n{snippet}\n\n学习大纲:"
|
69 |
+
)
|
70 |
+
result = llm.generate(prompt).generations[0][0].text.strip()
|
71 |
+
return result
|
72 |
+
except Exception as e:
|
73 |
+
logging.error(f"大纲生成失败: {e}")
|
74 |
+
return "⚠️ 抱歉,生成失败,请稍后再试。"
|
75 |
+
|
76 |
+
# === 占位函数 ===
|
77 |
def placeholder_fn(*args, **kwargs):
|
78 |
return "功能尚未实现,请等待后续更新。"
|
79 |
|
80 |
+
# === Gradio UI ===
|
81 |
with gr.Blocks() as demo:
|
82 |
gr.Markdown("# 智能学习助手 v2.0\n— 大学生专业课学习助手 —")
|
83 |
|
84 |
with gr.Tabs():
|
85 |
+
# --- 模块 A:智能问答 ---
|
86 |
with gr.TabItem("智能问答"):
|
87 |
+
gr.Markdown("> 示例:什么是函数的定义域?")
|
88 |
chatbot = gr.Chatbot()
|
89 |
user_msg = gr.Textbox(placeholder="输入您的学习问题,然后按回车或点击发送")
|
90 |
send_btn = gr.Button("发送")
|
91 |
|
|
|
92 |
def update_chat(message, chat_history):
|
93 |
reply = simple_qa(message)
|
94 |
chat_history.append((message, reply))
|
95 |
return "", chat_history
|
96 |
|
97 |
+
send_btn.click(update_chat, inputs=[user_msg, chatbot], outputs=[user_msg, chatbot])
|
98 |
+
user_msg.submit(update_chat, inputs=[user_msg, chatbot], outputs=[user_msg, chatbot])
|
99 |
+
|
100 |
+
# --- 模块 B:生成学习大纲 ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
with gr.TabItem("生成学习大纲"):
|
102 |
+
gr.Markdown("> 示例:高等数学 第六章 定积分")
|
103 |
+
topic_input = gr.Textbox(label="章节主题", placeholder="请输入章节名")
|
104 |
+
outline_output = gr.Textbox(label="系统生成的大纲", lines=12)
|
105 |
gen_outline_btn = gr.Button("生成大纲")
|
106 |
+
gen_outline_btn.click(fn=generate_outline, inputs=topic_input, outputs=outline_output)
|
107 |
|
108 |
+
# --- 模块 C:自动出题(占位) ---
|
109 |
with gr.TabItem("自动出题"):
|
110 |
gr.Markdown("(出题模块,待开发)")
|
111 |
topic2 = gr.Textbox(label="知识点/主题", placeholder="如:高数 第三章 多元函数")
|
|
|
114 |
gen_q_btn = gr.Button("开始出题")
|
115 |
gen_q_btn.click(placeholder_fn, inputs=[topic2, difficulty2, count2], outputs=topic2)
|
116 |
|
117 |
+
# --- 模块 D:答案批改(占位) ---
|
118 |
with gr.TabItem("答案批改"):
|
119 |
gr.Markdown("(批改模块,待开发)")
|
120 |
std_ans = gr.Textbox(label="标准答案", lines=5)
|