pkheria commited on
Commit
1dc7db9
·
1 Parent(s): 1d719f3

added logs

Browse files
Files changed (2) hide show
  1. .DS_Store +0 -0
  2. app.py +48 -7
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
app.py CHANGED
@@ -10,6 +10,9 @@ CommitLens — Gradio UI
10
 
11
  from __future__ import annotations
12
 
 
 
 
13
  import gradio as gr
14
  import spaces
15
  import torch
@@ -17,6 +20,13 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
17
 
18
  from commitlens import run_pipeline
19
 
 
 
 
 
 
 
 
20
  # ---------------------------------------------------------------------------
21
  # Model config
22
  # ---------------------------------------------------------------------------
@@ -50,31 +60,40 @@ _tokenizer = None
50
  def _get_llm():
51
  global _model, _tokenizer
52
  if _model is None:
 
53
  # 8-bit quantization is required to bypass the 16GB ZeroGPU CPU RAM limit
54
  quantization_config = BitsAndBytesConfig(
55
  load_in_8bit=True,
56
  )
57
-
 
58
  _tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO_ID)
59
-
 
60
  # flash_attention_2 removed. PyTorch will automatically use native SDPA.
 
61
  _model = AutoModelForCausalLM.from_pretrained(
62
  MODEL_REPO_ID,
63
  quantization_config=quantization_config,
64
  device_map="auto",
65
- torch_dtype=torch.bfloat16 # ZeroGPU RTX 6000 natively supports bfloat16
66
  )
 
67
  return _model, _tokenizer
68
 
69
 
70
  def _extract_filename(prompt: str) -> str:
71
  for line in prompt.splitlines():
72
  if line.startswith("Filename :"):
73
- return line.split(":", 1)[1].strip()
 
 
 
74
  return "unknown"
75
 
76
 
77
  def _generate_response(system_prompt: str, user_prompt: str, max_tokens: int) -> str:
 
78
  model, tokenizer = _get_llm()
79
 
80
  messages = [
@@ -83,12 +102,16 @@ def _generate_response(system_prompt: str, user_prompt: str, max_tokens: int) ->
83
  ]
84
 
85
  # Format the prompt using the model's chat template
 
86
  formatted_prompt = tokenizer.apply_chat_template(
87
  messages, tokenize=False, add_generation_prompt=True
88
  )
89
 
 
90
  inputs = tokenizer(formatted_prompt, return_tensors="pt").to("cuda")
 
91
 
 
92
  outputs = model.generate(
93
  **inputs,
94
  max_new_tokens=max_tokens,
@@ -96,52 +119,69 @@ def _generate_response(system_prompt: str, user_prompt: str, max_tokens: int) ->
96
  do_sample=False,
97
  pad_token_id=tokenizer.eos_token_id
98
  )
 
99
 
100
  # Decode and return just the generated response
101
  response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
 
102
  return response.strip()
103
 
104
 
105
  def _summarize(prompt: str) -> str:
106
- return _generate_response(SUMMARY_SYSTEM_PROMPT, prompt, max_tokens=1024)
 
 
 
107
 
108
 
109
  def _final_md(combined: str) -> str:
110
- return _generate_response(FINAL_SYSTEM_PROMPT, combined, max_tokens=2048)
 
 
 
111
 
112
 
113
  # ---------------------------------------------------------------------------
114
  # Pipeline
115
  # ---------------------------------------------------------------------------
116
 
117
- # Re-added the @spaces.GPU decorator with the 300-second timeout
118
  @spaces.GPU(duration=300)
119
  def process_repo(repo_url: str, token: str, progress: gr.Progress = gr.Progress()):
120
  try:
 
121
  progress(0, desc="Running CommitLens pipeline...")
122
  prompts = run_pipeline(repo_url, token.strip() or None)
 
123
 
124
  if not prompts:
 
125
  raise ValueError("No source-code files changed in the latest commit.")
126
 
127
  per_file_md_parts = []
128
  for i, prompt in enumerate(prompts):
129
  fname = _extract_filename(prompt)
 
130
  progress(
131
  (i + 1) / (len(prompts) + 1),
132
  desc=f"Summarizing [{i+1}/{len(prompts)}] {fname}...",
133
  )
134
  summary = _summarize(prompt)
135
  per_file_md_parts.append(f"## `{fname}`\n\n{summary}")
 
136
 
137
  combined = "\n\n---\n\n".join(per_file_md_parts)
 
138
 
139
  progress(0.95, desc="Generating final markdown report...")
140
  final_md = _final_md(combined)
141
 
 
142
  return combined, final_md
143
 
 
 
144
  except Exception as e:
 
145
  raise gr.Error(str(e))
146
 
147
 
@@ -178,4 +218,5 @@ with gr.Blocks(title="CommitLens", theme=gr.themes.Soft()) as demo:
178
  )
179
 
180
  if __name__ == "__main__":
 
181
  demo.launch()
 
10
 
11
  from __future__ import annotations
12
 
13
+ import logging
14
+ import sys
15
+
16
  import gradio as gr
17
  import spaces
18
  import torch
 
20
 
21
  from commitlens import run_pipeline
22
 
23
+ logging.basicConfig(
24
+ level=logging.INFO,
25
+ format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
26
+ stream=sys.stdout,
27
+ )
28
+ log = logging.getLogger("commitlens")
29
+
30
  # ---------------------------------------------------------------------------
