Spaces:
Sleeping
Sleeping
File size: 1,824 Bytes
9cd4327 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# -*- coding: utf-8 -*-
"""app.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1VgZCCaMxdd-9oiW3-Kme4eOwUvtDo_wr
"""
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
MODEL_ID = "Yenes/flan-t5-python-explainer"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSeq2SeqLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto",
)
MAX_INPUT_LENGTH = 256
def explain_code(code: str, max_new_tokens: int = 128):
"""Python kodunu Türkçe ve satır satır açıklayan fonksiyon."""
if not code.strip():
return "Lütfen açıklanacak bir Python kodu girin."
instruction = (
"Türkçe ve anlaşılır bir şekilde, aşağıdaki Python kodunu satır satır açıkla:\n"
f"{code}"
)
inputs = tokenizer(
instruction,
return_tensors="pt",
truncation=True,
max_length=MAX_INPUT_LENGTH,
).to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
num_beams=4,
early_stopping=True,
)
explanation = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
return explanation
demo = gr.Interface(
fn=explain_code,
inputs=[
gr.Textbox(lines=10, label="Python Kodu"),
gr.Slider(32, 512, value=128, step=16, label="Maksimum yeni token sayısı"),
],
outputs=gr.Textbox(lines=14, label="Türkçe Açıklama"),
title="Python Kod Açıklayıcı (FLAN-T5)",
description="FLAN-T5 tabanlı, Türkçe Python kod açıklama modeli.",
)
if __name__ == "__main__":
demo.launch() |