Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# 定义模型配置选项 | |
size_lst = ["-base", "-large"] | |
cased_lst = ["-cased", "-uncased"] | |
fpretrain_lst = ["None", "-scsmall", "-scmedium", "-sclarge"] | |
finetune_lst = ["-squad", "-scqa1", "-scqa2"] | |
# 为每个选项创建下拉菜单 | |
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(["ZongqianLi/bert", size, cased, finetune]) | |
else: | |
model = "".join(["ZongqianLi/bert", size, cased, fpretrain, finetune]) | |
# 显示用户选择的模型 | |
st.write(f"Your selected model: {model}") | |
# 加载问答模型 | |
pipe = pipeline("question-answering", model=model) | |
# 设置默认的问题和上下文 | |
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 your the name of the property: ", value=default_property) | |
context = st.text_area("Enter the context: ", value=default_context, height=300) | |
# 添加一个按钮,用户点击后执行问答 | |
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.") | |