epinfomax commited on
Commit
7ef137c
·
verified ·
1 Parent(s): 6fabab2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -7
app.py CHANGED
@@ -3,25 +3,64 @@ import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  from peft import PeftModel
5
 
 
6
  base_id = "Qwen/Qwen2.5-7B-Instruct"
7
  adapter_id = "epinfomax/BizFlow-Summarizer-Ko"
8
 
9
- # CPU 환경(무료)이면 float32, GPU 환경이면 float16/bfloat16 사용
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  dtype = torch.float16 if device == "cuda" else torch.float32
12
 
 
 
 
13
  tokenizer = AutoTokenizer.from_pretrained(base_id)
14
  model = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype=dtype)
15
  model = PeftModel.from_pretrained(model, adapter_id)
16
  model.to(device)
 
17
 
18
  def summarize(text):
 
19
  messages =
20
- prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
21
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
22
 
23
- outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.3)
24
- return tokenizer.decode(outputs[inputs.input_ids.shape[1]:], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- iface = gr.Interface(fn=summarize, inputs="text", outputs="text", title="BizFlow 요약기")
27
- iface.launch()
 
 
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  from peft import PeftModel
5
 
6
+ # 1. 모델 ID 설정
7
  base_id = "Qwen/Qwen2.5-7B-Instruct"
8
  adapter_id = "epinfomax/BizFlow-Summarizer-Ko"
9
 
10
+ # 2. 하드웨어 자동 설정 (GPU 없으면 CPU로 돌아가도록 처리)
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  dtype = torch.float16 if device == "cuda" else torch.float32
13
 
14
+ print(f"🚀 모델을 로딩 중입니다... (Device: {device})")
15
+
16
+ # 3. 모델과 토크나이저 불러오기
17
  tokenizer = AutoTokenizer.from_pretrained(base_id)
18
  model = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype=dtype)
19
  model = PeftModel.from_pretrained(model, adapter_id)
20
  model.to(device)
21
+ model.eval()
22
 
23
  def summarize(text):
24
+ # 오류가 났던 부분 수정: messages 리스트를 명확히 정의
25
  messages =
 
 
26
 
27
+ # Qwen 채팅 템플릿 적용
28
+ input_text = tokenizer.apply_chat_template(
29
+ messages,
30
+ tokenize=False,
31
+ add_generation_prompt=True
32
+ )
33
+
34
+ # 입력 토큰화
35
+ inputs = tokenizer([input_text], return_tensors="pt").to(device)
36
+
37
+ # 추론 (요약 생성)
38
+ with torch.no_grad():
39
+ outputs = model.generate(
40
+ **inputs,
41
+ max_new_tokens=512, # 생성할 최대 길이
42
+ temperature=0.3, # 값이 낮을수록 사실적인 요약
43
+ repetition_penalty=1.1
44
+ )
45
+
46
+ # 결과 디코딩 (입력 프롬프트 제외하고 순수 요약문만 추출)
47
+ summary = tokenizer.decode(outputs[inputs.input_ids.shape[1]:], skip_special_tokens=True)
48
+ return summary
49
+
50
+ # 4. 웹 인터페이스 구성 (Gradio)
51
+ iface = gr.Interface(
52
+ fn=summarize,
53
+ inputs=gr.Textbox(
54
+ lines=15,
55
+ placeholder="여기에 요약할 뉴스 기사나 회의록을 붙여넣으세요...",
56
+ label="입력 문서"
57
+ ),
58
+ outputs=gr.Textbox(label="요약 결과"),
59
+ title="BizFlow 문서 요약 에이전트",
60
+ description="Qwen2.5-7B 모델을 파인튜닝하여 만든 한국어 전문 요약기입니다.",
61
+ examples=["삼성전자가 오늘 컨퍼런스콜을 통해 지난해 4분기 확정 실적을 발표했다. 연결 기준 매출은 67조 7800억 원으로 전년 동기 대비 3.8% 감소했으나, 영업이익은 2조 8200억 원으로..."]
62
+ )
63
 
64
+ # 실행
65
+ if __name__ == "__main__":
66
+ iface.launch()