lakshraina2 commited on
Commit
bbcc54c
·
verified ·
1 Parent(s): 47131aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
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/leetcodeAI"
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
- # Basic check to ensure input isn't empty
18
- if not problem_text:
19
- return "No problem text detected."
20
-
21
- prompt = f"### Instruction:\nSolve this LeetCode problem:\n{problem_text}\n\n### Response:\n"
 
22
  inputs = tokenizer(prompt, return_tensors="pt")
23
 
 
24
  with torch.no_grad():
25
  outputs = model.generate(
26
- **inputs,
27
- max_new_tokens=512,
28
- temperature=0.2,
29
- do_sample=True
 
 
 
30
  )
31
 
32
- solution = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
- return solution.split("### Response:\n")[-1].strip()
34
-
35
- # THE FIX: We use gr.Interface but explicitly name the API endpoint 'predict'
36
- demo = gr.Interface(
37
- fn=solve,
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()