Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,77 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from flask import Flask, request, jsonify
|
| 3 |
-
from transformers import RobertaTokenizer, T5ForConditionalGeneration
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
app = Flask(__name__)
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
tokenizer = RobertaTokenizer.from_pretrained(
|
| 21 |
-
model = T5ForConditionalGeneration.from_pretrained(
|
| 22 |
-
print("Model loaded!")
|
| 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 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from transformers import RobertaTokenizer, T5ForConditionalGeneration
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# --- CRITICAL CHANGE ---
|
| 9 |
+
# Instead of your local folder, we point to a PUBLIC Expert Model
|
| 10 |
+
# This model has read millions of commit messages and knows exactly what to do.
|
| 11 |
+
MODEL_NAME = "ncoop57/commit-t5"
|
| 12 |
+
|
| 13 |
+
print(f"--- AI Commit Generator Server ---")
|
| 14 |
+
print(f"Downloading/Loading Expert Model: {MODEL_NAME}")
|
| 15 |
+
|
| 16 |
+
device = "cpu" # HF Spaces free tier is CPU
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# This will download the model automatically the first time it runs
|
| 20 |
+
tokenizer = RobertaTokenizer.from_pretrained(MODEL_NAME)
|
| 21 |
+
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME).to(device)
|
| 22 |
+
print("✅ Expert Model loaded successfully!")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"❌ Error loading model: {e}")
|
| 25 |
+
exit(1)
|
| 26 |
+
|
| 27 |
+
def generate_summary(diff_text):
|
| 28 |
+
if not diff_text or len(diff_text.strip()) < 5:
|
| 29 |
+
return "Update file"
|
| 30 |
+
|
| 31 |
+
# Preprocess: "commit-t5" just expects the raw code diff, no "Summarize:" prefix needed usually,
|
| 32 |
+
# but let's keep it simple.
|
| 33 |
+
input_text = diff_text + " </s>"
|
| 34 |
+
|
| 35 |
+
input_ids = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True).input_ids.to(device)
|
| 36 |
+
|
| 37 |
+
outputs = model.generate(
|
| 38 |
+
input_ids,
|
| 39 |
+
max_length=80, # Commits are usually short
|
| 40 |
+
min_length=5,
|
| 41 |
+
num_beams=5,
|
| 42 |
+
early_stopping=True,
|
| 43 |
+
no_repeat_ngram_size=2 # Stops it from saying "update update update"
|
| 44 |
+
)
|
| 45 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
+
|
| 47 |
+
@app.route('/generate', methods=['POST'])
|
| 48 |
+
def generate_commit():
|
| 49 |
+
data = request.json
|
| 50 |
+
files = data.get('files', [])
|
| 51 |
+
|
| 52 |
+
if not files:
|
| 53 |
+
return jsonify({"commit_message": ""})
|
| 54 |
+
|
| 55 |
+
final_message_parts = []
|
| 56 |
+
|
| 57 |
+
for file_obj in files:
|
| 58 |
+
name = file_obj.get('name', 'Unknown File')
|
| 59 |
+
diff = file_obj.get('diff', '')
|
| 60 |
+
|
| 61 |
+
# Skip binary files or huge diffs
|
| 62 |
+
if len(diff) > 4000:
|
| 63 |
+
final_message_parts.append(f"{name}\nLarge changes detected")
|
| 64 |
+
continue
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
summary = generate_summary(diff)
|
| 68 |
+
# Format: File Name -> The generated message
|
| 69 |
+
final_message_parts.append(f"{name}\n{summary}")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"Error processing {name}: {e}")
|
| 72 |
+
final_message_parts.append(f"{name}\nUpdate file")
|
| 73 |
+
|
| 74 |
+
return jsonify({"commit_message": "\n\n".join(final_message_parts)})
|
| 75 |
+
|
| 76 |
+
if __name__ == '__main__':
|
| 77 |
app.run(host='0.0.0.0', port=7860)
|