File size: 3,881 Bytes
6b27627
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import torch
import gc
import gradio as gr
import spaces  # Hugging Face ZeroGPU ็š„้—œ้ต
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from duckduckgo_search import DDGS
import requests
from bs4 import BeautifulSoup
import re

# ==========================================
# 1. ๆจกๅž‹่จญๅฎš่ˆ‡่ผ‰ๅ…ฅ
# ==========================================
MODEL_ID = "deepseek-ai/deepseek-math-7b-instruct"

# ่จญๅฎš 4-bit ้‡ๅŒ–้…็ฝฎ
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float16
)

print("๐Ÿš€ ๆญฃๅœจๅˆๅง‹ๅŒ–ๆจกๅž‹...")
try:
    tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
    model = AutoModelForCausalLM.from_pretrained(
        MODEL_ID,
        quantization_config=bnb_config,
        device_map="auto",
        low_cpu_mem_usage=True
    )
    print("โœ… ๆจกๅž‹่ผ‰ๅ…ฅๅฎŒๆˆ๏ผ")
except Exception as e:
    print(f"โŒ ๆจกๅž‹่ผ‰ๅ…ฅๅคฑๆ•—: {e}")

# ==========================================
# 2. ๆœๅฐ‹ๅŠŸ่ƒฝ (RAG)
# ==========================================
def search_academic_resources(query, max_results=3):
    print(f"๐Ÿ”Ž Searching for: {query}")
    context_data = ""
    try:
        with DDGS() as ddgs:
            results = list(ddgs.text(query, max_results=5))
            count = 0
            for r in results:
                if any(k in r['href'] for k in ["wikipedia", "edu", "arxiv", "math", "stackexchange"]):
                    try:
                        headers = {'User-Agent': 'Mozilla/5.0'}
                        page = requests.get(r['href'], headers=headers, timeout=5)
                        soup = BeautifulSoup(page.content, 'html.parser')
                        text = re.sub(r'\s+', ' ', soup.get_text())[:500]
                        context_data += f"Source: {r['title']}: {text}\n"
                        count += 1
                        if count >= max_results: break
                    except: continue
    except Exception as e:
        print(f"Search Error: {e}")
    
    return context_data if context_data else "No specific external context found."

# ==========================================
# 3. ๆŽจ็†ๆ ธๅฟƒ (ๅŠ ไธŠ @spaces.GPU ่ฃ้ฃพๅ™จ)
# ==========================================
# ้€™ๅ€‹่ฃ้ฃพๅ™จๅ‘Š่จด Hugging Face๏ผŒ้€™ๅ€‹ๅ‡ฝๅผ้œ€่ฆ็”จๅˆฐ GPU
@spaces.GPU
def math_solver(message, history):
    # 1. ๅ…ˆๆœๅฐ‹่ณ‡ๆ–™
    context = search_academic_resources(message)
    
    # 2. ็ต„ๅˆ Prompt
    prompt = f"""You are a math expert.
Context: {context}
User: {message}
Instruction: Please reason step-by-step to explain your solution. Use LaTeX for math equations.
Answer:"""

    inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
    
    # 3. ็”Ÿๆˆๅ›ž็ญ”
    with torch.no_grad():
        outputs = model.generate(
            **inputs, 
            max_new_tokens=1024, 
            temperature=0.1, 
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )
    
    response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Answer:")[-1].strip()
    
    # ๆธ…็†่จ˜ๆ†ถ้ซ”
    del inputs, outputs
    torch.cuda.empty_cache()
    gc.collect()
    
    return response

# ==========================================
# 4. ๅปบ็ซ‹ Gradio ็ถฒ้ ไป‹้ข
# ==========================================
# ไฝฟ็”จ ChatInterface ๅฟซ้€Ÿๅปบ็ซ‹ๅƒ ChatGPT ็š„ไป‹้ข
demo = gr.ChatInterface(
    fn=math_solver,
    title="๐Ÿค– DeepSeek Math RAG",
    description="่ผธๅ…ฅๆ•ธๅญธๅ•้กŒ๏ผŒๆˆ‘ๆœƒๆœๅฐ‹็ถฒ่ทฏ่ณ‡ๆ–™ไธฆ้€ฒ่กŒๆŽจ็†ๆฑ‚่งฃใ€‚(Powered by DeepSeek-Math-7B & DuckDuckGo)",
    examples=[
        "่จˆ็ฎ— f(x) = x^2 * e^(-x) ๅœจ x >= 0 ็š„ๆฅตๅคงๅ€ผ",
        "่ญ‰ๆ˜Ž่ณชๆ•ธๆœ‰็„ก็ชฎๅคšๅ€‹",
        "Explain the Riemann Hypothesis"
    ],
    theme="soft"
)

if __name__ == "__main__":
    demo.launch()