Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_id = "key-life/codegen-alpaca-1b"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
|
| 9 |
+
|
| 10 |
+
def generate_code(prompt):
|
| 11 |
+
formatted_prompt = f"### Instruction:\n{prompt}\n\n### Response:\n"
|
| 12 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
|
| 13 |
+
outputs = model.generate(**inputs, max_new_tokens=256)
|
| 14 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 15 |
+
|
| 16 |
+
interface = gr.Interface(
|
| 17 |
+
fn=generate_code,
|
| 18 |
+
inputs=gr.Textbox(lines=5, label="Instruction"),
|
| 19 |
+
outputs=gr.Code(label="Generated Code"),
|
| 20 |
+
title="CodeGen Alpaca 1B",
|
| 21 |
+
description="Give a coding instruction and get code as response from a fine-tuned StarCoderBase-1B model."
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
interface.launch()
|