Hijiki-HF commited on
Commit
b5ea8b6
·
1 Parent(s): ea8be74

feat: app onnx

Browse files
Files changed (1) hide show
  1. src/app.py +42 -17
src/app.py CHANGED
@@ -1,41 +1,66 @@
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  # ページ設定
4
  st.set_page_config(
5
- page_title="小説感想生成アプリ(デモ)",
6
  page_icon="📚",
7
  layout="centered",
8
  )
9
 
10
  # アプリのタイトル
11
- st.title("小説感想生成アプリ(デモ版)")
12
- st.subheader("あなたの入力をそのまま返します")
13
 
14
  # 入力フォーム
15
  with st.form("input_form"):
16
- novel_title = st.text_input("小説のタイトル", placeholder="例:人間失格")
17
- summary = st.text_area("あらすじや感想メモ", height=200, placeholder="例:主人公の葉蔵は自分を「人間失格」だと考えている...")
18
  submit_button = st.form_submit_button("生成")
19
 
20
  # 送信ボタンが押されたら結果を表示
21
  if submit_button:
22
- st.markdown("## 入力内容")
23
- st.write(f"**タイトル:** {novel_title}")
24
- st.write("**あらすじや感想メモ:**")
25
- st.write(summary)
 
 
26
 
27
- st.markdown("---")
28
 
29
  st.markdown("## 生成された感想記事(デモ)")
30
  st.info(f"""
31
- {novel_title}】についての感想
32
-
33
- {summary}
34
-
35
- ※このデモ版では入力内容をそのまま返しています。
36
- 実際のアプリではここにLLMによって生成された内容が表示されます。
37
  """)
38
 
39
  # フッター
40
  st.markdown("---")
41
- st.caption("Powered by Streamlit & Hugging Face")
 
1
+ from optimum.onnxruntime import ORTModelForSeq2SeqLM
2
  import streamlit as st
3
+ import torch
4
+ from transformers import AutoTokenizer, pipeline
5
+
6
+ torch.classes.__path__ = []
7
+
8
+ def summarize_article(input_text):
9
+ MODEL_PATH = "model/mt5_onnx"
10
+
11
+ # ONNXモデルの読み込み
12
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, local_files_only=True)
13
+ model = ORTModelForSeq2SeqLM.from_pretrained(MODEL_PATH, local_files_only=True)
14
+
15
+ summarizer = pipeline(
16
+ "summarization",
17
+ model=model,
18
+ tokenizer=tokenizer
19
+ )
20
+ return summarizer(
21
+ input_text,
22
+ max_length=600,
23
+ min_length=200,
24
+ do_sample=True,
25
+ temperature=0.5,
26
+ num_beams=4,
27
+ early_stopping=True
28
+ )
29
+
30
 
31
  # ページ設定
32
  st.set_page_config(
33
+ page_title="記事要約(デモ)",
34
  page_icon="📚",
35
  layout="centered",
36
  )
37
 
38
  # アプリのタイトル
39
+ st.title("記事要約(デモ)")
40
+ st.subheader("入力を元に要約を生成します")
41
 
42
  # 入力フォーム
43
  with st.form("input_form"):
44
+ # novel_title = st.text_input("小説のタイトル", placeholder="例:人間失格")
45
+ input_text = st.text_area("記事内容", height=200, placeholder="例:主人公の葉蔵は自分を「人間失格」だと考えている...")
46
  submit_button = st.form_submit_button("生成")
47
 
48
  # 送信ボタンが押されたら結果を表示
49
  if submit_button:
50
+ # st.markdown("## 入力内容")
51
+ # # st.write(f"**タイトル:** {novel_title}")
52
+ # st.write("**あらすじや感想メモ:**")
53
+ # st.write(summary)
54
+
55
+ # st.markdown("---")
56
 
57
+ summary = summarize_article(input_text)
58
 
59
  st.markdown("## 生成された感想記事(デモ)")
60
  st.info(f"""
61
+ {summary["summary_text"][0]}
 
 
 
 
 
62
  """)
63
 
64
  # フッター
65
  st.markdown("---")
66
+ st.caption("Powered by Streamlit & Hugging Face")