orgoflu commited on
Commit
af6f11c
ยท
verified ยท
1 Parent(s): 6cc7695

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -12,18 +12,22 @@ import re
12
  import torch
13
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
14
 
15
- # ===== LLM ๋กœ๋“œ (๊ฒฝ๋Ÿ‰ ๋ชจ๋ธ๋กœ ์†๋„ ๊ฐœ์„ ) =====
16
- # Qwen2.5-1.5B-Instruct โ†’ ํ’ˆ์งˆ์€ ์ข‹์ง€๋งŒ ๋А๋ฆผ
17
- # ์†๋„ ๊ฐœ์„  ์œ„ํ•ด phi-3-mini-4k-instruct ๊ฐ™์€ ๊ฒฝ๋Ÿ‰ ๋ชจ๋ธ๋„ ๊ฐ€๋Šฅ
18
- MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
19
-
20
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
21
- model = AutoModelForCausalLM.from_pretrained(
22
- MODEL_NAME,
23
- torch_dtype=torch.float32
24
- ).to("cpu")
25
-
26
- llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1)
 
 
 
 
27
 
28
  # ===== ์œ ํ‹ธ =====
29
  def clean_text(text: str) -> str:
@@ -71,21 +75,23 @@ def summarize_text(text):
71
  summary_list.sort(key=lambda s: text.find(s))
72
  return summary_list
73
 
74
- # ===== LLM ์ž๋™์žฌ์ž‘์„ฑ (๋ถˆํ•„์š” ์•ˆ๋‚ด๋ฌธ ์ œ๊ฑฐ) =====
75
- def rewrite_with_llm(sentences):
 
 
 
76
  joined_text = "\n".join(sentences)
77
- prompt = f"""๋‹ค์Œ ๋ฌธ์žฅ์„ ์˜๋ฏธ๋Š” ์œ ์ง€ํ•˜๋˜, ๋” ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ์žฌ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
78
- ์ถœ๋ ฅ์€ ์žฌ์ž‘์„ฑ๋œ ๋ฌธ์žฅ๋งŒ ํฌํ•จํ•˜๊ณ , ๋‹ค๋ฅธ ์„ค๋ช…์ด๋‚˜ ๋ถ€์—ฐ ๋ฌธ์žฅ์€ ์ ˆ๋Œ€ ์“ฐ์ง€ ๋งˆ์„ธ์š”.
79
 
80
  ๋ฌธ์žฅ:
81
  {joined_text}
82
  """
83
- result = llm_pipeline(prompt, max_new_tokens=300, do_sample=False)
84
- # ํ”„๋กฌํ”„ํŠธ ๋ถ€๋ถ„ ์ œ๊ฑฐ ํ›„ ์–‘๋ ๊ณต๋ฐฑ ์ œ๊ฑฐ
85
  return result[0]["generated_text"].replace(prompt, "").strip()
86
 
87
  # ===== ์ „์ฒด ํŒŒ์ดํ”„๋ผ์ธ =====
88
- def extract_summarize_paraphrase(url):
89
  headers = {"User-Agent": "Mozilla/5.0"}
90
  try:
91
  r = requests.get(url, headers=headers, timeout=10)
@@ -107,7 +113,7 @@ def extract_summarize_paraphrase(url):
107
  if not summary_sentences:
108
  summary_sentences = ["์š”์•ฝ ์—†์Œ"]
109
 
110
- paraphrased_text = rewrite_with_llm(summary_sentences)
111
 
112
  return (
113
  markdown_text or "๋ณธ๋ฌธ ์—†์Œ",
@@ -121,14 +127,17 @@ def extract_summarize_paraphrase(url):
121
  # ===== Gradio UI =====
122
  iface = gr.Interface(
123
  fn=extract_summarize_paraphrase,
124
- inputs=gr.Textbox(label="URL ์ž…๋ ฅ", placeholder="https://example.com"),
 
 
 
125
  outputs=[
126
  gr.Markdown(label="์ถ”์ถœ๋œ ๋ณธ๋ฌธ"),
127
  gr.Textbox(label="์ž๋™ ์š”์•ฝ", lines=5),
128
  gr.Textbox(label="์ž๋™ ์žฌ์ž‘์„ฑ (LLM)", lines=5)
129
  ],
130
- title="ํ•œ๊ตญ์–ด ๋ณธ๋ฌธ ์ถ”์ถœ + ์ž๋™ ์š”์•ฝ + HF LLM ์žฌ์ž‘์„ฑ",
131
- description="๋ณธ๋ฌธ์€ TextRank๋กœ ์š”์•ฝํ•˜๊ณ , ์žฌ์ž‘์„ฑ์€ Hugging Face Hub LLM์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ถœ๋ ฅ์€ ์žฌ์ž‘์„ฑ๋œ ๋ฌธ์žฅ๋งŒ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค."
132
  )
133
 
134
  if __name__ == "__main__":
 
12
  import torch
13
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
14
 
15
+ # ===== ์ง€์› ๋ชจ๋ธ ๋ชฉ๋ก =====
16
+ MODEL_OPTIONS = {
17
+ "Qwen2.5-1.5B-Instruct (ํ’ˆ์งˆโ†‘, ๋А๋ฆผ)": "Qwen/Qwen2.5-1.5B-Instruct",
18
+ "Qwen2.5-0.5B-Instruct (๋น ๋ฆ„, ๊ฒฝ๋Ÿ‰)": "Qwen/Qwen2.5-0.5B-Instruct",
19
+ "Phi-3-Mini-4K-Instruct (๋น ๋ฆ„, ๊ฒฝ๋Ÿ‰)": "microsoft/Phi-3-mini-4k-instruct",
20
+ "Mistral-7B-Instruct-v0.3": "mistralai/Mistral-7B-Instruct-v0.3"
21
+ }
22
+
23
+ # ===== ๋ชจ๋ธ ๋กœ๋“œ ํ•จ์ˆ˜ =====
24
+ def load_model(model_name):
25
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
26
+ model = AutoModelForCausalLM.from_pretrained(
27
+ model_name,
28
+ torch_dtype=torch.float32
29
+ ).to("cpu")
30
+ return pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1)
31
 
