Spaces:
Runtime error
Runtime error
Download app.py from pigrun2025/bot: direct link, hf CLI and curl.
- Browser
- Download file 10.3 kB
-
https://huggingface.co/spaces/pigrun2025/bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/pigrun2025/bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/pigrun2025/bot/resolve/main/app.py
10.3 kB
| import gradio as gr | |
| import os | |
| import tempfile | |
| from typing import List, Tuple, Optional | |
| import dashscope | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import Chroma | |
| from langchain_community.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader | |
| import shutil | |
| # ============== 配置 ============== | |
| DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY", "") | |
| if DASHSCOPE_API_KEY: | |
| dashscope.api_key = DASHSCOPE_API_KEY | |
| QWEN_MODEL = "qwen-turbo" | |
| # 全局变量 | |
| vector_store = None | |
| docs_loaded = False | |
| # ============== 工具函数 ============== | |
| def load_documents(file_paths: List[str]) -> List: | |
| """加载各种格式的文档""" | |
| documents = [] | |
| for file_path in file_paths: | |
| try: | |
| if file_path.endswith('.pdf'): | |
| loader = PyPDFLoader(file_path) | |
| elif file_path.endswith('.docx'): | |
| loader = Docx2txtLoader(file_path) | |
| elif file_path.endswith('.txt'): | |
| loader = TextLoader(file_path, encoding='utf-8') | |
| else: | |
| continue | |
| documents.extend(loader.load()) | |
| except Exception as e: | |
| print(f"加载文件失败 {file_path}: {e}") | |
| return documents | |
| def create_vector_store(documents: List): | |
| """创建向量数据库""" | |
| text_splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=500, | |
| chunk_overlap=50, | |
| length_function=len, | |
| ) | |
| chunks = text_splitter.split_documents(documents) | |
| embeddings = HuggingFaceEmbeddings( | |
| model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2", | |
| model_kwargs={'device': 'cpu'}, | |
| encode_kwargs={'normalize_embeddings': True} | |
| ) | |
| vector_store = Chroma.from_documents( | |
| documents=chunks, | |
| embedding=embeddings, | |
| persist_directory=None | |
| ) | |
| return vector_store | |
| def chat_with_qwen(messages: List[dict]) -> str: | |
| """调用通义千问 API""" | |
| try: | |
| response = dashscope.Generation.call( | |
| model=QWEN_MODEL, | |
| messages=messages, | |
| result_format='message' | |
| ) | |
| if response.status_code == 200: | |
| return response.output.choices[0].message.content | |
| else: | |
| return f"API 错误:{response.code} - {response.message}" | |
| except Exception as e: | |
| return f"调用失败:{str(e)}" | |
| def retrieve_context(query: str, top_k: int = 3) -> str: | |
| """从向量数据库检索相关内容""" | |
| global vector_store | |
| if vector_store is None: | |
| return "" | |
| try: | |
| results = vector_store.similarity_search(query, k=top_k) | |
| context = "\n\n".join([doc.page_content for doc in results]) | |
| return context | |
| except Exception as e: | |
| return f"检索失败:{str(e)}" | |
| # ============== Gradio 界面 ============== | |
| with gr.Blocks(title="通义千问混合机器人", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # 🤖 通义千问混合机器人 | |
| 支持两种模式: | |
| - **💬 聊天模式**:自由对话,什么问题都可以问 | |
| - **📄 文档问答**:上传文档后,基于文档内容回答问题 | |
| **使用前请确保已在 Space 设置中配置 `DASHSCOPE_API_KEY` 环境变量** | |
| """) | |
| # 状态存储 | |
| chat_state = gr.State([]) | |
| doc_store = gr.State(None) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### 📁 文档管理") | |
| file_upload = gr.File( | |
| label="上传文档 (PDF/Word/TXT)", | |
| file_types=[".pdf", ".docx", ".txt"], | |
| file_count="multiple" | |
| ) | |
| load_doc_btn = gr.Button("📥 加载文档", variant="secondary") | |
| mode_toggle = gr.Checkbox( | |
| label="📄 启用文档问答模式", | |
| value=False, | |
| info="开启后基于上传的文档回答问题" | |
| ) | |
| status_box = gr.Textbox( | |
| label="状态", | |
| value="就绪", | |
| interactive=False, | |
| lines=1 | |
| ) | |
| clear_btn = gr.Button("🗑️ 清除状态", variant="secondary") | |
| with gr.Column(scale=3): | |
| gr.Markdown("### 💬 对话") | |
| chatbot = gr.Chatbot( | |
| label="对话记录", | |
| height=450, | |
| placeholder="输入问题开始对话...", | |
| show_copy_button=True | |
| ) | |
| msg_input = gr.Textbox( | |
| label="输入消息", | |
| placeholder="输入您的问题,按 Enter 发送...", | |
| lines=2 | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("🚀 发送", variant="primary", scale=2) | |
| clear_chat_btn = gr.Button("🧹 清空对话", variant="secondary", scale=1) | |
| gr.Markdown(""" | |
| --- | |
| **提示:** | |
| - 首次使用请在 Space 设置中配置 `DASHSCOPE_API_KEY` 环境变量 | |
| - 文档问答模式需要先上传文档并点击"加载文档" | |
| - 通义千问模型:qwen-turbo(快速)/ qwen-plus(平衡)/ qwen-max(最强) | |
| """) | |
| # ============== 事件处理 ============== | |
| def load_docs(files): | |
| """加载文档到向量数据库""" | |
| global vector_store, docs_loaded | |
| if not files: | |
| return "⚠️ 请先选择文件", None | |
| try: | |
| file_paths = [f.name for f in files] | |
| documents = load_documents(file_paths) | |
| if not documents: | |
| return "❌ 未找到可加载的文档", None | |
| vector_store = create_vector_store(documents) | |
| docs_loaded = True | |
| return f"✅ 已加载 {len(documents)} 个文档片段", vector_store | |
| except Exception as e: | |
| return f"❌ 加载失败:{str(e)}", None | |
| def respond(message, chat_history, files, use_doc_mode, current_store): | |
| """处理用户消息""" | |
| global vector_store, docs_loaded | |
| if not message: | |
| return "", chat_history, "⚠️ 请输入内容" | |
| # 检查 API 密钥 | |
| if not DASHSCOPE_API_KEY: | |
| error_msg = "⚠️ 请在 Space 设置中配置 DASHSCOPE_API_KEY 环境变量\n\n配置方法:\n1. 进入 Space Settings\n2. 找到 Repository secrets\n3. 添加 DASHSCOPE_API_KEY" | |
| if chat_history is None: | |
| chat_history = [] | |
| chat_history.append((message, error_msg)) | |
| return "", chat_history, "❌ 缺少 API 密钥" | |
| # 如果是文档模式但没有加载文档 | |
| if use_doc_mode and not docs_loaded: | |
| hint_msg = "📄 检测到您启用了文档问答模式,但尚未加载文档。\n\n请先上传文档并点击「加载文档」按钮,或者关闭文档问答模式进行普通聊天。" | |
| if chat_history is None: | |
| chat_history = [] | |
| chat_history.append((message, hint_msg)) | |
| return "", chat_history, "⚠️ 请先加载文档" | |
| # 文档问答模式 | |
| if use_doc_mode and docs_loaded and vector_store: | |
| context = retrieve_context(message) | |
| if context: | |
| prompt = f"""请根据以下文档内容回答问题。如果文档中没有相关信息,请说明。 | |
| 文档内容: | |
| {context} | |
| 用户问题:{message} | |
| 请基于文档内容给出准确回答:""" | |
| messages = [ | |
| {"role": "system", "content": "你是一个专业的文档问答助手,请基于提供的文档内容准确回答问题。"}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| response = chat_with_qwen(messages) | |
| if chat_history is None: | |
| chat_history = [] | |
| chat_history.append((message, response)) | |
| return "", chat_history, "✅ 基于文档回答" | |
| else: | |
| hint_msg = "🤔 文档中没有找到相关信息。\n\n您可以:\n1. 尝试换一种问法\n2. 关闭文档问答模式进行普通聊天" | |
| if chat_history is None: | |
| chat_history = [] | |
| chat_history.append((message, hint_msg)) | |
| return "", chat_history, "⚠️ 未找到相关信息" | |
| # 普通聊天模式 | |
| messages = [{"role": "system", "content": "你是一个友好、专业的 AI 助手,由通义千问驱动。"}] | |
| if chat_history: | |
| for user_msg, assistant_msg in chat_history[-5:]: | |
| messages.append({"role": "user", "content": user_msg}) | |
| messages.append({"role": "assistant", "content": assistant_msg}) | |
| messages.append({"role": "user", "content": message}) | |
| response = chat_with_qwen(messages) | |
| if chat_history is None: | |
| chat_history = [] | |
| chat_history.append((message, response)) | |
| return "", chat_history, "✅ 已回复" | |
| def clear_state(): | |
| """清除状态""" | |
| global vector_store, docs_loaded | |
| vector_store = None | |
| docs_loaded = False | |
| return "状态已清除", None, [] | |
| def clear_chat_only(): | |
| """只清空对话""" | |
| return "", [] | |
| # 绑定事件 | |
| load_doc_btn.click( | |
| fn=load_docs, | |
| inputs=[file_upload], | |
| outputs=[status_box, doc_store] | |
| ) | |
| submit_btn.click( | |
| fn=respond, | |
| inputs=[msg_input, chatbot, file_upload, mode_toggle, doc_store], | |
| outputs=[msg_input, chatbot, status_box] | |
| ) | |
| msg_input.submit( | |
| fn=respond, | |
| inputs=[msg_input, chatbot, file_upload, mode_toggle, doc_store], | |
| outputs=[msg_input, chatbot, status_box] | |
| ) | |
| clear_btn.click( | |
| fn=clear_state, | |
| outputs=[status_box, doc_store, chatbot] | |
| ) | |
| clear_chat_btn.click( | |
| fn=clear_chat_only, | |
| outputs=[msg_input, chatbot] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |