|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
|
import gradio as gr |
|
|
|
|
|
model_name = "Salesforce/codet5-large" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) |
|
|
|
|
|
def generate_java_code(prompt): |
|
|
|
prompt = f"Generate a complete Java program:\n{prompt}" |
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True) |
|
outputs = model.generate( |
|
inputs["input_ids"], |
|
max_length=300, |
|
num_beams=5, |
|
early_stopping=True |
|
) |
|
|
|
code = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
if "public static void main" not in code: |
|
code = f"public class Main {{\n{code}\n}}" |
|
if code.strip().endswith("{"): |
|
code += "\n // Complete the logic here\n}" |
|
|
|
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=15, |
|
) |
|
btn = gr.Button("Kod Üret") |
|
btn.click(generate_java_code, inputs=prompt, outputs=output_code) |
|
|
|
demo.launch() |
|
|
|
|