Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,11 @@ 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 |
suffix = file_path.lower()
|
|
@@ -19,40 +19,44 @@ def extract_text_from_file(file_path):
|
|
| 19 |
elif suffix.endswith(".docx"):
|
| 20 |
doc = docx.Document(file_path)
|
| 21 |
return "\n".join([para.text for para in doc.paragraphs])
|
| 22 |
-
elif suffix.endswith(".doc"):
|
| 23 |
-
return "⚠️ 提示:系统暂不支持旧版 .doc 格式,请将其另存为 .docx 或 .pdf 后再次上传。"
|
| 24 |
else:
|
| 25 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 26 |
return f.read()
|
| 27 |
except Exception as e:
|
| 28 |
-
return f"[
|
| 29 |
|
| 30 |
@spaces.GPU(duration=120)
|
| 31 |
def chat_fn(message, history):
|
| 32 |
-
|
| 33 |
current_text = message.get("text", "")
|
|
|
|
| 34 |
|
|
|
|
| 35 |
if files:
|
| 36 |
file_path = files[0] if isinstance(files[0], str) else files[0].path
|
| 37 |
-
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
else:
|
| 41 |
-
|
| 42 |
|
| 43 |
-
# 严
|
| 44 |
-
|
| 45 |
for turn in history:
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
response = ""
|
| 52 |
try:
|
| 53 |
-
#
|
| 54 |
for msg in client.chat_completion(
|
| 55 |
-
|
| 56 |
max_tokens=4096,
|
| 57 |
stream=True,
|
| 58 |
temperature=0.6
|
|
@@ -63,19 +67,19 @@ 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 |
-
|
| 72 |
-
yield f"{response}\n\n⚠️ OpenIPOS 调度异常 (可能是文档过长导致超时): {str(e)}\n提示:请尝试清空对话或分段上传文档。"
|
| 73 |
|
| 74 |
-
# 3. 极简 UI
|
| 75 |
with gr.Blocks(fill_height=True) as demo:
|
| 76 |
gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
|
| 77 |
-
gr.Markdown("### 适配
|
| 78 |
|
|
|
|
| 79 |
gr.ChatInterface(chat_fn, multimodal=True)
|
| 80 |
|
| 81 |
gr.Markdown("---")
|
|
|
|
| 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 |
suffix = file_path.lower()
|
|
|
|
| 19 |
elif suffix.endswith(".docx"):
|
| 20 |
doc = docx.Document(file_path)
|
| 21 |
return "\n".join([para.text for para in doc.paragraphs])
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 24 |
return f.read()
|
| 25 |
except Exception as e:
|
| 26 |
+
return f"[文档读取失败: {str(e)}]"
|
| 27 |
|
| 28 |
@spaces.GPU(duration=120)
|
| 29 |
def chat_fn(message, history):
|
| 30 |
+
# a. 提取当前文本和文件
|
| 31 |
current_text = message.get("text", "")
|
| 32 |
+
files = message.get("files", [])
|
| 33 |
|
| 34 |
+
# b. 构造当前用户输入的 Payload
|
| 35 |
if files:
|
| 36 |
file_path = files[0] if isinstance(files[0], str) else files[0].path
|
| 37 |
+
content = extract_text_from_file(file_path)
|
| 38 |
+
# 为保证 100% 成功率,初次测试建议截取前 20,000 字
|
| 39 |
+
user_input = f"【参考文档】:\n{content[:20000]}\n\n【指令】: {current_text}"
|
| 40 |
else:
|
| 41 |
+
user_input = current_text
|
| 42 |
|
| 43 |
+
# c. 严格的消息格式化(关键:只传字符串内容)
|
| 44 |
+
formatted_messages = []
|
| 45 |
for turn in history:
|
| 46 |
+
# 只处理 role 为 user/assistant 且 content 为字符串的项
|
| 47 |
+
role = turn.get("role")
|
| 48 |
+
content = turn.get("content")
|
| 49 |
+
if role in ["user", "assistant"] and isinstance(content, str):
|
| 50 |
+
formatted_messages.append({"role": role, "content": content})
|
| 51 |
|
| 52 |
+
# 加入当前这轮
|
| 53 |
+
formatted_messages.append({"role": "user", "content": user_input})
|
| 54 |
|
| 55 |
response = ""
|
| 56 |
try:
|
| 57 |
+
# 调用 API
|
| 58 |
for msg in client.chat_completion(
|
| 59 |
+
formatted_messages,
|
| 60 |
max_tokens=4096,
|
| 61 |
stream=True,
|
| 62 |
temperature=0.6
|
|
|
|
| 67 |
response += token
|
| 68 |
yield response
|
| 69 |
|
| 70 |
+
# 注入品牌指纹
|
| 71 |
response += "\n\n--- \n> **Powered by OpenIPOS Global 算力节点 | 访问 openipos.com 获取企业级私有部署**"
|
| 72 |
yield response
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
yield f"⚠️ OpenIPOS 算力调度异常: {str(e)}\n\n【解决方案】:请点击右上角垃圾桶图标『清空对话历史』后再重新上传文件。"
|
|
|
|
| 76 |
|
| 77 |
+
# 3. 极简 UI 构建
|
| 78 |
with gr.Blocks(fill_height=True) as demo:
|
| 79 |
gr.Markdown("# 🚀 OpenIPOS/DeepSeek-R1-MegaNode-64K")
|
| 80 |
+
gr.Markdown("### 工业级稳定性适配 | 支持 PDF/DOCX/TXT")
|
| 81 |
|
| 82 |
+
# 使用官方推荐的 ChatInterface 默认配置
|
| 83 |
gr.ChatInterface(chat_fn, multimodal=True)
|
| 84 |
|
| 85 |
gr.Markdown("---")
|