# -*- 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()