ljy5946 commited on
Commit
536e921
·
verified ·
1 Parent(s): 19a294d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -25
app.py CHANGED
@@ -1,45 +1,120 @@
 
1
  import gradio as gr
 
2
 
3
- # (未来会引入更多模块,这里先写占位版)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def placeholder_fn(*args, **kwargs):
6
  return "功能尚未实现,请等待后续更新。"
7
 
8
  with gr.Blocks() as demo:
9
- gr.Markdown("# Intelligent Learning Assistant v2.0\n— 大学生专业课学习助手 —")
10
 
11
  with gr.Tabs():
 
12
  with gr.TabItem("智能问答"):
13
- gr.Markdown("(多轮对话模块,待开发)")
14
- chat = gr.Chatbot()
15
- user_msg = gr.Textbox(placeholder="在此输入您的学习问题")
16
- send_button = gr.Button("发送")
17
- send_button.click(fn=placeholder_fn, inputs=user_msg, outputs=chat)
 
 
 
 
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  with gr.TabItem("生成学习大纲"):
20
- gr.Markdown("(学习大纲生成功能,待开发)")
21
- topic_input = gr.Textbox(label="主题/章节名称", placeholder="如:线性代数 第五章 特征值与特征向量")
22
- gen_button = gr.Button("生成大纲")
23
- gen_button.click(fn=placeholder_fn, inputs=topic_input, outputs=topic_input)
24
 
 
25
  with gr.TabItem("自动出题"):
26
- gr.Markdown("(练习题生成功能,待开发)")
27
  topic2 = gr.Textbox(label="知识点/主题", placeholder="如:高数 第三章 多元函数")
28
- difficulty2 = gr.Dropdown(choices=["简单", "中等", "困难"], label="题目难度")
29
- count2 = gr.Slider(minimum=1, maximum=10, step=1, label="题目数量")
30
- gen2_button = gr.Button("开始出题")
31
- # 先用 topic2 作为占位输出
32
- gen2_button.click(fn=placeholder_fn, inputs=[topic2, difficulty2, count2], outputs=topic2)
33
 
 
34
  with gr.TabItem("答案批改"):
35
- gr.Markdown("(答案批改功能,待开发)")
36
- std_ans = gr.Textbox(label="标准答案", lines=5, placeholder="请在此粘贴标准答案")
37
- user_ans = gr.Textbox(label="您的作答", lines=5, placeholder="请在此输入您的解答")
38
- grade_button = gr.Button("开始批改")
39
- # 先用 user_ans 作为占位输出
40
- grade_button.click(fn=placeholder_fn, inputs=[user_ans, std_ans], outputs=user_ans)
41
-
42
- gr.Markdown("---\nPowered by HuggingFace • v2.0")
43
 
44
  if __name__ == "__main__":
45
  demo.launch()
 
1
+ # app.py
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
9
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
10
+ from langchain.llms import HuggingFacePipeline
11
+
12
+ logging.basicConfig(level=logging.INFO)
13
+
14
+ # --- 1) 加载本地向量库 ---
15
+ embedding_model = HuggingFaceEmbeddings(
16
+ model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
17
+ )
18
+ vector_store = Chroma(
19
+ persist_directory="vector_store",
20
+ embedding_function=embedding_model,
21
+ )
22
+
23
+ # --- 2) 加载(较轻量)LLM ---
24
+ # 如果 7B 跑不动,可以先用 openchat-mini 试试
25
+ model_id = "openchat/openchat-3.5-0106"
26
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
27
+ model = AutoModelForCausalLM.from_pretrained(
28
+ model_id,
29
+ device_map="auto",
30
+ torch_dtype="auto",
31
+ )
32
+ gen_pipe = pipeline(
33
+ "text-generation",
34
+ model=model,
35
+ tokenizer=tokenizer,
36
+ max_new_tokens=512,
37
+ temperature=0.7,
38
+ top_p=0.9,
39
+ )
40
+ llm = HuggingFacePipeline(pipeline=gen_pipe)
41
+
42
+ # --- 3) 构建 RAG 问答链 ---
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
+ # ==== ② Gradio UI ====
50
+
51
+ def simple_qa(user_query):
52
+ """智能问答:检索 + 生成(单轮演示版)"""
53
+ if not user_query.strip():
54
+ return "⚠️ 请输入学习问题,例如:什么是定积分?"
55
+ try:
56
+ answer = qa_chain.run(user_query)
57
+ return answer
58
+ except Exception as e:
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
+ fn=update_chat,
84
+ inputs=[user_msg, chatbot],
85
+ outputs=[user_msg, chatbot],
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="主题/章节名称", placeholder="如:线性代数 第五章 特征值")
97
+ gen_outline_btn = gr.Button("生成大纲")
98
+ gen_outline_btn.click(placeholder_fn, inputs=topic_input, outputs=topic_input)
99
 
100
+ # ---------- 自动出题 ----------
101
  with gr.TabItem("自动出题"):
102
+ gr.Markdown("(出题模块,待开发)")
103
  topic2 = gr.Textbox(label="知识点/主题", placeholder="如:高数 第三章 多元函数")
104
+ difficulty2 = gr.Dropdown(choices=["简单", "中等", "困难"], label="难度")
105
+ count2 = gr.Slider(1, 10, step=1, label="题目数量")
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)
113
+ user_ans = gr.Textbox(label="您的作答", lines=5)
114
+ grade_btn = gr.Button("开始批改")
115
+ grade_btn.click(placeholder_fn, inputs=[user_ans, std_ans], outputs=user_ans)
116
+
117
+ gr.Markdown("---\n由 HuggingFace 提供支持 • 版本 2.0")
 
118
 
119
  if __name__ == "__main__":
120
  demo.launch()