Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Try to import transformers, but provide fallback
|
| 5 |
+
try:
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 7 |
+
TRANSFORMERS_AVAILABLE = True
|
| 8 |
+
except ImportError:
|
| 9 |
+
TRANSFORMERS_AVAILABLE = False
|
| 10 |
+
print("Transformers not available - running in demo mode")
|
| 11 |
+
|
| 12 |
+
def load_model():
|
| 13 |
+
"""Load the model if transformers is available"""
|
| 14 |
+
if not TRANSFORMERS_AVAILABLE:
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
model_id = "aliarsal1512/clarifai_java_code_commenter"
|
| 19 |
+
print(f"Loading model: {model_id}")
|
| 20 |
+
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 22 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 23 |
+
|
| 24 |
+
pipe = pipeline(
|
| 25 |
+
"text2text-generation",
|
| 26 |
+
model=model,
|
| 27 |
+
tokenizer=tokenizer,
|
| 28 |
+
max_length=64,
|
| 29 |
+
num_beams=1,
|
| 30 |
+
do_sample=False
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
print("✅ Model loaded successfully!")
|
| 34 |
+
return pipe
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"❌ Error loading model: {e}")
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
# Load model
|
| 40 |
+
pipe = load_model()
|
| 41 |
+
|
| 42 |
+
def generate_comment(code):
|
| 43 |
+
"""Generate comment for Java code"""
|
| 44 |
+
if pipe is None:
|
| 45 |
+
if not TRANSFORMERS_AVAILABLE:
|
| 46 |
+
return "⚠️ Transformers library not installed. Please check requirements.txt"
|
| 47 |
+
return "⚠️ Model failed to load. Check logs above."
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Clean and prepare code
|
| 51 |
+
code = code.strip()
|
| 52 |
+
if not code:
|
| 53 |
+
return "Please provide some Java code"
|
| 54 |
+
|
| 55 |
+
# Generate comment
|
| 56 |
+
result = pipe(code)
|
| 57 |
+
comment = result[0]['generated_text']
|
| 58 |
+
|
| 59 |
+
return comment
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"❌ Error: {str(e)}"
|
| 62 |
+
|
| 63 |
+
# Create Gradio interface
|
| 64 |
+
demo = gr.Interface(
|
| 65 |
+
fn=generate_comment,
|
| 66 |
+
inputs=gr.Textbox(
|
| 67 |
+
label="Java Code",
|
| 68 |
+
lines=10,
|
| 69 |
+
placeholder="Paste your Java code here...\nExample: public class Hello { public static void main(String[] args) { } }"
|
| 70 |
+
),
|
| 71 |
+
outputs=gr.Textbox(
|
| 72 |
+
label="Generated Comment",
|
| 73 |
+
lines=4
|
| 74 |
+
),
|
| 75 |
+
title="Java Code Comment Generator",
|
| 76 |
+
description="Generates comments for Java code using a fine-tuned T5 model",
|
| 77 |
+
examples=[
|
| 78 |
+
["public class Calculator {\n public int add(int a, int b) {\n return a + b;\n }\n}"],
|
| 79 |
+
["public class Main {\n public static void main(String[] args) {\n System.out.println(\"Hello, World!\");\n }\n}"]
|
| 80 |
+
],
|
| 81 |
+
theme="soft"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch(debug=True)
|