Spaces:
Sleeping
Sleeping
File size: 2,649 Bytes
8bf4c24 83194b8 b2794e5 83194b8 976a15a 83194b8 8bf4c24 19b9973 8bf4c24 d0150b0 8bf4c24 b2794e5 232a998 d0150b0 8bf4c24 19b9973 b2794e5 8bf4c24 83194b8 b2794e5 19b9973 8bf4c24 83194b8 b2794e5 83194b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import streamlit as st
import torch
from transformers import pipeline
st.set_page_config(page_title="ViBidLawQA - Hệ thống hỏi đáp trực tuyến luật Việt Nam", page_icon="./app/static/ai.png", layout="centered", initial_sidebar_state="expanded")
with open("./static/styles.css") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
st.markdown(f"""
<div class=logo_area>
<img src="./app/static/ai.png"/>
</div>
""", unsafe_allow_html=True)
st.markdown("<h2 style='text-align: center;'> ViBidLawQA</h2>", unsafe_allow_html=True)
context = st.sidebar.text_area(label='Nội dung văn bản pháp luật Việt Nam:', placeholder='Vui lòng nhập nội dung văn bản pháp luật Việt Nam tại đây...', height=925)
device = 0 if torch.cuda.is_available() else -1
if 'model' not in st.session_state:
print('Some errors occurred!')
st.session_state.model = pipeline("question-answering", model='./model/', device=device)
def get_answer(context, question):
return st.session_state.model(context=context, question=question, max_answer_len=512)
if 'messages' not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
if message['role'] == 'assistant':
avatar_class = "assistant-avatar"
message_class = "assistant-message"
avatar = './app/static/ai.png'
else:
avatar_class = "user-avatar"
message_class = "user-message"
avatar = './app/static/human.png'
st.markdown(f"""
<div class="{message_class}">
<img src="{avatar}" class="{avatar_class}" />
<div class="stMarkdown">{message['content']}</div>
</div>
""", unsafe_allow_html=True)
if prompt := st.chat_input(placeholder='Tôi có thể giúp được gì cho bạn?'):
st.markdown(f"""
<div class="user-message">
<img src="./app/static/human.png" class="user-avatar" />
<div class="stMarkdown">{prompt}</div>
</div>
""", unsafe_allow_html=True)
st.session_state.messages.append({'role': 'user', 'content': prompt})
respond = get_answer(context=context, question=prompt)['answer']
respond = respond.strip()
respond = respond.rstrip('.,!?;:')
respond = respond.capitalize()
if not respond.endswith('.'):
respond += '.'
st.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.png" class="assistant-avatar" />
<div class="stMarkdown">{respond}</div>
</div>
""", unsafe_allow_html=True)
st.session_state.messages.append({'role': 'assistant', 'content': respond}) |