Kota Takahashi commited on
Commit
3c0c826
1 Parent(s): a46a105

コードを追加

Browse files
Files changed (4) hide show
  1. app.py +43 -0
  2. generator.py +16 -0
  3. requirements.txt +6 -0
  4. scraper.py +10 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import unicodedata
3
+
4
+ import streamlit as st
5
+
6
+ from generator import generate_response
7
+ from scraper import get_stock_info
8
+
9
+ st.title("株価お知らせBot")
10
+
11
+ stock_code = st.text_input("株コード", placeholder="株コード", max_chars=4, help='4桁の数字')
12
+
13
+ if "content" not in st.session_state:
14
+ st.write("はじめに、株コードを指定してください。")
15
+
16
+ if st.button("株決定"):
17
+ content = get_stock_info(stock_code)
18
+ content = unicodedata.normalize('NFKD', content)
19
+ st.session_state.content = re.sub('[\r\t\n]+', ' ', content)
20
+
21
+ # メッセージがない時
22
+ if "messages" not in st.session_state.keys():
23
+ st.session_state.messages = [{"role": "assistant", "content": "何か御用ですか?"}]
24
+
25
+ # チャット内容の表示
26
+ for message in st.session_state.messages:
27
+ with st.chat_message(message["role"]):
28
+ st.write(message["content"])
29
+
30
+ # ユーザーの質問
31
+ if prompt := st.chat_input():
32
+ st.session_state.messages.append({"role": "user", "content": prompt})
33
+ with st.chat_message("user"):
34
+ st.write(prompt)
35
+
36
+ # AIによる回答
37
+ if st.session_state.messages[-1]["role"] != "assistant":
38
+ with st.chat_message("assistant"):
39
+ with st.spinner("考え中..."):
40
+ response = generate_response(st, prompt)
41
+ st.write(response)
42
+ message = {"role": "assistant", "content": response}
43
+ st.session_state.messages.append(message)
generator.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer
2
+ from transformers import AutoModelForQuestionAnswering
3
+ from transformers import QuestionAnsweringPipeline
4
+
5
+ model_name = 'KoichiYasuoka/bert-base-japanese-wikipedia-ud-head'
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForQuestionAnswering.from_pretrained(model_name)
9
+ qap = QuestionAnsweringPipeline(tokenizer=tokenizer, model=model)
10
+
11
+ def generate_response(st, prompt):
12
+ # Transformersで回答を作成
13
+ answer = qap(context=st.session_state.content[:100], question=prompt)
14
+
15
+ return answer["answer"]
16
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ sentencepiece
3
+ transformers
4
+ bs4
5
+ Cython
6
+ torch
scraper.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from bs4 import BeautifulSoup
2
+ import requests
3
+
4
+ def get_stock_info(stock_code):
5
+ url = f"https://www.nikkei.com/nkd/company/?scode={stock_code}"
6
+ r = requests.get(url)
7
+ soup = BeautifulSoup(r.content, 'html.parser')
8
+ _text = soup.find('div', attrs={'class': 'm-stockInfo_top_left'})
9
+ content = _text.text
10
+ return content