Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,55 +2,57 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
import fitz # PyMuPDF
|
|
|
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
-
# 1. 客户端初始化
|
| 8 |
hf_token = os.getenv("HF_TOKEN")
|
| 9 |
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
|
| 10 |
|
| 11 |
-
# 2.
|
| 12 |
def extract_text_from_file(file_path):
|
| 13 |
try:
|
| 14 |
-
# 处理 PDF
|
| 15 |
if file_path.lower().endswith(".pdf"):
|
| 16 |
doc = fitz.open(file_path)
|
| 17 |
return "".join([page.get_text() for page in doc])
|
| 18 |
-
|
| 19 |
-
# 处理 Word (.docx)
|
| 20 |
elif file_path.lower().endswith(".docx"):
|
| 21 |
doc = docx.Document(file_path)
|
| 22 |
return "\n".join([para.text for para in doc.paragraphs])
|
| 23 |
-
|
| 24 |
-
# 处理 TXT/MD
|
| 25 |
else:
|
| 26 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 27 |
return f.read()
|
| 28 |
except Exception as e:
|
| 29 |
-
return f"[
|
| 30 |
|
| 31 |
-
@spaces.GPU(duration=120)
|
| 32 |
def chat_fn(message, history):
|
|
|
|
| 33 |
files = message.get("files", [])
|
| 34 |
-
|
| 35 |
|
| 36 |
if files:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
user_input = f"【参考文档内容】:\n{text_content[:50000]}\n\n【用户指令】: {message['text']}"
|
| 42 |
else:
|
| 43 |
-
|
| 44 |
|
| 45 |
-
# 构建
|
| 46 |
messages = []
|
| 47 |
for turn in history:
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
response = ""
|
| 52 |
try:
|
| 53 |
-
#
|
| 54 |
for msg in client.chat_completion(
|
| 55 |
messages,
|
| 56 |
max_tokens=4096,
|
|
@@ -63,26 +65,21 @@ def chat_fn(message, history):
|
|
| 63 |
response += token
|
| 64 |
yield response
|
| 65 |
|
| 66 |
-
#
|
| 67 |
response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
|
| 68 |
yield response
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
-
yield f"⚠️ OpenIPOS 调度异常: {str(e)}。
|
| 72 |
|
| 73 |
-
#
|
| 74 |
with gr.Blocks(fill_height=True) as demo:
|
| 75 |
gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
|
| 76 |
-
gr.Markdown("###
|
| 77 |
-
gr.Markdown("本节点由 **OpenIPOS Global** 部署,支持 **64K 超长上下文** 及 **PDF/TXT** 文档分析。")
|
| 78 |
|
| 79 |
-
gr.ChatInterface(
|
| 80 |
-
chat_fn,
|
| 81 |
-
multimodal=True,
|
| 82 |
-
)
|
| 83 |
|
| 84 |
gr.Markdown("---")
|
| 85 |
-
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU
|
| 86 |
|
| 87 |
-
# 4. 启动应用
|
| 88 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
+
import docx # python-docx
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
# 1. 客户端初始化
|
| 9 |
hf_token = os.getenv("HF_TOKEN")
|
| 10 |
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
|
| 11 |
|
| 12 |
+
# 2. 全能文档解析引擎
|
| 13 |
def extract_text_from_file(file_path):
|
| 14 |
try:
|
|
|
|
| 15 |
if file_path.lower().endswith(".pdf"):
|
| 16 |
doc = fitz.open(file_path)
|
| 17 |
return "".join([page.get_text() for page in doc])
|
|
|
|
|
|
|
| 18 |
elif file_path.lower().endswith(".docx"):
|
| 19 |
doc = docx.Document(file_path)
|
| 20 |
return "\n".join([para.text for para in doc.paragraphs])
|
|
|
|
|
|
|
| 21 |
else:
|
| 22 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 23 |
return f.read()
|
| 24 |
except Exception as e:
|
| 25 |
+
return f"[文档读取失败: {str(e)}]"
|
| 26 |
|
| 27 |
+
@spaces.GPU(duration=120)
|
| 28 |
def chat_fn(message, history):
|
| 29 |
+
# 处理当前输入
|
| 30 |
files = message.get("files", [])
|
| 31 |
+
current_text = message.get("text", "")
|
| 32 |
|
| 33 |
if files:
|
| 34 |
+
file_path = files[0] if isinstance(files[0], str) else files[0].path
|
| 35 |
+
doc_content = extract_text_from_file(file_path)
|
| 36 |
+
# 截取前 40,000 字以防溢出
|
| 37 |
+
user_payload = f"【参考文档内容】:\n{doc_content[:40000]}\n\n【用户指令】: {current_text}"
|
|
|
|
| 38 |
else:
|
| 39 |
+
user_payload = current_text
|
| 40 |
|
| 41 |
+
# 3. 构建严谨的消息列表 (适配 Gradio 6.0)
|
| 42 |
messages = []
|
| 43 |
for turn in history:
|
| 44 |
+
# 只保留 role 和 content,过滤掉其他干扰字段
|
| 45 |
+
if isinstance(turn, dict) and "role" in turn and "content" in turn:
|
| 46 |
+
# 过滤掉空的 content 或非字符串内容
|
| 47 |
+
if turn["content"] and isinstance(turn["content"], str):
|
| 48 |
+
messages.append({"role": turn["role"], "content": turn["content"]})
|
| 49 |
+
|
| 50 |
+
# 加入当前请求
|
| 51 |
+
messages.append({"role": "user", "content": user_payload})
|
| 52 |
|
| 53 |
response = ""
|
| 54 |
try:
|
| 55 |
+
# 这里的 client.chat_completion 对 payload 非常挑剔
|
| 56 |
for msg in client.chat_completion(
|
| 57 |
messages,
|
| 58 |
max_tokens=4096,
|
|
|
|
| 65 |
response += token
|
| 66 |
yield response
|
| 67 |
|
| 68 |
+
# 注入品牌签名
|
| 69 |
response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
|
| 70 |
yield response
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
+
yield f"⚠️ OpenIPOS 调度异常 (API 拒绝请求): {str(e)}\n提示:请尝试清空对话历史后再试。"
|
| 74 |
|
| 75 |
+
# 4. 构建 UI
|
| 76 |
with gr.Blocks(fill_height=True) as demo:
|
| 77 |
gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
|
| 78 |
+
gr.Markdown("### 适配 Gradio 6.0 的高稳定性通用算力节点")
|
|
|
|
| 79 |
|
| 80 |
+
gr.ChatInterface(chat_fn, multimodal=True)
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
gr.Markdown("---")
|
| 83 |
+
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU")
|
| 84 |
|
|
|
|
| 85 |
demo.launch(theme=gr.themes.Soft())
|