|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
|
import gradio as gr |
|
|
|
|
|
model_name = "Salesforce/codet5-base" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) |
|
|
|
|
|
def generate_java_code(prompt): |
|
|
|
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True) |
|
|
|
outputs = model.generate( |
|
inputs["input_ids"], |
|
max_length=150, |
|
num_beams=4, |
|
early_stopping=True |
|
) |
|
|
|
code = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
return code |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("<h1 style='text-align: center;'>Java Kod Üretici (CodeT5)</h1>") |
|
prompt = gr.Textbox(label="Doğal Dil Girdisi", placeholder="Örnek: Write a Java program to find the larger of two numbers.") |
|
output_code = gr.Textbox(label="Üretilen Java Kodu", lines=10) |
|
btn = gr.Button("Kod Üret") |
|
btn.click(generate_java_code, inputs=prompt, outputs=output_code) |
|
|
|
demo.launch() |
|
|