Spaces:
Runtime error
Runtime error
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() |