added api
Browse files
app.py
CHANGED
|
@@ -2,15 +2,28 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
def generate_code(prompt):
|
| 10 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(
|
| 11 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
| 12 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load model
|
| 6 |
+
model_name = "deepseek-ai/deepseek-coder-1.3b-base" # use smaller for free CPU
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
device_map="auto",
|
| 11 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
# Define inference function
|
| 15 |
def generate_code(prompt):
|
| 16 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 17 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
| 18 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
|
| 20 |
+
# Create Gradio interface
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=generate_code,
|
| 23 |
+
inputs=gr.Textbox(lines=5, label="Prompt"),
|
| 24 |
+
outputs=gr.Textbox(lines=10, label="Generated Code"),
|
| 25 |
+
title="AI Coder Agent"
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
# Important: launch with api=True so Hugging Face creates an API
|
| 29 |
+
iface.launch(api=True)
|