Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,9 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
-
model_id = "lakshraina2/
|
| 6 |
-
|
| 7 |
-
print("Loading model on CPU...")
|
| 8 |
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=False)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_id,
|
|
@@ -14,30 +13,32 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 14 |
)
|
| 15 |
|
| 16 |
def solve(problem_text):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
prompt = f"
|
|
|
|
| 22 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 23 |
|
|
|
|
| 24 |
with torch.no_grad():
|
| 25 |
outputs = model.generate(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
inputs=gr.Textbox(),
|
| 39 |
-
outputs=gr.Textbox(),
|
| 40 |
-
api_name="predict" # This matches the /predict in your content.js URL
|
| 41 |
-
)
|
| 42 |
|
|
|
|
| 43 |
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
model_id = "lakshraina2/leetcode-coder-1.5B"
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
print("Loading model...")
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=False)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_id,
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
def solve(problem_text):
|
| 16 |
+
if not problem_text or len(problem_text) < 10:
|
| 17 |
+
return "// Error: Problem text too short or not scraped correctly."
|
| 18 |
+
|
| 19 |
+
# Standard Alpaca/Llama prompt format
|
| 20 |
+
prompt = f"Below is a LeetCode problem. Write a complete Python solution.\n\n### Problem:\n{problem_text}\n\n### Solution:\n"
|
| 21 |
+
|
| 22 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 23 |
|
| 24 |
+
# Generate with specific constraints to prevent empty output
|
| 25 |
with torch.no_grad():
|
| 26 |
outputs = model.generate(
|
| 27 |
+
input_ids=inputs["input_ids"],
|
| 28 |
+
attention_mask=inputs["attention_mask"],
|
| 29 |
+
max_new_tokens=1024, # Increased for complex problems
|
| 30 |
+
min_new_tokens=50, # Force the model to talk
|
| 31 |
+
temperature=0.1, # Lower temperature = more focused/less random
|
| 32 |
+
do_sample=True,
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
| 34 |
)
|
| 35 |
|
| 36 |
+
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
# Extract only the part after our '### Solution:' marker
|
| 39 |
+
if "### Solution:" in full_text:
|
| 40 |
+
return full_text.split("### Solution:")[-1].strip()
|
| 41 |
+
return full_text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
demo = gr.Interface(fn=solve, inputs="text", outputs="text", api_name="predict")
|
| 44 |
demo.launch()
|