Spaces:
Running on Zero
Running on Zero
zhang commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,45 +2,75 @@ import os
|
|
| 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 |
@spaces.GPU
|
| 11 |
def chat_fn(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
messages = []
|
| 13 |
for turn in history:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
messages.append({"role": "assistant", "content": turn[1]})
|
| 19 |
-
messages.append({"role": "user", "content": message})
|
| 20 |
|
| 21 |
response = ""
|
| 22 |
try:
|
| 23 |
-
#
|
| 24 |
-
for msg in client.chat_completion(
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if len(msg.choices) > 0:
|
| 27 |
token = msg.choices[0].delta.content
|
| 28 |
if token:
|
| 29 |
response += token
|
| 30 |
yield response
|
| 31 |
except Exception as e:
|
| 32 |
-
yield f"⚠️
|
| 33 |
|
| 34 |
-
#
|
| 35 |
with gr.Blocks(fill_height=True, theme=gr.themes.Soft()) as demo:
|
| 36 |
-
gr.Markdown("# 🚀 OpenIPOS Global 算力节点")
|
| 37 |
-
gr.Markdown("###
|
|
|
|
| 38 |
|
|
|
|
| 39 |
gr.ChatInterface(
|
| 40 |
chat_fn,
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
)
|
|
|
|
| 43 |
gr.Markdown("---")
|
| 44 |
-
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU")
|
| 45 |
|
| 46 |
demo.launch()
|
|
|
|
| 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. 文件读取辅助函数 (处理 PDF 和 TXT)
|
| 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 |
+
text_content = extract_text(files[0])
|
| 30 |
+
prompt = f"参考文件内容:\n{text_content}\n\n用户指令:{message['text']}"
|
| 31 |
+
else:
|
| 32 |
+
prompt = message['text']
|
| 33 |
+
|
| 34 |
+
# 构建对话上下文
|
| 35 |
messages = []
|
| 36 |
for turn in history:
|
| 37 |
+
messages.append({"role": "user", "content": turn[0]})
|
| 38 |
+
messages.append({"role": "assistant", "content": turn[1]})
|
| 39 |
+
|
| 40 |
+
messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
| 41 |
|
| 42 |
response = ""
|
| 43 |
try:
|
| 44 |
+
# 设置最大上下文支持:尝试推向 32k 左右的 Token 长度
|
| 45 |
+
for msg in client.chat_completion(
|
| 46 |
+
messages,
|
| 47 |
+
max_tokens=8192, # 单次回复长度
|
| 48 |
+
stream=True,
|
| 49 |
+
temperature=0.6 # 保持推理的严谨性
|
| 50 |
+
):
|
| 51 |
if len(msg.choices) > 0:
|
| 52 |
token = msg.choices[0].delta.content
|
| 53 |
if token:
|
| 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 |
+
textbox=gr.MultimodalTextbox(file_types=[".txt", ".md", ".py"], placeholder="输入消息或上传文件..."),
|
| 70 |
+
examples=[{"text": "总结这份文件的核心逻辑", "files": []}],
|
| 71 |
)
|
| 72 |
+
|
| 73 |
gr.Markdown("---")
|
| 74 |
+
gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face ZeroGPU | 上下文支持:最高 32K Token")
|
| 75 |
|
| 76 |
demo.launch()
|