Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,34 +2,32 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
model_id = "lakshraina2/
|
| 7 |
|
| 8 |
-
print("
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
prompt = f"### Instruction:\nSolve this LeetCode problem:\n{problem_text}\n\n### Response:\n"
|
| 15 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
solution = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
code_only = solution.split("### Response:\n")[1].strip()
|
| 28 |
-
except IndexError:
|
| 29 |
-
code_only = solution
|
| 30 |
-
|
| 31 |
-
return code_only
|
| 32 |
|
| 33 |
-
# Gradio
|
| 34 |
-
iface = gr.Interface(fn=
|
| 35 |
iface.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
# Replace with your actual merged model repo
|
| 6 |
+
model_id = "lakshraina2/leetcode-coder-1.5B"
|
| 7 |
|
| 8 |
+
print("Loading model on CPU...")
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_id,
|
| 12 |
+
torch_dtype=torch.float32, # CPU needs float32
|
| 13 |
+
device_map={"": "cpu"} # Force CPU
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
def solve(problem_text):
|
| 17 |
prompt = f"### Instruction:\nSolve this LeetCode problem:\n{problem_text}\n\n### Response:\n"
|
| 18 |
+
inputs = tokenizer(prompt, return_tensors="pt") # No .to("cuda")!
|
| 19 |
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model.generate(
|
| 22 |
+
**inputs,
|
| 23 |
+
max_new_tokens=512,
|
| 24 |
+
temperature=0.2,
|
| 25 |
+
do_sample=True
|
| 26 |
+
)
|
| 27 |
|
| 28 |
solution = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
+
return solution.split("### Response:\n")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Gradio 4 interface
|
| 32 |
+
iface = gr.Interface(fn=solve, inputs="text", outputs="text")
|
| 33 |
iface.launch()
|