31
  # Model config
32
  # ---------------------------------------------------------------------------
 
60
  def _get_llm():
61
  global _model, _tokenizer
62
  if _model is None:
63
+ log.info("Starting model load from %s ...", MODEL_REPO_ID)
64
  # 8-bit quantization is required to bypass the 16GB ZeroGPU CPU RAM limit
65
  quantization_config = BitsAndBytesConfig(
66
  load_in_8bit=True,
67
  )
68
+
69
+ log.info("Loading tokenizer ...")
70
  _tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO_ID)
71
+ log.info("Tokenizer loaded.")
72
+
73
  # flash_attention_2 removed. PyTorch will automatically use native SDPA.
74
+ log.info("Loading model with 8-bit quantization and device_map='auto' ...")
75
  _model = AutoModelForCausalLM.from_pretrained(
76
  MODEL_REPO_ID,
77
  quantization_config=quantization_config,
78
  device_map="auto",
79
+ torch_dtype=torch.bfloat16, # ZeroGPU RTX 6000 natively supports bfloat16
80
  )
81
+ log.info("Model loaded successfully.")
82
  return _model, _tokenizer
83
 
84
 
85
  def _extract_filename(prompt: str) -> str:
86
  for line in prompt.splitlines():
87
  if line.startswith("Filename :"):
88
+ name = line.split(":", 1)[1].strip()
89
+ log.debug("Extracted filename: %s", name)
90
+ return name
91
+ log.warning("Could not extract filename from prompt")
92
  return "unknown"
93
 
94
 
95
  def _generate_response(system_prompt: str, user_prompt: str, max_tokens: int) -> str:
96
+ log.info("Generating response (max_tokens=%d) ...", max_tokens)
97
  model, tokenizer = _get_llm()
98
 
99
  messages = [
 
102
  ]
103
 
104
  # Format the prompt using the model's chat template
105
+ log.debug("Applying chat template ...")
106
  formatted_prompt = tokenizer.apply_chat_template(
107
  messages, tokenize=False, add_generation_prompt=True
108
  )
109
 
110
+ log.debug("Tokenizing input ...")
111
  inputs = tokenizer(formatted_prompt, return_tensors="pt").to("cuda")
112
+ log.debug("Input shape: %s", inputs.input_ids.shape)
113
 
114
+ log.info("Running model.generate ...")
115
  outputs = model.generate(
116
  **inputs,
117
  max_new_tokens=max_tokens,
 
119
  do_sample=False,
120
  pad_token_id=tokenizer.eos_token_id
121
  )
122
+ log.info("Generation complete.")
123
 
124
  # Decode and return just the generated response
125
  response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
126
+ log.debug("Response length: %d characters", len(response))
127
  return response.strip()
128
 
129
 
130
  def _summarize(prompt: str) -> str:
131
+ log.info("Summarizing file ...")
132
+ result = _generate_response(SUMMARY_SYSTEM_PROMPT, prompt, max_tokens=1024)
133
+ log.info("File summarization done.")
134
+ return result
135
 
136
 
137
  def _final_md(combined: str) -> str:
138
+ log.info("Generating final markdown report ...")
139
+ result = _generate_response(FINAL_SYSTEM_PROMPT, combined, max_tokens=2048)
140
+ log.info("Final markdown report generated.")
141
+ return result
142
 
143
 
144
  # ---------------------------------------------------------------------------
145
  # Pipeline
146
  # ---------------------------------------------------------------------------
147
 
 
148
  @spaces.GPU(duration=300)
149
  def process_repo(repo_url: str, token: str, progress: gr.Progress = gr.Progress()):
150
  try:
151
+ log.info("Pipeline started for repo: %s", repo_url)
152
  progress(0, desc="Running CommitLens pipeline...")
153
  prompts = run_pipeline(repo_url, token.strip() or None)
154
+ log.info("CommitLens pipeline returned %d prompts.", len(prompts))
155
 
156
  if not prompts:
157
+ log.warning("No source-code files changed in the latest commit.")
158
  raise ValueError("No source-code files changed in the latest commit.")
159
 
160
  per_file_md_parts = []
161
  for i, prompt in enumerate(prompts):
162
  fname = _extract_filename(prompt)
163
+ log.info("Processing file %d/%d: %s", i + 1, len(prompts), fname)
164
  progress(
165
  (i + 1) / (len(prompts) + 1),
166
  desc=f"Summarizing [{i+1}/{len(prompts)}] {fname}...",
167
  )
168
  summary = _summarize(prompt)
169
  per_file_md_parts.append(f"## `{fname}`\n\n{summary}")
170
+ log.info("Finished file %d/%d: %s", i + 1, len(prompts), fname)
171
 
172
  combined = "\n\n---\n\n".join(per_file_md_parts)
173
+ log.info("All per-file summaries combined (%d characters).", len(combined))
174
 
175
  progress(0.95, desc="Generating final markdown report...")
176
  final_md = _final_md(combined)
177
 
178
+ log.info("Pipeline finished successfully.")
179
  return combined, final_md
180
 
181
+ except gr.Error:
182
+ raise
183
  except Exception as e:
184
+ log.error("Pipeline failed: %s", e, exc_info=True)
185
  raise gr.Error(str(e))
186
 
187
 
 
218
  )
219
 
220
  if __name__ == "__main__":
221
+ log.info("Starting Gradio app ...")
222
  demo.launch()