Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
import logging
|
4 |
|
5 |
from langchain_community.vectorstores import Chroma
|
6 |
-
from
|
7 |
from langchain.chains import RetrievalQA
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
9 |
from langchain.llms import HuggingFacePipeline
|
@@ -53,12 +52,12 @@ def simple_qa(user_query):
|
|
53 |
return answer
|
54 |
except Exception as e:
|
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])
|
@@ -68,10 +67,10 @@ def generate_outline(topic: str):
|
|
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):
|
@@ -102,8 +101,9 @@ with gr.Blocks() as demo:
|
|
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("自动出题"):
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import logging
|
3 |
|
4 |
from langchain_community.vectorstores import Chroma
|
5 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
6 |
from langchain.chains import RetrievalQA
|
7 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
8 |
from langchain.llms import HuggingFacePipeline
|
|
|
52 |
return answer
|
53 |
except Exception as e:
|
54 |
logging.error(f"问答失败: {e}")
|
55 |
+
return f"⚠️ 问答失败,请稍后再试。\n[调试信息] {e}"
|
56 |
|
57 |
# === 大纲生成函数 ===
|
58 |
def generate_outline(topic: str):
|
59 |
if not topic.strip():
|
60 |
+
return "⚠️ 请输入章节或主题,例如:高等数学 第六章 定积分", ""
|
61 |
try:
|
62 |
docs = vector_store.as_retriever(search_kwargs={"k": 3}).get_relevant_documents(topic)
|
63 |
snippet = "\n".join([doc.page_content for doc in docs])
|
|
|
67 |
f"文档内容:\n{snippet}\n\n学习大纲:"
|
68 |
)
|
69 |
result = llm.generate(prompt).generations[0][0].text.strip()
|
70 |
+
return result, snippet
|
71 |
except Exception as e:
|
72 |
logging.error(f"大纲生成失败: {e}")
|
73 |
+
return "⚠️ 抱歉,生成失败,请稍后再试。", ""
|
74 |
|
75 |
# === 占位函数 ===
|
76 |
def placeholder_fn(*args, **kwargs):
|
|
|
101 |
gr.Markdown("> 示例:高等数学 第六章 定积分")
|
102 |
topic_input = gr.Textbox(label="章节主题", placeholder="请输入章节名")
|
103 |
outline_output = gr.Textbox(label="系统生成的大纲", lines=12)
|
104 |
+
snippet_output = gr.Textbox(label="[调试] 检索片段展示", lines=6)
|
105 |
gen_outline_btn = gr.Button("生成大纲")
|
106 |
+
gen_outline_btn.click(fn=generate_outline, inputs=topic_input, outputs=[outline_output, snippet_output])
|
107 |
|
108 |
# --- 模块 C:自动出题(占位) ---
|
109 |
with gr.TabItem("自动出题"):
|