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