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()