OpenIPOS commited on
Commit
b3409ca
·
verified ·
1 Parent(s): e491433

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -7
app.py CHANGED
@@ -1,10 +1,85 @@
 
1
  import gradio as gr
 
 
 
 
2
 
3
- with gr.Blocks(fill_height=True) as demo:
4
- with gr.Sidebar():
5
- gr.Markdown("# Inference Provider")
6
- gr.Markdown("This Space showcases the deepseek-ai/DeepSeek-V3.2-Exp model, served by the novita API. Sign in with your Hugging Face account to use this API.")
7
- button = gr.LoginButton("Sign in")
8
- gr.load("models/deepseek-ai/DeepSeek-V3.2-Exp", accept_token=button, provider="novita")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ import spaces
4
+ import fitz
5
+ import docx
6
+ from huggingface_hub import InferenceClient
7
 
8
+ # 1. 引擎配置:DeepSeek-V3.2-Exp (685B 逻辑核弹)
9
+ model_id = "deepseek-ai/DeepSeek-V3.2-Exp"
10
+ hf_token = os.getenv("HF_TOKEN")
11
+
12
+ client = InferenceClient(
13
+ model=model_id,
14
+ token=hf_token,
15
+ headers={"Authorization": f"Bearer {hf_token}"} if hf_token else None
16
+ )
17
+
18
+ def extract_text(file_path):
19
+ if not file_path: return ""
20
+ try:
21
+ ext = str(file_path).lower()
22
+ if ext.endswith(".pdf"):
23
+ doc = fitz.open(file_path)
24
+ text = "".join([page.get_text() for page in doc])
25
+ doc.close()
26
+ return text[:15000]
27
+ elif ext.endswith(".docx"):
28
+ doc = docx.Document(file_path)
29
+ return "\n".join([p.text for p in doc.paragraphs])[:15000]
30
+ elif ext.endswith(".txt"):
31
+ with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
32
+ return f.read()[:15000]
33
+ return ""
34
+ except:
35
+ return ""
36
+
37
+ @spaces.GPU(duration=60)
38
+ def chat_fn(message, history):
39
+ # 清洗历史记录,适配 DeepSeek 的严苛格式
40
+ clean_messages = []
41
+ for turn in history:
42
+ clean_messages.append({"role": turn["role"], "content": str(turn["content"])})
43
+
44
+ prompt_text = message.get("text", "")
45
+ files = message.get("files", [])
46
 
47
+ if files:
48
+ f = files[0]
49
+ file_path = f.get("path") if isinstance(f, dict) else f
50
+ context = extract_text(file_path)
51
+ if context:
52
+ prompt_text = f"【参考文档背景】:\n{context}\n\n【深度分析指令】: {prompt_text}"
53
+
54
+ clean_messages.append({"role": "user", "content": prompt_text})
55
+
56
+ response = ""
57
+ try:
58
+ stream = client.chat_completion(clean_messages, max_tokens=4096, stream=True, temperature=0.5)
59
+ for msg in stream:
60
+ if hasattr(msg, 'choices') and len(msg.choices) > 0:
61
+ delta = msg.choices[0].delta
62
+ if hasattr(delta, 'content') and delta.content:
63
+ response += delta.content
64
+ yield response
65
+
66
+ if response:
67
+ yield response + "\n\n---\n> **OpenIPOS MegaNode | DeepSeek-V3.2-Exp 逻辑推理出口**"
68
+ except Exception as e:
69
+ yield f"⚠️ 算力调度提示: {str(e)}。由于 DeepSeek 节点极其繁忙,请刷新重试。"
70
+
71
+ # 3. UI 装修
72
+ with gr.Blocks(fill_height=True) as demo:
73
+ gr.HTML("""
74
+ <div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
75
+ <img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px; width: auto;">
76
+ <div>
77
+ <h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
78
+ <p style="margin: 0; color: #666;">全球领先法律与逻辑推理节点 (DeepSeek-V3.2-Exp)</p>
79
+ </div>
80
+ </div>
81
+ """)
82
+ gr.ChatInterface(chat_fn, multimodal=True)
83
+ gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Novita API Cluster")
84
+
85
+ demo.launch(theme=gr.themes.Soft())