BienKieu commited on
Commit
ed32b22
·
verified ·
1 Parent(s): 452197c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import torch
3
  import gradio as gr
 
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
5
 
6
  REPO_ID = os.environ.get("HF_MODEL_ID", "HuyTran1301/constrative_cont_so_phase2_SI")
@@ -11,27 +12,31 @@ torch.set_num_threads(int(os.environ.get("TORCH_NUM_THREADS", "1")))
11
  tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
12
  model = AutoModelForSeq2SeqLM.from_pretrained(REPO_ID)
13
 
14
- summarizer = pipeline(
15
- task="summarization",
16
- model=model,
17
- tokenizer=tokenizer,
18
- device=-1
19
- )
20
-
21
  def summarize_one(lang: str, desc: str, code: str):
22
  if not any([lang.strip(), desc.strip(), code.strip()]):
23
- return ""
24
  merged_text = f"{lang.strip()}: {desc.strip()} <code> {code.strip()}"
25
- summary = summarizer(
26
  merged_text,
 
27
  truncation=True,
28
- max_length=GEN_MAX_LENGTH,
29
- min_length=5,
30
- do_sample=False
31
- )[0]["summary_text"]
32
- return summary
33
-
34
- with gr.Blocks(title="Code Summarizer") as demo:
 
 
 
 
 
 
 
 
 
 
35
  gr.Markdown("# Code Summarization")
36
 
37
  with gr.Row():
@@ -39,16 +44,16 @@ with gr.Blocks(title="Code Summarizer") as demo:
39
  desc = gr.Textbox(label="Description", placeholder="What does the code do?")
40
  code = gr.Textbox(lines=8, label="Code", placeholder="Paste your code here...")
41
 
42
- btn = gr.Button("Generate Summary")
43
- out_summary = gr.Textbox(label="Generated Summary", lines=4)
44
 
45
  btn.click(
46
  summarize_one,
47
  inputs=[lang, desc, code],
48
- outputs=[out_summary]
49
  )
50
 
51
- gr.Markdown(f"**Model:** `{REPO_ID}` • **Input max length:** {MAX_LENGTH} • **Output max length:** {GEN_MAX_LENGTH}")
52
 
53
  if __name__ == "__main__":
54
- demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
 
1
  import os
2
  import torch
3
  import gradio as gr
4
+ import pandas as pd
5
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
6
 
7
  REPO_ID = os.environ.get("HF_MODEL_ID", "HuyTran1301/constrative_cont_so_phase2_SI")
 
12
  tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
13
  model = AutoModelForSeq2SeqLM.from_pretrained(REPO_ID)
14
 
 
 
 
 
 
 
 
15
  def summarize_one(lang: str, desc: str, code: str):
16
  if not any([lang.strip(), desc.strip(), code.strip()]):
17
+ return pd.DataFrame([["", ""]], columns=["#","Summary"])
18
  merged_text = f"{lang.strip()}: {desc.strip()} <code> {code.strip()}"
19
+ input_ids = tokenizer(
20
  merged_text,
21
+ return_tensors="pt",
22
  truncation=True,
23
+ max_length=MAX_LENGTH
24
+ ).input_ids
25
+
26
+ with torch.no_grad():
27
+ outputs = model.generate(
28
+ input_ids,
29
+ max_length=GEN_MAX_LENGTH,
30
+ num_beams=5,
31
+ num_return_sequences=5,
32
+ early_stopping=True,
33
+ )
34
+
35
+ summaries = [tokenizer.decode(o, skip_special_tokens=True).strip() for o in outputs]
36
+ df = pd.DataFrame(list(enumerate(summaries, start=1)), columns=["#", "Summary"])
37
+ return df
38
+
39
+ with gr.Blocks(title="Code Summarization") as demo:
40
  gr.Markdown("# Code Summarization")
41
 
42
  with gr.Row():
 
44
  desc = gr.Textbox(label="Description", placeholder="What does the code do?")
45
  code = gr.Textbox(lines=8, label="Code", placeholder="Paste your code here...")
46
 
47
+ btn = gr.Button("Generate Summaries (5 beams)")
48
+ out_table = gr.Dataframe(headers=["#", "Summary"], label="Generated Summaries", interactive=False)
49
 
50
  btn.click(
51
  summarize_one,
52
  inputs=[lang, desc, code],
53
+ outputs=[out_table]
54
  )
55
 
56
+ gr.Markdown(f"**Model:** `{REPO_ID}` • **Input max length:** {MAX_LENGTH} • **Output max length:** {GEN_MAX_LENGTH} • **num_beams:** 5")
57
 
58
  if __name__ == "__main__":
59
+ demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))