NiviruIns commited on
Commit
fddb21f
·
verified ·
1 Parent(s): 3474fd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -62
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
- # Hugging Face stores files in the current directory
9
- MODEL_PATH = "./my_codet5_model"
10
-
11
- # Check if model exists
12
- if not os.path.exists(MODEL_PATH):
13
- print(f" Error: Model found at {MODEL_PATH}")
14
- else:
15
- print(f"✅ Model found at {MODEL_PATH}")
16
-
17
- device = "cpu" # Free tier is CPU only (but fast enough for this)
18
-
19
- print("Loading model...")
20
- tokenizer = RobertaTokenizer.from_pretrained(MODEL_PATH)
21
- model = T5ForConditionalGeneration.from_pretrained(MODEL_PATH).to(device)
22
- print("Model loaded!")
23
-
24
- def generate_summary(diff_text):
25
- if not diff_text or len(diff_text.strip()) < 5:
26
- return "Minor changes"
27
-
28
- input_text = "Summarize: " + diff_text
29
- input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(device)
30
-
31
- outputs = model.generate(
32
- input_ids,
33
- max_length=50,
34
- num_beams=5,
35
- early_stopping=True
36
- )
37
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
38
-
39
- @app.route('/generate', methods=['POST'])
40
- def generate_commit():
41
- data = request.json
42
- files = data.get('files', [])
43
-
44
- if not files:
45
- return jsonify({"commit_message": ""})
46
-
47
- final_message_parts = []
48
-
49
- for file_obj in files:
50
- name = file_obj.get('name', 'Unknown File')
51
- diff = file_obj.get('diff', '')
52
- try:
53
- summary = generate_summary(diff)
54
- final_message_parts.append(f"{name}\n{summary}")
55
- except Exception as e:
56
- print(f"Error processing {name}: {e}")
57
- final_message_parts.append(f"{name}\nUpdate file")
58
-
59
- return jsonify({"commit_message": "\n\n".join(final_message_parts)})
60
-
61
- # Hugging Face expects running on port 7860
62
- if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)