Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,135 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import random
|
| 3 |
-
from smolagents import GradioUI, CodeAgent, HfApiModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
from retriever import load_guest_dataset
|
| 8 |
|
| 9 |
-
# Initialize the
|
| 10 |
-
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
| 33 |
-
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# import random
|
| 3 |
+
# from smolagents import GradioUI, CodeAgent, HfApiModel
|
| 4 |
+
|
| 5 |
+
# # Import our custom tools from their modules
|
| 6 |
+
# from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
|
| 7 |
+
# from retriever import load_guest_dataset
|
| 8 |
+
|
| 9 |
+
# # Initialize the Hugging Face model
|
| 10 |
+
# model = HfApiModel()
|
| 11 |
+
|
| 12 |
+
# # Initialize the web search tool
|
| 13 |
+
# search_tool = DuckDuckGoSearchTool()
|
| 14 |
|
| 15 |
+
# # Initialize the weather tool
|
| 16 |
+
# weather_info_tool = WeatherInfoTool()
|
|
|
|
| 17 |
|
| 18 |
+
# # Initialize the Hub stats tool
|
| 19 |
+
# hub_stats_tool = HubStatsTool()
|
| 20 |
|
| 21 |
+
# # Load the guest dataset and initialize the guest info tool
|
| 22 |
+
# guest_info_tool = load_guest_dataset()
|
| 23 |
|
| 24 |
+
# # Create Alfred with all the tools
|
| 25 |
+
# alfred = CodeAgent(
|
| 26 |
+
# tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
|
| 27 |
+
# model=model,
|
| 28 |
+
# add_base_tools=True, # Add any additional base tools
|
| 29 |
+
# planning_interval=3 # Enable planning every 3 steps
|
| 30 |
+
# )
|
| 31 |
+
|
| 32 |
+
# if __name__ == "__main__":
|
| 33 |
+
# GradioUI(alfred).launch()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
import gradio as gr
|
| 37 |
+
from tool import run_agentic_rag # 从tool模块导入工作流运行函数
|
| 38 |
+
import time
|
| 39 |
|
| 40 |
+
def format_history(history):
|
| 41 |
+
"""格式化执行历史用于显示"""
|
| 42 |
+
formatted = []
|
| 43 |
+
for entry in history:
|
| 44 |
+
step = entry.get("step", "")
|
| 45 |
+
status = entry.get("status", "")
|
| 46 |
+
action = entry.get("action", "")
|
| 47 |
+
details = ""
|
| 48 |
+
|
| 49 |
+
if step == "检索" and status == "完成":
|
| 50 |
+
docs = entry.get("documents", [])
|
| 51 |
+
details = f"检索到 {len(docs)} 个文档"
|
| 52 |
+
|
| 53 |
+
elif step == "生成" and status == "完成":
|
| 54 |
+
answer = entry.get("answer", "")
|
| 55 |
+
details = f"生成答案: {answer[:100]}..." if len(answer) > 100 else f"生成答案: {answer}"
|
| 56 |
+
|
| 57 |
+
elif step == "验证" and status == "完成":
|
| 58 |
+
verif = entry.get("verification", {})
|
| 59 |
+
valid = verif.get("valid", False)
|
| 60 |
+
feedback = verif.get("feedback", "")
|
| 61 |
+
details = f"结果: {'通过' if valid else '失败'}, 反馈: {feedback}"
|
| 62 |
+
|
| 63 |
+
elif step == "准备重试" and status == "完成":
|
| 64 |
+
feedback = entry.get("feedback", "")
|
| 65 |
+
details = f"反馈: {feedback}"
|
| 66 |
+
|
| 67 |
+
elif "action" in entry:
|
| 68 |
+
details = entry["action"]
|
| 69 |
+
|
| 70 |
+
formatted.append(f"{step}: {status} {details}")
|
| 71 |
+
|
| 72 |
+
return "\n".join(formatted)
|
| 73 |
|
| 74 |
+
def process_query(question):
|
| 75 |
+
"""处理用户查询"""
|
| 76 |
+
start_time = time.time()
|
| 77 |
+
|
| 78 |
+
# 执行RAG工作流
|
| 79 |
+
result = run_agentic_rag(question)
|
| 80 |
+
|
| 81 |
+
# 准备结果显示
|
| 82 |
+
documents = "\n\n".join([
|
| 83 |
+
f"文档 {i+1}:\n{doc[:200]}..." if len(doc) > 200 else f"文档 {i+1}:\n{doc}"
|
| 84 |
+
for i, doc in enumerate(result["documents"])
|
| 85 |
+
])
|
| 86 |
+
|
| 87 |
+
end_time = time.time()
|
| 88 |
+
process_time = f"{end_time - start_time:.2f}秒"
|
| 89 |
+
|
| 90 |
+
return {
|
| 91 |
+
"answer": result["answer"],
|
| 92 |
+
"documents": documents,
|
| 93 |
+
"history": format_history(result["history"]),
|
| 94 |
+
"stats": f"重试次数: {result['retries_used']} | 处理时间: {process_time}"
|
| 95 |
+
}
|
| 96 |
|
| 97 |
+
# 创建Gradio界面
|
| 98 |
+
with gr.Blocks(title="Agentic RAG with LangGraph") as demo:
|
| 99 |
+
gr.Markdown("# 🧠 Agentic RAG 系统 (LangGraph 实现)")
|
| 100 |
+
gr.Markdown("使用LangGraph实现的带有自我验证和重试机制的RAG系统")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column():
|
| 104 |
+
question = gr.Textbox(
|
| 105 |
+
label="输入问题",
|
| 106 |
+
placeholder="在此输入您的问题...",
|
| 107 |
+
lines=3
|
| 108 |
+
)
|
| 109 |
+
submit_btn = gr.Button("提交", variant="primary")
|
| 110 |
+
|
| 111 |
+
with gr.Column():
|
| 112 |
+
answer = gr.Textbox(label="最终答案", interactive=False, lines=5)
|
| 113 |
+
documents = gr.Textbox(label="相关文档", interactive=False, lines=10)
|
| 114 |
+
history = gr.Textbox(label="执行历史", interactive=False, lines=15)
|
| 115 |
+
stats = gr.Textbox(label="统计信息", interactive=False)
|
| 116 |
+
|
| 117 |
+
# 事件处理
|
| 118 |
+
submit_btn.click(
|
| 119 |
+
fn=process_query,
|
| 120 |
+
inputs=[question],
|
| 121 |
+
outputs=[answer, documents, history, stats]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# 示例问题
|
| 125 |
+
gr.Examples(
|
| 126 |
+
examples=[
|
| 127 |
+
"量子纠缠是什么?它有哪些实际应用?",
|
| 128 |
+
"解释Transformer架构的核心创新点",
|
| 129 |
+
"如何在PyTorch中实现一个简单的神经网络?"
|
| 130 |
+
],
|
| 131 |
+
inputs=question
|
| 132 |
+
)
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
+
demo.launch()
|