32
  # ===== ์œ ํ‹ธ =====
33
  def clean_text(text: str) -> str:
 
75
  summary_list.sort(key=lambda s: text.find(s))
76
  return summary_list
77
 
78
+ # ===== LLM ์ž๋™์žฌ์ž‘์„ฑ =====
79
+ def rewrite_with_llm(sentences, model_choice):
80
+ model_name = MODEL_OPTIONS[model_choice]
81
+ llm_pipeline = load_model(model_name)
82
+
83
  joined_text = "\n".join(sentences)
84
+ prompt = f"""๋‹ค์Œ ๋ฌธ์žฅ์„ ์˜๋ฏธ๋Š” ์œ ์ง€ํ•˜๋˜, ์›๋ฌธ์— ์—†๋Š” ๋‚ด์šฉ์€ ์ ˆ๋Œ€ ์ถ”๊ฐ€ํ•˜์ง€ ๋ง๊ณ ,
85
+ ๋ฌธ์žฅ๋งŒ ๋” ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”. ๋‹ค๋ฅธ ์„ค๋ช…์ด๋‚˜ ๋ถ€์—ฐ ๋ฌธ์žฅ์€ ์“ฐ์ง€ ๋งˆ์„ธ์š”.
86
 
87
  ๋ฌธ์žฅ:
88
  {joined_text}
89
  """
90
+ result = llm_pipeline(prompt, max_new_tokens=180, do_sample=False, temperature=0)
 
91
  return result[0]["generated_text"].replace(prompt, "").strip()
92
 
93
  # ===== ์ „์ฒด ํŒŒ์ดํ”„๋ผ์ธ =====
94
+ def extract_summarize_paraphrase(url, model_choice):
95
  headers = {"User-Agent": "Mozilla/5.0"}
96
  try:
97
  r = requests.get(url, headers=headers, timeout=10)
 
113
  if not summary_sentences:
114
  summary_sentences = ["์š”์•ฝ ์—†์Œ"]
115
 
116
+ paraphrased_text = rewrite_with_llm(summary_sentences, model_choice)
117
 
118
  return (
119
  markdown_text or "๋ณธ๋ฌธ ์—†์Œ",
 
127
  # ===== Gradio UI =====
128
  iface = gr.Interface(
129
  fn=extract_summarize_paraphrase,
130
+ inputs=[
131
+ gr.Textbox(label="URL ์ž…๋ ฅ", placeholder="https://example.com"),
132
+ gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="Qwen2.5-0.5B-Instruct (๋น ๋ฆ„, ๊ฒฝ๋Ÿ‰)", label="์žฌ์ž‘์„ฑ ๋ชจ๋ธ ์„ ํƒ")
133
+ ],
134
  outputs=[
135
  gr.Markdown(label="์ถ”์ถœ๋œ ๋ณธ๋ฌธ"),
136
  gr.Textbox(label="์ž๋™ ์š”์•ฝ", lines=5),
137
  gr.Textbox(label="์ž๋™ ์žฌ์ž‘์„ฑ (LLM)", lines=5)
138
  ],
139
+ title="ํ•œ๊ตญ์–ด ๋ณธ๋ฌธ ์ถ”์ถœ + ์ž๋™ ์š”์•ฝ + LLM ์žฌ์ž‘์„ฑ (๋ชจ๋ธ ์„ ํƒ ๊ฐ€๋Šฅ)",
140
+ description="๋ณธ๋ฌธ์€ TextRank๋กœ ์š”์•ฝํ•˜๊ณ , ์žฌ์ž‘์„ฑ์€ ์„ ํƒํ•œ Hugging Face Hub LLM์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค."
141
  )
142
 
143
  if __name__ == "__main__":