Spaces:
Paused
Paused
Upload 3 files
Browse files
app.py
CHANGED
|
@@ -1,42 +1,32 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import login
|
| 4 |
-
from transformers import pipeline
|
| 5 |
import torch
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
| 8 |
# Load the gated model
|
| 9 |
#model_name = "RickyDeSkywalker/TheoremLlama"
|
| 10 |
-
model_name = "unsloth/Llama-3.2-1B-Instruct"
|
| 11 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
#model_name = "internlm/internlm2-math-plus-7b"
|
| 13 |
#model_name = "deepseek-ai/DeepSeek-Prover-V1.5-RL"
|
|
|
|
|
|
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
#login(HF_TOKEN)
|
| 16 |
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 18 |
-
# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and might cause OOM Error.
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16, device_map="auto").eval()
|
| 20 |
-
terminators = [tokenizer.eos_token_id,
|
| 21 |
-
tokenizer.convert_tokens_to_ids("<|eot_id|>"),
|
| 22 |
-
tokenizer.convert_tokens_to_ids("<|reserved_special_token_26|>")]
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#generator = pipeline('text-generation', model=model_name, trust_remote_code=True, token=HF_TOKEN)
|
| 26 |
|
| 27 |
# Function for generating Lean 4 code
|
| 28 |
@torch.inference_mode()
|
| 29 |
-
def generate_lean4_code(prompt):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#print(response, history)
|
| 34 |
-
#return response
|
| 35 |
chat = [
|
| 36 |
{"role": "system", "content": "You are a Lean4 expert who can write good Lean4 code based on natural language mathematical theorem and proof"},
|
| 37 |
{"role": "user", "content": prompt},
|
| 38 |
]
|
| 39 |
-
input_ids = tokenizer.apply_chat_template(chat, return_tensors="pt").to(device)
|
| 40 |
results = model.generate(input_ids, max_new_tokens=1024, eos_token_id=terminators, do_sample=True, temperature=0.85, top_p=0.9)
|
| 41 |
result_str = tokenizer.decode(results[0], skip_special_tokens=True)
|
| 42 |
return result_str
|
|
@@ -52,8 +42,7 @@ interface = gr.Interface(
|
|
| 52 |
placeholder="Prove that the sum of two even numbers is even.",
|
| 53 |
lines=4
|
| 54 |
),
|
| 55 |
-
|
| 56 |
-
outputs=gr.Code(label="Generated Lean 4 Code"),
|
| 57 |
title=title,
|
| 58 |
description=description
|
| 59 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import login
|
|
|
|
| 4 |
import torch
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
| 7 |
# Load the gated model
|
| 8 |
#model_name = "RickyDeSkywalker/TheoremLlama"
|
|
|
|
|
|
|
| 9 |
#model_name = "internlm/internlm2-math-plus-7b"
|
| 10 |
#model_name = "deepseek-ai/DeepSeek-Prover-V1.5-RL"
|
| 11 |
+
model_name = "unsloth/Llama-3.2-1B-Instruct"
|
| 12 |
+
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
#login(HF_TOKEN)
|
| 15 |
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
|
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16, device_map="auto").eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Function for generating Lean 4 code
|
| 20 |
@torch.inference_mode()
|
| 21 |
+
def generate_lean4_code(prompt: str):
|
| 22 |
+
terminators = [tokenizer.eos_token_id,
|
| 23 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>"),
|
| 24 |
+
tokenizer.convert_tokens_to_ids("<|reserved_special_token_26|>")]
|
|
|
|
|
|
|
| 25 |
chat = [
|
| 26 |
{"role": "system", "content": "You are a Lean4 expert who can write good Lean4 code based on natural language mathematical theorem and proof"},
|
| 27 |
{"role": "user", "content": prompt},
|
| 28 |
]
|
| 29 |
+
input_ids = tokenizer.apply_chat_template(chat, return_tensors="pt").to(model.device)
|
| 30 |
results = model.generate(input_ids, max_new_tokens=1024, eos_token_id=terminators, do_sample=True, temperature=0.85, top_p=0.9)
|
| 31 |
result_str = tokenizer.decode(results[0], skip_special_tokens=True)
|
| 32 |
return result_str
|
|
|
|
| 42 |
placeholder="Prove that the sum of two even numbers is even.",
|
| 43 |
lines=4
|
| 44 |
),
|
| 45 |
+
outputs=gr.Code(label="Generated Lean 4 Code"), # language="lean" unsupported in Gradio...
|
|
|
|
| 46 |
title=title,
|
| 47 |
description=description
|
| 48 |
)
|