yutakashino commited on
Commit
b154c96
·
1 Parent(s): dc672da

Add application file

Browse files
Files changed (2) hide show
  1. app.py +98 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import re
4
+ from bs4 import BeautifulSoup
5
+ from transformers import AutoTokenizer
6
+ from vllm import LLM, SamplingParams
7
+
8
+ # Load model and tokenizer
9
+ MODEL_NAME = "tokyotech-llm/Llama-3.1-Swallow-8B-Instruct-v0.3"
10
+ SYS_CONTENT = (
11
+ "あなたは誠実で優秀な日本人の新聞記者です。質問には正確に具体的に答えることができます。"
12
+ "入力される記事について,誰(who)が何(what)をいつ(when)どこ(where)でどうした(how)と書いてますか?"
13
+ "次のJSONの値を埋めて返して下さい.どこ(where)には地図で示せるくらい具体的な地名や施設名を入れてください。"
14
+ "もしも該当の情報が記事になければJSONの値を空にしてください。"
15
+ "{ \"who\": \"...\", \"what\": \"...\", \"when\": \"...\", \"where\": \"...\", \"how\": \"...\"} "
16
+ )
17
+
18
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
19
+ llm = LLM(
20
+ model=MODEL_NAME,
21
+ tensor_parallel_size=1,
22
+ )
23
+
24
+ def preprocess_text(text: str) -> str:
25
+ # HTMLタグの削除
26
+ soup = BeautifulSoup(text, 'html.parser')
27
+ text = soup.get_text()
28
+
29
+ # 独自タグの削除 (<...> </...>)
30
+ text = re.sub(r'<[^>]+>', '', text)
31
+
32
+ # 改行、タブ、余分な空白(半角・全角)の削除
33
+ text = re.sub(r'[\n\t]', '', text)
34
+ text = re.sub(r'[\s ]+', ' ', text) # 連続する空白を1つの半角スペースに置換
35
+ text = text.strip()
36
+
37
+ return text
38
+
39
+ def inference(content: str, max_tokens: int, temperature: float, top_p: float):
40
+ sampling_params = SamplingParams(
41
+ temperature=temperature,
42
+ top_p=top_p,
43
+ max_tokens=max_tokens,
44
+ stop="<|eot_id|>"
45
+ )
46
+
47
+ # 入力テキストの前処理
48
+ processed_content = preprocess_text(content)
49
+
50
+ message = [
51
+ {
52
+ "role": "system",
53
+ "content": SYS_CONTENT
54
+ },
55
+ {
56
+ "role": "user",
57
+ "content": processed_content,
58
+ },
59
+ ]
60
+
61
+ try:
62
+ prompt = tokenizer.apply_chat_template(
63
+ message, tokenize=False, add_generation_prompt=True
64
+ )
65
+ output = llm.generate(prompt, sampling_params)
66
+ result_text = output[0].outputs[0].text
67
+
68
+ # JSONを抽出
69
+ json_pattern = r'\{[^{}]*\}'
70
+ match = re.search(json_pattern, result_text)
71
+ if not match:
72
+ return "エラー: 生成されたテキストからJSONが見つかりませんでした。"
73
+
74
+ try:
75
+ json_data = json.loads(match.group())
76
+ return json.dumps(json_data, ensure_ascii=False, indent=2)
77
+ except json.JSONDecodeError as e:
78
+ return f"JSONパースエラー: {str(e)}"
79
+ except Exception as e:
80
+ return f"生成エラー: {str(e)}"
81
+
82
+ # Gradioインターフェースの作成
83
+ demo = gr.Interface(
84
+ fn=inference,
85
+ inputs=[
86
+ gr.Textbox(label="入力テキスト", lines=10),
87
+ gr.Number(label="最大トークン数", value=512),
88
+ gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.3, step=0.1),
89
+ gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1),
90
+ ],
91
+ outputs=gr.Textbox(label="解析結果", lines=10),
92
+ title="意味解析エンジン",
93
+ description="テキストを入力すると、5W(Who, What, When, Where, How)の形式で情報を抽出します.テキスト内に混入した改行や空白,独自タグ等を削除する整形処理を入れてますが,きちんとテストしていません.エラーが出る場合は事前に整形してからテキストを入れて下さい.",
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()
98
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio>=4.44.1
2
+ torch
3
+ transformers
4
+ vllm
5
+ beautifulsoup4