import streamlit as st
from transformers import pipeline
from TransformationQA_ZongqianLi.transformationqa import create_qa_database
from numpy import linspace
import jsonlines
import json

st.markdown("# 🎓 Auto-generating Question-Answering Datasets with Domain-Specific Knowledge for Language Models in Scientific Tasks", unsafe_allow_html=True)
    
##########
# Question Answering
##########
st.markdown('## 📖 Question Answering', unsafe_allow_html=True)
st.markdown('### Select a model: ', unsafe_allow_html=True)

# 定义模型配置选项
size_lst = ["-base (110 million parameters)", "-large (340 million parameters)"]
cased_lst = ["-cased (distinguish upper and lowercase letters)", "-uncased (not distinguish upper and lowercase letters)"]
fpretrain_lst = ["None", "-scsmall (further pretrained on Solar Cell Corpus Small)", "-scmedium (further pretrained on Solar Cell Corpus Medium)", "-sclarge (further pretrained on Solar Cell Corpus Large)"]
finetune_lst = ["-squad (finetuned on the SQuAD dataset)", "-squadscqa1 (finetuned on SQuAD and Solar Cell QA Dataset (first-turn QA pairs))", "-scqa1 (finetuned on Solar Cell QA Dataset (first-turn QA pairs))", "-scqa2 (finetuned on the Solar Cell QA Dataset (whole))"]

# 为每个选项创建下拉菜单
st.markdown(f'###### bert', unsafe_allow_html=True)
size = st.selectbox("Choose a model size:", size_lst)
cased = st.selectbox("Whether distinguish upper and lowercase letters:", cased_lst)
fpretrain = st.selectbox("Further pretrained on a solar cell corpus:", fpretrain_lst)
finetune = st.selectbox("Finetuned on a QA dataset:", finetune_lst)

# 根据选择构建模型名称
if fpretrain == "None":
    model = "".join(["CambridgeMolecularEngineering/bert", size.split(" (")[0], cased.split(" (")[0], finetune.split(" (")[0]])
else:
    model = "".join(["CambridgeMolecularEngineering/bert", size.split(" (")[0], cased.split(" (")[0], fpretrain.split(" (")[0], finetune.split(" (")[0]])

# 显示用户选择的模型
st.write(f"Your selected model:")
st.markdown(f'###### {model}', unsafe_allow_html=True)

# 加载问答模型
pipe = pipeline("question-answering", model=model)
st.markdown('### Answer the question: ', unsafe_allow_html=True)

# 使用Session State来存储默认的问题和上下文
if 'default_question' not in st.session_state:
    st.session_state['default_question'] = "What is the value of FF?"
if 'default_context' not in st.session_state:
    st.session_state['default_context'] = "The referential DSSC with Pt CE was also measured under the same conditions, which yields η of 6.66% (Voc= 0.78 V, Jsc= 13.0 mA cm−2, FF = 65.9%)."

col1, col2 = st.columns(2)

with col1:
    if st.button("Solar Cell QA Dataset"):
        st.session_state['default_question'] = "What is the value of FF?"
        st.session_state['default_context'] = "The referential DSSC with Pt CE was also measured under the same conditions, which yields η of 6.66% (Voc= 0.78 V, Jsc= 13.0 mA cm−2, FF = 65.9%)."

with col2:
    if st.button("SuAD Dataset QA"):
        st.session_state['default_question'] = "To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?"
        st.session_state['default_context'] = """Architecturally, the school has a Catholic character. Atop the Main Building's gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary."""

# 这些输入字段现在使用session state来填充默认值
question = st.text_input("Enter the question: ", value=st.session_state['default_question'])
context = st.text_area("Enter the context: ", value=st.session_state['default_context'], height=100)

# 添加一个按钮,用户点击后执行问答
if st.button('Extract the answer'):
    if context and question:
        out = pipe({
            'question': question,
            'context': context
        })
        answer = out["answer"]
        st.write(f"Question: {question}")
        st.write(f"Answer: {answer}")
    else:
        st.write("Please enter both a question and context.")


        
##########
# Property Extraction
##########
st.markdown('## 📤 Property Extraction', unsafe_allow_html=True)

default_property = "FF"
default_context = "The referential DSSC with Pt CE was also measured under the same conditions, which yields η of 6.66% (Voc= 0.78 V, Jsc= 13.0 mA cm−2, FF = 65.9%)."

# 获取用户输入的问题和上下文
property = st.text_input("Enter the name of the property: ", value=default_property)
context = st.text_area("Enter the paper: ", value=default_context, height=100)

# 添加一个按钮,用户点击后执行问答
if st.button('Extract the property'):
    question_1 = f"What is the value of {property}?"
    if context and question_1:
        out = pipe({
            'question': question_1,
            'context': context
        })
        value = out["answer"]
        st.write(f"First-turn question: {question_1}")
        st.write(f"First-turn answer: {value}")
        question_2 = f"What material has {property} of {value}?"
        out = pipe({
            'question': question_2,
            'context': context
        })
        material = out["answer"]
        st.write(f"Second-turn question: {question_2}")
        st.write(f"First-turn answer: {material}")
    else:
        st.write("Please enter both a question and context.")



##########
# Transformation Algorithm
##########
st.markdown('## 🖥️ QA Dataset Auto Generation', unsafe_allow_html=True)
st.markdown('### Algorithm Input', unsafe_allow_html=True)

st.markdown("###### Example of the ChemDataExtractor generated database: ", unsafe_allow_html=True)
file_path = "./CDE_properties.jsonl"
json_list = []
with open(file_path, 'r', encoding="utf-8") as file:
    for line in file:
        json_dict = json.loads(line.strip())
        json_list.append(json_dict)
json_string = json.dumps(json_list, indent=4)
st.text_area("", value=json_string, height=200)

st.markdown('###### Example of the paper collection: ', unsafe_allow_html=True)
with open("./reference_paper.json",'r+', encoding = "utf-8") as f:
    string = f.read()
    json_data = json.loads(string)
    json_string = json.dumps(json_data, indent=4)
    st.text_area("", value=json_string, height=200)

st.markdown('### Algorithm Output', unsafe_allow_html=True)

st.markdown('###### Extracted first-turn QA pairs: ', unsafe_allow_html=True)
file_path = "./output_qa.jsonl"
json_list = []
with open(file_path, 'r', encoding="utf-8") as file:
    for line in file:
        json_dict = json.loads(line.strip())
        json_list.append(json_dict)
json_string = json.dumps(json_list, indent=4)
st.text_area("", value=json_string, height=200)

st.markdown('###### Extracted second-turn QA pairs: ', unsafe_allow_html=True)
file_path = "output_secondqa.jsonl"
json_list = []
with open(file_path, 'r', encoding="utf-8") as file:
    for line in file:
        json_dict = json.loads(line.strip())
        json_list.append(json_dict)
json_string = json.dumps(json_list, indent=4)
st.text_area("", value=json_string, height=200)