news 2026/8/14 21:11:35

文生TikZ

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
文生TikZ

目录

Qwen-GeoGebra-Coder-7B

文生TikZ

Qwen/Qwen3-0.6B 要单独下载


Qwen-GeoGebra-Coder-7B

from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from llama_cpp import Llama import uvicorn import re app = FastAPI() # Standard CORS setup for local web tools app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"] ) # Load the fine-tuned math model # Utilizing the full 16GB VRAM of the RTX 4060 Ti llm = Llama( model_path="/home/khurram/ai_models/math_dataset/math_viz_Q4_K_M.gguf", n_gpu_layers=-1, n_ctx=2048, n_batch=512, temperature=0.1 ) def clean_and_format_ggb(raw_text): """ Standardizes model coordinates and brackets for GeoGebra. Converts [Cylinder[<0,0,0>, <3,0,0>, <0,10,0>]] to Cylinder((0,0,0), (0,10,0), 3.0) """ # 1. Clean up bracket variations text = raw_text.replace("<", "(").replace(">", ")") # 2. Extract all coordinate sets (x,y,z) coords = re.findall(r"\((-?\d+\.?\d*,\s*-?\d+\.?\d*,\s*-?\d+\.?\d*)\)", text) if len(coords) >= 3: bottom_pt = f"({coords[0]})" top_pt = f"({coords[2]})" # Extract scalar radius from the middle point radius_match = re.findall(r"[-+]?\d*\.\d+|\d+", coords[1]) radius = next((abs(float(n)) for n in radius_match if float(n) != 0), 3.0) return f"Cylinder({bottom_pt}, {top_pt}, {radius})" # Fallback for simple Sphere or direct commands return text.replace("[", "").replace("]", "").replace("<", "(").replace(">", ")").strip() @app.post("/ask") async def ask_geo(data: dict): user_prompt = data.get("prompt", "") # Step 1: Request the "Thought" (Mathematical Reasoning) prompt = f"<|im_start|>user\n{user_prompt}<|im_end|>\n<|im_start|>thought\n" thought_output = llm(prompt, max_tokens=150, stop=["<|im_end|>"]) thought_text = thought_output["choices"][0]["text"].strip() # Step 2: Request the "Assistant" (GeoGebra Code) command_prompt = f"{prompt}{thought_text}<|im_end|>\n<|im_start|>assistant\n" command_output = llm(command_prompt, max_tokens=150, stop=["<|im_end|>"]) assistant_raw = command_output["choices"][0]["text"].strip() # Final string formatting for the GeoGebra Applet final_cmds = clean_and_format_ggb(assistant_raw) return { "commands": final_cmds, "thought": thought_text } if __name__ == "__main__": uvicorn.run(app, host="127.0.0.1", port=8000)

文生TikZ

环境安装:

pip install peft pip install transformers==5.14.1

Qwen/Qwen3-0.6B 要单独下载

import torch from transformers import AutoModelForCausalLM, AutoTokenizer from peft import PeftModel BASE = "Qwen/Qwen3-0.6B" # match the adapter's base model REPO = "kyhe/qwen3-geotikz" # this Hub repo ADAPTER = "qwen3-pgf-geotikz" # subfolder for the v2 specialist tok = AutoTokenizer.from_pretrained(BASE) model = AutoModelForCausalLM.from_pretrained(BASE, dtype=torch.bfloat16) model = PeftModel.from_pretrained(model, REPO, subfolder=ADAPTER).eval() SYSTEM = ( "You are a geometry-to-TikZ compiler. Given a geometry scene described only through " "relationships and constraints (no explicit coordinates), you must derive the exact " "coordinates yourself and output a single valid TikZ/PGF figure that compiles and " "renders the described geometry. Output ONLY the TikZ code, starting with " "\\begin{tikzpicture} and ending with \\end{tikzpicture}. No prose, no markdown fences." ) scene = ("There is a circle centered at the origin with radius 3. Point A lies on the " "circle at 40 degrees. Point B lies on the circle at 200 degrees. Point M is the " "midpoint of segment AB.") msgs = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": f"Scene:\n{scene}\n\nReturn the TikZ figure."}] prompt = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True, enable_thinking=False) out = model.generate(**tok(prompt, return_tensors="pt").to(model.device), max_new_tokens=512, do_sample=False) print(tok.decode(out[0][tok(prompt, return_tensors="pt")["input_ids"].shape[1]:], skip_special_tokens=True))
cannot import name 'EncoderDecoderCache' from 'transformers'
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/14 21:11:25

国家工程技术研究中心申报条件有哪些

国家工程技术研究中心&#xff08;国家科技部审批&#xff09;的申报条件主要围绕申报单位的综合实力、科研能力、成果转化能力及组织管理等方面进行严格考核。具体申报条件如下&#xff1a;一、申报单位基本资质独立法人资格&#xff1a;申报单位需在中国境内注册&#xff0c;…

作者头像 李华
网站建设 2026/8/14 21:04:48

Python Django电商比价系统:从爬虫到智能Agent的全栈实践

这次我们来看一个基于Python的电商比价系统&#xff0c;它整合了Django框架、requests爬虫、MySQL数据库以及数据分析和可视化功能&#xff0c;甚至还引入了智能体&#xff08;Agent&#xff09;的概念。这个项目的核心价值在于&#xff0c;它不是一个简单的爬虫脚本&#xff0…

作者头像 李华
网站建设 2026/8/14 20:52:30

RTX Spark:在个人PC上搭建GPU加速的Spark大数据与AI开发环境

如果你是一名开发者&#xff0c;或者对AI应用开发感兴趣&#xff0c;最近可能被一个词刷屏了&#xff1a;RTX AI。从CES上铺天盖地的“AI PC”宣传&#xff0c;到各大OEM厂商纷纷推出搭载NPU的笔记本&#xff0c;再到英伟达在GTC上宣布的“RTX AI平台”&#xff0c;这个概念似乎…

作者头像 李华
网站建设 2026/8/14 20:45:45

三步装好Android Studio中文语言包,开发界面5分钟变中文

三步装好Android Studio中文语言包&#xff0c;开发界面5分钟变中文 【免费下载链接】AndroidStudioChineseLanguagePack AndroidStudio中文插件(官方修改版本&#xff09; 项目地址: https://gitcode.com/gh_mirrors/an/AndroidStudioChineseLanguagePack 第一次打开就…

作者头像 李华