Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load your fine-tuned model
|
| 7 |
+
model_name = "Pradnya27/codegpt-finetuned-code-generation"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
def generate_code(question):
|
| 13 |
+
prompt = "Generate code: " + question
|
| 14 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 15 |
+
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model.generate(
|
| 18 |
+
inputs["input_ids"],
|
| 19 |
+
max_new_tokens=200,
|
| 20 |
+
temperature=0.7,
|
| 21 |
+
do_sample=True,
|
| 22 |
+
pad_token_id=tokenizer.eos_token_id
|
| 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(
|
| 33 |
+
label="Your coding question",
|
| 34 |
+
placeholder="e.g. Write a function to check if a number is prime",
|
| 35 |
+
lines=3
|
| 36 |
+
),
|
| 37 |
+
outputs=gr.Code(
|
| 38 |
+
label="Generated Code",
|
| 39 |
+
language="python"
|
| 40 |
+
),
|
| 41 |
+
title="🤖 CodeGPT — AI Code Generator",
|
| 42 |
+
description="Fine-tuned CodeGPT model by Pradnya27. 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"],
|
| 46 |
+
["Write a function to check if a string is a palindrome"]
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
demo.launch()
|