Spaces:
Running on Zero
Running on Zero
zhang commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,50 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
-
from iopath.common.file_io import HTTPURLFile # 用于处理文件流
|
| 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(file):
|
| 13 |
-
if file is None:
|
| 14 |
-
return ""
|
| 15 |
-
try:
|
| 16 |
-
# 如果是 PDF,这里建议配合 PyMuPDF,但为了保持环境简单,先支持纯文本读取
|
| 17 |
-
with open(file.name, "r", encoding="utf-8") as f:
|
| 18 |
-
return f.read()
|
| 19 |
-
except:
|
| 20 |
-
return "[文件解析失败,请确保上传的是 UTF-8 编码的文本文件]"
|
| 21 |
-
|
| 22 |
@spaces.GPU
|
| 23 |
def chat_fn(message, history):
|
| 24 |
-
#
|
| 25 |
files = message.get("files", [])
|
| 26 |
text_content = ""
|
|
|
|
|
|
|
| 27 |
if files:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
else:
|
| 32 |
-
|
| 33 |
|
| 34 |
-
# 构建对话
|
| 35 |
messages = []
|
| 36 |
for turn in history:
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
messages.append({"role": "user", "content":
|
| 41 |
|
| 42 |
response = ""
|
| 43 |
try:
|
| 44 |
-
#
|
| 45 |
for msg in client.chat_completion(
|
| 46 |
messages,
|
| 47 |
-
max_tokens=
|
| 48 |
stream=True,
|
| 49 |
-
temperature=0.6
|
| 50 |
):
|
| 51 |
if len(msg.choices) > 0:
|
| 52 |
token = msg.choices[0].delta.content
|
|
@@ -54,23 +53,22 @@ def chat_fn(message, history):
|
|
| 54 |
response += token
|
| 55 |
yield response
|
| 56 |
except Exception as e:
|
| 57 |
-
yield f"⚠️ 算力调度异常: {str(e)}"
|
| 58 |
|
| 59 |
# 3. 构建 OpenIPOS 通用算力界面
|
| 60 |
with gr.Blocks(fill_height=True, theme=gr.themes.Soft()) as demo:
|
| 61 |
gr.Markdown("# 🚀 OpenIPOS Global 通用算力节点")
|
| 62 |
gr.Markdown("### DeepSeek-R1 32B 满血推理引擎")
|
| 63 |
-
gr.Markdown("本节点提供大规模、高逻辑密度的通用计算服务。支持文件上传分析
|
| 64 |
|
| 65 |
-
# 开启多模态(
|
| 66 |
gr.ChatInterface(
|
| 67 |
chat_fn,
|
| 68 |
-
multimodal=True,
|
| 69 |
-
|
| 70 |
-
examples=[{"text": "总结这份文件的核心逻辑", "files": []}],
|
| 71 |
)
|
| 72 |
|
| 73 |
gr.Markdown("---")
|
| 74 |
-
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU | 上下文支持:
|
| 75 |
|
| 76 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
from huggingface_hub import InferenceClient
|
|
|
|
| 5 |
|
| 6 |
# 1. 密钥与客户端初始化
|
| 7 |
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
client = InferenceClient("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", token=hf_token)
|
| 9 |
|
| 10 |
+
# 2. 定义核心推理逻辑
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@spaces.GPU
|
| 12 |
def chat_fn(message, history):
|
| 13 |
+
# 获取上传的文件列表
|
| 14 |
files = message.get("files", [])
|
| 15 |
text_content = ""
|
| 16 |
+
|
| 17 |
+
# 如果有上传文件,读取第一个文件的内容
|
| 18 |
if files:
|
| 19 |
+
try:
|
| 20 |
+
# 使用 Python 原生方式读取文本
|
| 21 |
+
with open(files[0], "r", encoding="utf-8") as f:
|
| 22 |
+
text_content = f.read()
|
| 23 |
+
user_input = f"【参考文件内容】:\n{text_content}\n\n【用户指令】: {message['text']}"
|
| 24 |
+
except Exception as e:
|
| 25 |
+
user_input = f"{message['text']} (文件读取失败: {str(e)})"
|
| 26 |
else:
|
| 27 |
+
user_input = message['text']
|
| 28 |
|
| 29 |
+
# 构建对话历史
|
| 30 |
messages = []
|
| 31 |
for turn in history:
|
| 32 |
+
# 兼容 Gradio 5.x 的消息字典格式
|
| 33 |
+
if isinstance(turn, dict):
|
| 34 |
+
messages.append({"role": turn["role"], "content": turn["content"]})
|
| 35 |
+
elif isinstance(turn, (list, tuple)) and len(turn) >= 2:
|
| 36 |
+
messages.append({"role": "user", "content": turn[0]})
|
| 37 |
+
messages.append({"role": "assistant", "content": turn[1]})
|
| 38 |
|
| 39 |
+
messages.append({"role": "user", "content": user_input})
|
| 40 |
|
| 41 |
response = ""
|
| 42 |
try:
|
| 43 |
+
# 尝试推向 32k 级别的上下文
|
| 44 |
for msg in client.chat_completion(
|
| 45 |
messages,
|
| 46 |
+
max_tokens=4096,
|
| 47 |
stream=True,
|
| 48 |
+
temperature=0.6
|
| 49 |
):
|
| 50 |
if len(msg.choices) > 0:
|
| 51 |
token = msg.choices[0].delta.content
|
|
|
|
| 53 |
response += token
|
| 54 |
yield response
|
| 55 |
except Exception as e:
|
| 56 |
+
yield f"⚠️ OpenIPOS 算力调度异常: {str(e)}"
|
| 57 |
|
| 58 |
# 3. 构建 OpenIPOS 通用算力界面
|
| 59 |
with gr.Blocks(fill_height=True, theme=gr.themes.Soft()) as demo:
|
| 60 |
gr.Markdown("# 🚀 OpenIPOS Global 通用算力节点")
|
| 61 |
gr.Markdown("### DeepSeek-R1 32B 满血推理引擎")
|
| 62 |
+
gr.Markdown("本节点提供**大规模、高逻辑密度**的通用计算服务。支持 `.txt`, `.md`, `.py` 等文本文件上传分析。")
|
| 63 |
|
| 64 |
+
# 开启多模态输入(左侧会出现 + 号)
|
| 65 |
gr.ChatInterface(
|
| 66 |
chat_fn,
|
| 67 |
+
multimodal=True,
|
| 68 |
+
type="messages", # 显式声明消息格式
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
gr.Markdown("---")
|
| 72 |
+
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU | 上下文支持:测试峰值 32K Token")
|
| 73 |
|
| 74 |
demo.launch()
|