ljy5946 commited on
Commit
014d031
·
verified ·
1 Parent(s): dd3f8c5

Upload 7 files

Browse files
Files changed (7) hide show
  1. .env +1 -0
  2. Readme.md +38 -0
  3. app.py +30 -64
  4. courses.json +5 -0
  5. deepseek_client.py +28 -0
  6. knowledge.py +16 -0
  7. requirement.txt +2 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ DEEPSEEK_API_KEY=sk-be518c2c78dc49e688c125d9b761c244
Readme.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 智能学习助手 Demo
2
+
3
+ **赛道标签**:`agent-demo-track`
4
+
5
+ ## 项目简介
6
+
7
+ 这是一个基于 Gradio + DeepSeek 打造的“智能学习助手”示例。用户可以:
8
+ - **课程知识点**:输入某门课程(如“C语言程序设计”),从本地 JSON 知识库快速返回摘要;
9
+ - **与 LLM 问答**:输入任意学习相关问题,由 DeepSeek 模型生成回答。
10
+
11
+ ## 部署链接
12
+
13
+ Demo 地址:https://huggingface.co/spaces/your-username/my-llm-learning-assistant
14
+
15
+ ## 使用说明
16
+
17
+ 1. 点击“课程知识点”模式,输入课程名(必须与 `courses.json` 中的 key 一致)。
18
+ 2. 切换到“与 LLM 问答”模式,输入对学习相关的任意问题,点击提交后,DeepSeek 模型会给出回答。
19
+
20
+ ## 模型与技术栈
21
+
22
+ - **DeepSeek-V3** (`deepseek-chat`),通过 OpenAI SDK 调用,`openai.api_base` 设置为 `https://api.deepseek.com`。:contentReference[oaicite:7]{index=7}
23
+ - **Gradio**:快速部署交互界面。
24
+
25
+ ## 运行环境
26
+
27
+ - Python 3.10+
28
+ - 依赖列表:见 `requirements.txt`
29
+
30
+ ## 未来优化方向
31
+
32
+ - **本地知识库扩充**:将更多课程或章节信息以 JSON 形式补充进去,或接入学校图书馆 API 做实时检索。
33
+ - **多轮对话**:目前为逐次单轮问答,后续可以保留聊天上下文,支持多轮跟进提问。
34
+ - **自定义组件**:为下拉选择常见课程名或热门问题添加自动补全与联想功能。
35
+
36
+ ## 视频演示
37
+
38
+ [在此粘贴你录制的 2 分钟 Demo 视频链接]
app.py CHANGED
@@ -1,64 +1,30 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ # app.py
2
+ import gradio as gr
3
+ from knowledge import lookup_course
4
+ from deepseek_client import ask_deepseek
5
+
6
+ def student_assistant(user_input: str, mode: str) -> str:
7
+ user_input = user_input.strip()
8
+ if not user_input:
9
+ return "请输入课程名或问题。"
10
+ if mode == "课程知识点":
11
+ return lookup_course(user_input)
12
+ else:
13
+ return ask_deepseek(user_input)
14
+
15
+ with gr.Blocks() as demo:
16
+ gr.Markdown("# 智能学习助手 Demo\n输入课程名或问题,选择模式,获取答案。")
17
+ with gr.Row():
18
+ user_input = gr.Textbox(label="输入课程名或问题", placeholder="如:C语言程序设计", lines=1)
19
+ mode = gr.Radio(choices=["课程知识点", "与 LLM 问答"], label="模式选择", value="课程知识点")
20
+ output = gr.Textbox(label="输出结果", interactive=False)
21
+ btn = gr.Button("提交")
22
+ gr.Examples(
23
+ examples=[["C语言程序设计", "课程知识点"], ["什么是二叉树?", "与 LLM 问答"]],
24
+ inputs=[user_input, mode]
25
+ )
26
+ btn.click(student_assistant, inputs=[user_input, mode], outputs=output)
27
+ user_input.submit(student_assistant, inputs=[user_input, mode], outputs=output)
28
+
29
+ if __name__ == "__main__":
30
+ demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
courses.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "C语言程序设计": "第1章:数据类型与运算符;第2章:顺序与分支结构;第3章:循环与函数;第4章:数组与指针;第5章:结构体与文件操作。",
3
+ "Web前端开发": "HTML 基础标签;CSS 布局与样式;JavaScript DOM 操作;前端框架概述;常见开发工具与调试技巧。",
4
+ "计算机科学导论": "计算机系统组成;操作系统基础;算法与数据结构概念;编程语言演进;人工智能概览。"
5
+ }
deepseek_client.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # deepseek_client.py
2
+ import os
3
+ from openai import OpenAI
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+ DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
8
+ if not DEEPSEEK_API_KEY:
9
+ raise RuntimeError("未检测到环境变量 DEEPSEEK_API_KEY,请先设置 DeepSeek 的 Key。")
10
+
11
+ deepseek_client = OpenAI(
12
+ api_key=DEEPSEEK_API_KEY,
13
+ base_url="https://api.deepseek.com"
14
+ )
15
+
16
+ def ask_deepseek(user_query: str,
17
+ system_prompt: str = "You are a helpful learning assistant.") -> str:
18
+ try:
19
+ response = deepseek_client.chat.completions.create(
20
+ model="deepseek-chat",
21
+ messages=[
22
+ {"role": "system", "content": system_prompt},
23
+ {"role": "user", "content": user_query}
24
+ ]
25
+ )
26
+ return response.choices[0].message.content.strip()
27
+ except Exception as e:
28
+ return f"调用 DeepSeek API 时出错:{e}"
knowledge.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # knowledge.py
2
+ import json
3
+ import os
4
+
5
+ BASE_DIR = os.path.dirname(__file__)
6
+ COURSE_DB_PATH = os.path.join(BASE_DIR, "courses.json")
7
+
8
+ with open(COURSE_DB_PATH, "r", encoding="utf-8") as f:
9
+ COURSE_DB = json.load(f)
10
+
11
+ def lookup_course(course_name: str) -> str:
12
+ course_name = course_name.strip()
13
+ if course_name in COURSE_DB:
14
+ return COURSE_DB[course_name]
15
+ else:
16
+ return f"未在本地知识库中找到“{course_name}”,你可以输入其他课程名或使用 LLM 问答模式。"
requirement.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ openai