Spaces:
Running
Running
Commit
·
ea0af80
1
Parent(s):
98fca07
Initial commit
Browse files- app.py +19 -0
- model.py +16 -0
- requirements.txt +4 -0
- runtime.txt +1 -0
app.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from model import generate_code
|
3 |
+
|
4 |
+
app = FastAPI()
|
5 |
+
|
6 |
+
@app.get("/")
|
7 |
+
def home():
|
8 |
+
return {"message": "Code Generation API is running!"}
|
9 |
+
|
10 |
+
@app.post("/generate")
|
11 |
+
def generate(prompt: str, max_tokens: int = 256):
|
12 |
+
if not prompt:
|
13 |
+
raise HTTPException(status_code=400, detail="Prompt cannot be empty.")
|
14 |
+
|
15 |
+
try:
|
16 |
+
code = generate_code(prompt, max_tokens)
|
17 |
+
return {"generated_code": code}
|
18 |
+
except Exception as e:
|
19 |
+
raise HTTPException(status_code=500, detail=f"Internal Server Error: {str(e)}")
|
model.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import torch
|
3 |
+
|
4 |
+
# Load the pre-trained model
|
5 |
+
MODEL_NAME = "bigcode/starcoder"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, device_map="auto")
|
8 |
+
|
9 |
+
def generate_code(prompt: str, max_tokens: int = 256):
|
10 |
+
"""Generates code based on the input prompt."""
|
11 |
+
if not prompt.strip():
|
12 |
+
return "Error: Empty prompt provided."
|
13 |
+
|
14 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
+
output = model.generate(**inputs, max_new_tokens=max_tokens)
|
16 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
fastapi
|
4 |
+
uvicorn
|
runtime.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python-3.9
|