"""Gradio Frontend Application.
This module provides a web-based user interface for the code comment classification API.
Users can input code comments and get real-time predictions through an interactive interface.
"""
import os
import gradio as gr
import requests
# API configuration for deploy
API_URL = os.getenv("API_URL", "https://se4ai2526-uniba-nygaard-nygaardcodecomment-backend.hf.space/")
def get_categories_for_language(language: str) -> str:
"""Get supported categories for a specific programming language.
Args:
language: The programming language
Returns:
Formatted string with supported categories
"""
categories = {
"Java": [
"summary: A brief description of the behavior of the code",
"Ownership: Describes the authors and ownership",
"Expand: Aims to describe the associated code",
"usage: Describes how to use the code",
"Pointer: Points to related code or resources",
"deprecation: Indicates deprecated code",
"rational: Explains the reasoning behind the implementation",
],
"Python": [
"Usage: Describes usage of the code",
"Parameters: Documents function/method parameters",
"DevelopmentNotes: Contains notes for developers",
"Expand: Provides detailed explanations",
"Summary: Summarizes the functionality",
],
"Pharo": [
"Keyimplementationpoints: Highlights key implementation details",
"Example: Provides code examples",
"Responsibilities: Describes object responsibilities",
"Intent: Explains the intent or purpose",
"Keymessages: Documents key messages or methods",
"Collaborators: Lists collaborating objects/classes",
],
}
lang_categories = categories.get(language, [])
if lang_categories:
return f"**Supported Categories for {language}:**\n" + "\n".join(
f"- {cat}" for cat in lang_categories
)
return "**Supported Categories:** Not available"
def predict_gradio(text: str, class_name: str, language: str) -> str:
"""Gradio interface function for single text prediction.
Args:
text: The code comment to classify
class_name: The name of the class containing the comment
language: The programming language
Returns:
Formatted HTML string with prediction results
"""
if not text.strip():
return """
⚠️ Please enter a code comment to classify.
"""
if not class_name.strip():
return """
⚠️ Please enter the class name containing the comment.
"""
try:
# Call the API with separate texts and class_names
# Backend will combine them as "comment | class_name" before passing to the model
response = requests.post(
f"{API_URL}/predict",
json={
"texts": [text],
"class_names": [class_name],
"language": language.lower(),
"model_type": "catboost",
},
timeout=30,
)
if response.status_code == 200:
data = response.json()
results = data.get("data", {}).get("results", [])
if results and len(results) > 0:
labels = results[0].get("labels", [])
if labels:
labels_html = "".join(
[
f''
f"🏷️ {label}"
for label in labels
]
)
return f"""
✅ Prediction Successful
{labels_html}
"""
return """
📊 No labels predicted (below threshold)
"""
return """
🔍 No prediction results available
"""
else:
return f"""
❌ API Error: {response.status_code}
{response.text}
"""
except requests.exceptions.ConnectionError:
return f"""
🌐 Connection Error
Cannot connect to API at {API_URL}
"""
except requests.exceptions.Timeout:
return """
⏱️ Timeout Error
The request took too long
"""
except Exception as e:
return f"""
⚠️ Unexpected Error
{str(e)}
"""
def update_categories(language: str) -> str:
"""Update the categories display when language changes.
Args:
language: The selected programming language
Returns:
Updated description text with categories
"""
return get_categories_for_language(language)
# Create Gradio interface with dynamic categories
with gr.Blocks(title="🔍 Nygaard Code Comment Classifier") as gradio_app:
gr.Markdown("# 🔍 Nygaard Code Comment Classifier")
gr.Markdown("Classify code comments into multiple categories using machine learning.")
# Language selector
language_dropdown = gr.Dropdown(
choices=["Java", "Python", "Pharo"], label="Programming Language", value="Python"
)
# Dynamic categories display
categories_display = gr.Markdown(value=get_categories_for_language("Python"))
# Connect language change to categories update
language_dropdown.change(
fn=update_categories, inputs=language_dropdown, outputs=categories_display
)
# Input components
with gr.Row():
text_input = gr.Textbox(
label="Code Comment", placeholder="Enter your code comment here...", lines=5
)
with gr.Row():
class_name_input = gr.Textbox(
label="Class Name", placeholder="Enter the class name...", lines=1
)
# Output
output_display = gr.HTML(label="Prediction Result")
# Buttons
with gr.Row():
clear_btn = gr.Button("Clear", variant="secondary")
submit_btn = gr.Button("Submit", variant="primary")
gr.Examples(
examples=[
["@deprecated Use newMethod() instead", "Java"],
["This method calculates the factorial of a number", "Python"],
["Returns the sum of all elements in the collection", "Pharo"],
],
inputs=[text_input, language_dropdown],
)
# Connect clear button to reset inputs
clear_btn.click(
fn=lambda: ("", "", "Python"),
inputs=[],
outputs=[text_input, class_name_input, language_dropdown],
)
# Connect submit button to prediction function
submit_btn.click(
fn=predict_gradio,
inputs=[text_input, class_name_input, language_dropdown],
outputs=output_display,
)
if __name__ == "__main__":
gradio_app.launch(server_name="0.0.0.0", server_port=9001)