misalsathsara commited on
Commit
6a4eb93
·
verified ·
1 Parent(s): 5ee4d73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- os.environ["HF_HOME"] = "/tmp/hf"
3
 
4
  from fastapi import FastAPI
5
  from pydantic import BaseModel
@@ -7,17 +7,23 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
7
  import torch
8
  import re
9
 
10
- app = FastAPI()
 
 
 
 
11
 
12
- # Load model and tokenizer from Hugging Face
13
  model_id = "misalsathsara/phi1.5-js-codegen"
14
  tokenizer = AutoTokenizer.from_pretrained(model_id)
15
  model = AutoModelForCausalLM.from_pretrained(model_id)
 
 
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
  model.to(device)
18
  model.eval()
19
 
20
- # Your system prompt
21
  system_prompt = """
22
  You are a smart javascript assistant that only generates only the best simple javascript functions without any comments like this:
23
  function transform(row) {
@@ -41,22 +47,21 @@ Don't add any markdown block markers either.
41
  Every function must end with return row;
42
  """
43
 
44
- # Define the expected request body
45
  class RequestData(BaseModel):
46
  instruction: str
47
 
48
- # POST endpoint
49
- @app.post("/generate")
50
  def generate_code(data: RequestData):
51
  instruction = data.instruction
52
- full_prompt = system_prompt + f"\n### Instruction:\n{instruction}\n\n### Response:\n"
53
-
54
- # Tokenize input
55
- input_ids = tokenizer(full_prompt, return_tensors="pt").input_ids.to(device)
56
-
57
  with torch.no_grad():
58
- output_ids = model.generate(
59
- input_ids,
60
  max_new_tokens=200,
61
  temperature=0.3,
62
  top_k=50,
@@ -65,10 +70,6 @@ def generate_code(data: RequestData):
65
  pad_token_id=tokenizer.eos_token_id
66
  )
67
 
68
- generated_text = tokenizer.decode(output_ids[0][input_ids.shape[-1]:], skip_special_tokens=True)
69
-
70
- # Extract clean JS function
71
- match = re.search(r"function\s*\(.*?\)\s*{.*?return row;\s*}", generated_text, re.DOTALL)
72
- clean_output = match.group(0).strip() if match else generated_text.strip()
73
-
74
- return {"result": clean_output}
 
1
  import os
2
+ os.environ["HF_HOME"] = "/tmp/hf" # Prevents write errors on Hugging Face Spaces
3
 
4
  from fastapi import FastAPI
5
  from pydantic import BaseModel
 
7
  import torch
8
  import re
9
 
10
+ app = FastAPI(
11
+ title="JavaScript Code Generator API",
12
+ description="Generate simple JavaScript functions from natural language instructions",
13
+ version="1.0"
14
+ )
15
 
16
+ # Load model and tokenizer
17
  model_id = "misalsathsara/phi1.5-js-codegen"
18
  tokenizer = AutoTokenizer.from_pretrained(model_id)
19
  model = AutoModelForCausalLM.from_pretrained(model_id)
20
+
21
+ # Device setup
22
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
  model.to(device)
24
  model.eval()
25
 
26
+ # Prompt Template
27
  system_prompt = """
28
  You are a smart javascript assistant that only generates only the best simple javascript functions without any comments like this:
29
  function transform(row) {
 
47
  Every function must end with return row;
48
  """
49
 
50
+ # Input schema
51
  class RequestData(BaseModel):
52
  instruction: str
53
 
54
+ # Main route
55
+ @app.post("/generate", summary="Generate JavaScript code", tags=["Code Generation"])
56
  def generate_code(data: RequestData):
57
  instruction = data.instruction
58
+ full_prompt = f"{system_prompt}\n### Instruction:\n{instruction}\n\n### Response:\n"
59
+
60
+ inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
61
+
 
62
  with torch.no_grad():
63
+ outputs = model.generate(
64
+ **inputs,
65
  max_new_tokens=200,
66
  temperature=0.3,
67
  top_k=50,
 
70
  pad_token_id=tokenizer.eos_token_id
71
  )
72
 
73
+ result = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
74
+ match = re.search(r"function\s*\(.*?\)\s*{.*?return row;\s*}", result, re.DOTALL)
75
+ return {"result": match.group(0).strip() if match else result.strip()}