File size: 4,024 Bytes
9f9afd5
3298b63
9f9afd5
 
 
3298b63
94359fc
9f9afd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94359fc
 
9f9afd5
 
 
3298b63
9f9afd5
 
 
 
 
 
 
 
94359fc
9f9afd5
 
 
 
 
 
 
 
 
 
94359fc
9f9afd5
 
 
 
 
 
94359fc
 
 
 
 
 
 
 
 
 
 
 
 
9f9afd5
 
 
 
 
 
94359fc
9f9afd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import gradio as gr
import fitz
import docx
from huggingface_hub import InferenceClient

# 1. 引擎配置:GPT-OSS-120B (通用重型算力)
model_id = "openai/gpt-oss-120b"
hf_token = os.getenv("HF_TOKEN")

client = InferenceClient(
    model=model_id, 
    token=hf_token,
    headers={"Authorization": f"Bearer {hf_token}"} if hf_token else None
)

def extract_text(file_path):
    if not file_path: return ""
    try:
        ext = str(file_path).lower()
        if ext.endswith(".pdf"):
            doc = fitz.open(file_path)
            text = "".join([page.get_text() for page in doc])
            doc.close()
            return text[:15000]
        elif ext.endswith(".docx"):
            doc = docx.Document(file_path)
            return "\n".join([p.text for p in doc.paragraphs])[:15000]
        elif ext.endswith(".txt"):
            with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
                return f.read()[:15000]
        return ""
    except:
        return ""

def chat_fn(message, history):
    clean_messages = []
    # 设定通用大师身份
    clean_messages.append({"role": "system", "content": "You are a professional AI assistant. Please output responses in standard Markdown format (including tables, bold text, etc.)."})
    
    for turn in history:
        clean_messages.append({"role": turn["role"], "content": str(turn["content"])})
    
    prompt_text = message.get("text", "")
    files = message.get("files", [])
    
    if files:
        f = files[0]
        file_path = f.get("path") if isinstance(f, dict) else f
        context = extract_text(file_path)
        if context:
            prompt_text = f"【参考背景文档】:\n{context}\n\n【用户指令】: {prompt_text}"
        
    clean_messages.append({"role": "user", "content": prompt_text})

    response = ""
    try:
        # GPT-OSS-120B 专用流式处理
        stream = client.chat_completion(
            clean_messages, 
            max_tokens=4096, 
            stream=True, 
            temperature=0.4
        )
        
        for msg in stream:
            if hasattr(msg, 'choices') and len(msg.choices) > 0:
                delta_content = msg.choices[0].delta.content
                if delta_content:
                    # --- 核心修复:针对性清洗 JSON 列表格式数据 ---
                    # 120B 经常返回 [{'text': '...'}],我们需要把里面的文字抠出来
                    content = ""
                    if isinstance(delta_content, list):
                        for item in delta_content:
                            if isinstance(item, dict) and 'text' in item:
                                content += item['text']
                    elif isinstance(delta_content, dict):
                        content = delta_content.get("text", "")
                    else:
                        content = str(delta_content)
                    
                    response += content
                    yield response
        
        if response:
            yield response + "\n\n---\n> **OpenIPOS MegaNode | GPT-OSS-120B 通用算力出口**"
            
    except Exception as e:
        yield f"⚠️ 算力调度提示: {str(e)}。由于 120B 模型算力强劲但协议特殊,请尝试重新发送指令。"

# 3. 统一品牌装修
with gr.Blocks(fill_height=True) as demo:
    gr.HTML("""
        <div style="display: flex; align-items: center; gap: 20px; padding: 10px;">
            <img src="https://s3.bmp.ovh/2026/03/30/rIAGKg0O.png" style="height: 60px; width: auto;">
            <div>
                <h1 style="margin: 0; font-size: 24px;">OpenIPOS MegaNode</h1>
                <p style="margin: 0; color: #666;">全球领先开源大模型算力出口 (GPT-OSS-120B)</p>
            </div>
        </div>
    """)
    gr.ChatInterface(chat_fn, multimodal=True)
    gr.Markdown("© 2026 OpenIPOS Global Limited. 算力驱动:Hugging Face Auto-Inference Providers")

demo.launch(theme=gr.themes.Soft())