Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
tokenizer.pad_token = tokenizer.eos_token
|
| 10 |
-
model
|
| 11 |
|
| 12 |
def generate_code(question):
|
| 13 |
prompt = "Generate code: " + question
|
|
@@ -23,10 +25,8 @@ def generate_code(question):
|
|
| 23 |
)
|
| 24 |
|
| 25 |
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
-
# Return only the generated part after the prompt
|
| 27 |
return generated[len(prompt):]
|
| 28 |
|
| 29 |
-
# Build the UI
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=generate_code,
|
| 32 |
inputs=gr.Textbox(
|
|
@@ -38,8 +38,8 @@ demo = gr.Interface(
|
|
| 38 |
label="Generated Code",
|
| 39 |
language="python"
|
| 40 |
),
|
| 41 |
-
title="
|
| 42 |
-
description="Fine-tuned CodeGPT
|
| 43 |
examples=[
|
| 44 |
["Write a function to reverse a string"],
|
| 45 |
["Write a function to find the largest number in a list"],
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
+
from peft import PeftModel
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# Load LoRA model
|
| 8 |
+
base_model = AutoModelForCausalLM.from_pretrained("microsoft/CodeGPT-small-py")
|
| 9 |
+
model = PeftModel.from_pretrained(base_model, "Pradnya27/codegpt-lora-code-generation")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/CodeGPT-small-py")
|
| 11 |
tokenizer.pad_token = tokenizer.eos_token
|
| 12 |
+
model.eval()
|
| 13 |
|
| 14 |
def generate_code(question):
|
| 15 |
prompt = "Generate code: " + question
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 28 |
return generated[len(prompt):]
|
| 29 |
|
|
|
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=generate_code,
|
| 32 |
inputs=gr.Textbox(
|
|
|
|
| 38 |
label="Generated Code",
|
| 39 |
language="python"
|
| 40 |
),
|
| 41 |
+
title="⚡ CodeGPT LoRA — AI Code Generator",
|
| 42 |
+
description="Fine-tuned CodeGPT with LoRA by Pradnya27. 275x smaller than full fine-tuning! Ask any coding question and get Python code.",
|
| 43 |
examples=[
|
| 44 |
["Write a function to reverse a string"],
|
| 45 |
["Write a function to find the largest number in a list"],
|