Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,95 @@
|
|
1 |
-
#python -m streamlit run app.py
|
2 |
-
|
3 |
-
import streamlit as st
|
4 |
-
from transformers import pipeline
|
5 |
-
from FlagEmbedding import BGEM3FlagModel
|
6 |
-
from FlagEmbedding import FlagReranker
|
7 |
-
from inference_script import answer_question #import function from another file
|
8 |
-
from corpus import corpusvalue
|
9 |
-
import numpy as np
|
10 |
-
import getpass
|
11 |
-
import os
|
12 |
-
from langchain.prompts.prompt import PromptTemplate
|
13 |
-
from langchain.chains import ConversationChain
|
14 |
-
from langchain.chains import LLMChain
|
15 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
16 |
-
import pickle
|
17 |
-
import sqlite3
|
18 |
-
|
19 |
-
@st.cache_resource
|
20 |
-
def load_model():
|
21 |
-
return BGEM3FlagModel('BAAI/bge-m3',use_fp16=True)
|
22 |
-
|
23 |
-
@st.cache_resource
|
24 |
-
def load_rerank_model():
|
25 |
-
return FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=True)
|
26 |
-
|
27 |
-
@st.cache_resource
|
28 |
-
def initLLM():
|
29 |
-
os.environ["GOOGLE_API_KEY"] = "AIzaSyAuKPswmbdM8jCpSt0luez7tjLND-uyY7M"
|
30 |
-
llm = ChatGoogleGenerativeAI(model="gemini-pro")
|
31 |
-
template = """
|
32 |
-
คุณเป็นผู้เชี่ยวชาญด้านกฎหมายจราจร มีหน้าที่ในการนำข้อความทางกฎหมายเเละข้อปฎิบัติเกี่ยวกับการละเมิดกฎจราจรเเละข้อปฎิบัติต่างๆมาตอบคำถามว่าคำถามที่ถามมานั้นว่าผิดหรือไม่หรือจะต้องปฎิบัติตัวอย่างไร เเละอธิบายเพิ่มเติม ให้รายละเอียดและคำอธิบายเพิ่มเติมเพื่อให้ผู้ที่ไม่ใช่ผู้เชี่ยวชาญด้านกฎหมายเข้าใจได้ง่ายขึ้น
|
33 |
-
|
34 |
-
นี้คือคำถาม : {question}
|
35 |
-
ข้อความทางกฎหมาย: {section}
|
36 |
-
|
37 |
-
คำอธิบายโดยละเอียด:
|
38 |
-
"""
|
39 |
-
prompt = PromptTemplate(
|
40 |
-
input_variables=["section","question"],
|
41 |
-
template=template
|
42 |
-
)
|
43 |
-
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
44 |
-
return llm_chain
|
45 |
-
|
46 |
-
@st.cache_data
|
47 |
-
def embeded_corpus():
|
48 |
-
file_path_embeded_corpus = "save/BGM3savesimilar_Corpus" #
|
49 |
-
with open(file_path_embeded_corpus,'rb') as file :
|
50 |
-
BGM3similar_Corpus = pickle.load(file)
|
51 |
-
return BGM3similar_Corpus
|
52 |
-
|
53 |
-
def insert_feedback(question, answer,like,dislike, feedback_text):
|
54 |
-
conn = sqlite3.connect('feedback.db')
|
55 |
-
cursor = conn.cursor()
|
56 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS qa_feedback
|
57 |
-
(id INTEGER PRIMARY KEY, question TEXT, answer TEXT,
|
58 |
-
like INTEGER, dislike INTEGER, feedback_text TEXT)''')
|
59 |
-
data_to_insert = (question, answer, like, dislike, feedback_text)
|
60 |
-
sql_query = 'INSERT INTO qa_feedback (question, answer, like, dislike, feedback_text) VALUES (?, ?, ?, ?, ?)'
|
61 |
-
cursor.execute(sql_query, data_to_insert)
|
62 |
-
conn.commit()
|
63 |
-
conn.close()
|
64 |
-
|
65 |
-
model = load_model()
|
66 |
-
rerank_model = load_rerank_model()
|
67 |
-
llm_chain = initLLM()
|
68 |
-
BGM3similar_Corpus = embeded_corpus()
|
69 |
-
corpus_list = corpusvalue()
|
70 |
-
|
71 |
-
st.title("Traffic Law Question-Answering")
|
72 |
-
|
73 |
-
question = st.text_area("Enter your question:")
|
74 |
-
|
75 |
-
if 'like_value' not in st.session_state:
|
76 |
-
st.session_state.like_value = 0
|
77 |
-
if 'dislike_value' not in st.session_state:
|
78 |
-
st.session_state.dislike_value = 0
|
79 |
-
|
80 |
-
if st.button("Get Answer"):
|
81 |
-
if question:
|
82 |
-
answer = answer_question(question=question,model=model,rerankmodel=rerank_model,corpus_embed= BGM3similar_Corpus, corpus_list=corpus_list,llm_chain=llm_chain)
|
83 |
-
st.text_area("Answer:", value=answer, height=500)
|
84 |
-
|
85 |
-
st.write("### Feedback")
|
86 |
-
feedback = st.text_area("Your feedback:")
|
87 |
-
like = st.button("👍 Like")
|
88 |
-
dislike = st.button("👎 Dislike")
|
89 |
-
|
90 |
-
like_value = 1 if like else 0
|
91 |
-
dislike_value = -1 if dislike else 0
|
92 |
-
feedback = feedback if feedback else "No Feed back"
|
93 |
-
|
94 |
-
if like or dislike or feedback:
|
95 |
insert_feedback(question, answer, like_value, dislike_value,feedback)
|
|
|
1 |
+
#python -m streamlit run app.py
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from transformers import pipeline
|
5 |
+
from FlagEmbedding import BGEM3FlagModel
|
6 |
+
from FlagEmbedding import FlagReranker
|
7 |
+
from inference_script import answer_question #import function from another file
|
8 |
+
from corpus import corpusvalue
|
9 |
+
import numpy as np
|
10 |
+
import getpass
|
11 |
+
import os
|
12 |
+
from langchain.prompts.prompt import PromptTemplate
|
13 |
+
from langchain.chains import ConversationChain
|
14 |
+
from langchain.chains import LLMChain
|
15 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
16 |
+
import pickle
|
17 |
+
import sqlite3
|
18 |
+
|
19 |
+
@st.cache_resource
|
20 |
+
def load_model():
|
21 |
+
return BGEM3FlagModel('BAAI/bge-m3',use_fp16=True)
|
22 |
+
|
23 |
+
@st.cache_resource
|
24 |
+
def load_rerank_model():
|
25 |
+
return FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=True)
|
26 |
+
|
27 |
+
@st.cache_resource
|
28 |
+
def initLLM():
|
29 |
+
os.environ["GOOGLE_API_KEY"] = "AIzaSyAuKPswmbdM8jCpSt0luez7tjLND-uyY7M"
|
30 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro")
|
31 |
+
template = """
|
32 |
+
คุณเป็นผู้เชี่ยวชาญด้านกฎหมายจราจร มีหน้าที่ในการนำข้อความทางกฎหมายเเละข้อปฎิบัติเกี่ยวกับการละเมิดกฎจราจรเเละข้อปฎิบัติต่างๆมาตอบคำถามว่าคำถามที่ถามมานั้นว่าผิดหรือไม่หรือจะต้องปฎิบัติตัวอย่างไร เเละอธิบายเพิ่มเติม ให้รายละเอียดและคำอธิบายเพิ่มเติมเพื่อให้ผู้ที่ไม่ใช่ผู้เชี่ยวชาญด้านกฎหมายเข้าใจได้ง่ายขึ้น
|
33 |
+
|
34 |
+
นี้คือคำถาม : {question}
|
35 |
+
ข้อความทางกฎหมาย: {section}
|
36 |
+
|
37 |
+
คำอธิบายโดยละเอียด:
|
38 |
+
"""
|
39 |
+
prompt = PromptTemplate(
|
40 |
+
input_variables=["section","question"],
|
41 |
+
template=template
|
42 |
+
)
|
43 |
+
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
44 |
+
return llm_chain
|
45 |
+
|
46 |
+
@st.cache_data
|
47 |
+
def embeded_corpus():
|
48 |
+
file_path_embeded_corpus = "/save/BGM3savesimilar_Corpus" #
|
49 |
+
with open(file_path_embeded_corpus,'rb') as file :
|
50 |
+
BGM3similar_Corpus = pickle.load(file)
|
51 |
+
return BGM3similar_Corpus
|
52 |
+
|
53 |
+
def insert_feedback(question, answer,like,dislike, feedback_text):
|
54 |
+
conn = sqlite3.connect('feedback.db')
|
55 |
+
cursor = conn.cursor()
|
56 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS qa_feedback
|
57 |
+
(id INTEGER PRIMARY KEY, question TEXT, answer TEXT,
|
58 |
+
like INTEGER, dislike INTEGER, feedback_text TEXT)''')
|
59 |
+
data_to_insert = (question, answer, like, dislike, feedback_text)
|
60 |
+
sql_query = 'INSERT INTO qa_feedback (question, answer, like, dislike, feedback_text) VALUES (?, ?, ?, ?, ?)'
|
61 |
+
cursor.execute(sql_query, data_to_insert)
|
62 |
+
conn.commit()
|
63 |
+
conn.close()
|
64 |
+
|
65 |
+
model = load_model()
|
66 |
+
rerank_model = load_rerank_model()
|
67 |
+
llm_chain = initLLM()
|
68 |
+
BGM3similar_Corpus = embeded_corpus()
|
69 |
+
corpus_list = corpusvalue()
|
70 |
+
|
71 |
+
st.title("Traffic Law Question-Answering")
|
72 |
+
|
73 |
+
question = st.text_area("Enter your question:")
|
74 |
+
|
75 |
+
if 'like_value' not in st.session_state:
|
76 |
+
st.session_state.like_value = 0
|
77 |
+
if 'dislike_value' not in st.session_state:
|
78 |
+
st.session_state.dislike_value = 0
|
79 |
+
|
80 |
+
if st.button("Get Answer"):
|
81 |
+
if question:
|
82 |
+
answer = answer_question(question=question,model=model,rerankmodel=rerank_model,corpus_embed= BGM3similar_Corpus, corpus_list=corpus_list,llm_chain=llm_chain)
|
83 |
+
st.text_area("Answer:", value=answer, height=500)
|
84 |
+
|
85 |
+
st.write("### Feedback")
|
86 |
+
feedback = st.text_area("Your feedback:")
|
87 |
+
like = st.button("👍 Like")
|
88 |
+
dislike = st.button("👎 Dislike")
|
89 |
+
|
90 |
+
like_value = 1 if like else 0
|
91 |
+
dislike_value = -1 if dislike else 0
|
92 |
+
feedback = feedback if feedback else "No Feed back"
|
93 |
+
|
94 |
+
if like or dislike or feedback:
|
95 |
insert_feedback(question, answer, like_value, dislike_value,feedback)
|