Spaces:
Runtime error
Runtime error
Download app.py from xsf0508/DeepSeek-Math-RAG: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/spaces/xsf0508/DeepSeek-Math-RAG/resolve/main/app.py
- Command line
-
hf download hf://spaces/xsf0508/DeepSeek-Math-RAG/app.py
-
curl -L -o app.py https://huggingface.co/spaces/xsf0508/DeepSeek-Math-RAG/resolve/main/app.py
3.88 kB
| 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 | |
| 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() |