File size: 6,450 Bytes
392b11e 8dcbee7 392b11e 8dcbee7 392b11e 8dcbee7 392b11e 8dcbee7 392b11e | 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | import streamlit as st
from transformers import RobertaTokenizer, T5ForConditionalGeneration
import torch
# =========================================================
# PAGE CONFIG
# =========================================================
st.set_page_config(
page_title="Multilingual Code Comment Generator",
page_icon="π€",
layout="wide",
initial_sidebar_state="expanded"
)
# =========================================================
# CUSTOM CSS
# =========================================================
st.markdown(
"""
<style>
.main {
background-color: #0e1117;
color: white;
}
textarea {
font-size: 15px !important;
font-family: 'Consolas', monospace !important;
}
.stButton>button {
width: 100%;
background: linear-gradient(90deg, #4F46E5, #9333EA);
color: white;
border-radius: 12px;
height: 3em;
font-size: 16px;
font-weight: 600;
border: none;
}
.stButton>button:hover {
background: linear-gradient(90deg, #4338CA, #7E22CE);
color: white;
}
.result-box {
background-color: #161b22;
padding: 20px;
border-radius: 12px;
border: 1px solid #30363d;
color: #e6edf3;
font-size: 16px;
line-height: 1.6;
}
.header-title {
font-size: 42px;
font-weight: 800;
color: white;
}
.sub-text {
color: #9ca3af;
font-size: 18px;
}
</style>
""",
unsafe_allow_html=True
)
# =========================================================
# SIDEBAR
# =========================================================
with st.sidebar:
st.title("β‘ Project Info")
st.markdown("---")
st.markdown("### π€ Model")
st.write("Salesforce CodeT5")
st.markdown("### π BLEU Score")
st.write("24.44")
st.markdown("### π§ Architecture")
st.write("Encoder-Decoder Transformer")
st.markdown("### π₯ Frameworks")
st.write("PyTorch + HuggingFace")
st.markdown("### π Supported Languages")
st.write("Python")
st.write("Java (Upcoming)")
st.markdown("---")
st.markdown("### π Features")
st.write("β
AI-powered code summarization")
st.write("β
Automatic docstring generation")
st.write("β
Beam-search decoding")
st.write("β
Transformer fine-tuning")
st.write("β
HuggingFace model deployment")
# =========================================================
# MODEL CONFIG
# =========================================================
MODEL_NAME = "Eren18/multilingual-code-comment-generator-v2"
# =========================================================
# MODEL LOADING
# =========================================================
@st.cache_resource
def load_model():
tokenizer = RobertaTokenizer.from_pretrained(MODEL_NAME)
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)
return tokenizer, model, device
tokenizer, model, device = load_model()
# =========================================================
# INFERENCE FUNCTION
# =========================================================
def generate_comment(code):
inputs = tokenizer(
code,
return_tensors="pt",
truncation=True,
max_length=256
).to(device)
outputs = model.generate(
**inputs,
max_length=128,
num_beams=4,
no_repeat_ngram_size=2,
early_stopping=True
)
generated_comment = tokenizer.decode(
outputs[0],
skip_special_tokens=True
)
return generated_comment
# =========================================================
# HEADER
# =========================================================
st.markdown(
"""
<div class='header-title'>
π€ Multilingual Code Comment Generator
</div>
""",
unsafe_allow_html=True
)
st.markdown(
"""
<div class='sub-text'>
Generate AI-powered human-readable docstrings from raw source code using a fine-tuned CodeT5 Transformer.
</div>
""",
unsafe_allow_html=True
)
st.markdown("---")
# =========================================================
# MAIN LAYOUT
# =========================================================
left_col, right_col = st.columns(2)
# =========================================================
# LEFT PANEL
# =========================================================
with left_col:
st.subheader("π» Input Source Code")
language = st.selectbox(
"Select Programming Language",
["Python", "Java"]
)
sample_code = '''def calculate_discount(price, percent):
return price - (price * percent / 100)'''
code_input = st.text_area(
"Paste your function here",
value=sample_code,
height=400
)
generate_button = st.button("β‘ Generate Comment")
# =========================================================
# RIGHT PANEL
# =========================================================
with right_col:
st.subheader("π Generated Documentation")
if generate_button:
if code_input.strip() == "":
st.error("Please enter source code.")
else:
with st.spinner("Generating intelligent documentation..."):
generated = generate_comment(code_input)
st.success("Comment generated successfully!")
st.markdown(
f"""
<div class='result-box'>
{generated}
</div>
""",
unsafe_allow_html=True
)
st.download_button(
label="π₯ Download Comment",
data=generated,
file_name="generated_comment.txt",
mime="text/plain"
)
# =========================================================
# FOOTER
# =========================================================
st.markdown("---")
st.markdown(
"""
<center>
<h4>Built With β€οΈ Using</h4>
<p>
π€ HuggingFace |
π₯ PyTorch |
β‘ Streamlit |
π§ Transformers
</p>
</center>
""",
unsafe_allow_html=True
) |