Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,15 +22,15 @@ bert_qa_model_v1 = TFAutoModelForQuestionAnswering.from_pretrained(bert_model_na
|
|
| 22 |
bert_qa_tokenizer_v1 = AutoTokenizer.from_pretrained(bert_model_name_v1)
|
| 23 |
|
| 24 |
# Load Spanish models and tokenizers
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
confli_tokenizer_spanish = AutoTokenizer.from_pretrained(
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
beto_tokenizer_spanish = AutoTokenizer.from_pretrained(
|
| 32 |
|
| 33 |
-
# Load the
|
| 34 |
confli_sqac_model_spanish = 'salsarra/ConfliBERT-Spanish-Beto-Cased-SQAC'
|
| 35 |
confli_sqac_model_spanish_qa = TFAutoModelForQuestionAnswering.from_pretrained(confli_sqac_model_spanish)
|
| 36 |
confli_sqac_tokenizer_spanish = AutoTokenizer.from_pretrained(confli_sqac_model_spanish)
|
|
@@ -56,7 +56,7 @@ def handle_error_message(e, default_limit=512):
|
|
| 56 |
|
| 57 |
return f"<span style='color: red; font-weight: bold;'>Error: {error_message}</span>"
|
| 58 |
|
| 59 |
-
# Define question_answering_v1 for ConfliBERT English
|
| 60 |
def question_answering_v1(context, question):
|
| 61 |
try:
|
| 62 |
inputs = qa_tokenizer_v1(question, context, return_tensors='tf', truncation=True)
|
|
@@ -66,11 +66,11 @@ def question_answering_v1(context, question):
|
|
| 66 |
answer = qa_tokenizer_v1.convert_tokens_to_string(
|
| 67 |
qa_tokenizer_v1.convert_ids_to_tokens(inputs['input_ids'].numpy()[0][answer_start:answer_end])
|
| 68 |
)
|
| 69 |
-
return f"<span style='
|
| 70 |
except Exception as e:
|
| 71 |
return handle_error_message(e)
|
| 72 |
-
|
| 73 |
-
# Define bert_question_answering_v1 for BERT English
|
| 74 |
def bert_question_answering_v1(context, question):
|
| 75 |
try:
|
| 76 |
inputs = bert_qa_tokenizer_v1(question, context, return_tensors='tf', truncation=True)
|
|
@@ -83,6 +83,83 @@ def bert_question_answering_v1(context, question):
|
|
| 83 |
return f"<span style='font-weight: bold;'>{answer}</span>"
|
| 84 |
except Exception as e:
|
| 85 |
return handle_error_message(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# Main comparison function with language selection
|
| 87 |
def compare_question_answering(language, context, question):
|
| 88 |
if language == "English":
|
|
@@ -94,19 +171,13 @@ def compare_question_answering(language, context, question):
|
|
| 94 |
<h2 style='color: #2e8b57; font-weight: bold;'>Answers:</h2>
|
| 95 |
</div><br>
|
| 96 |
<div>
|
| 97 |
-
<strong>ConfliBERT-cont-cased-SQuAD-v1:</strong><br>{confli_answer_v1}</div><br>
|
| 98 |
<div>
|
| 99 |
-
<strong>BERT-base-cased-SQuAD-v1:</strong><br>{bert_answer_v1}
|
| 100 |
</div><br>
|
| 101 |
<div>
|
| 102 |
-
<strong>ChatGPT:</strong><br>{chatgpt_answer}
|
| 103 |
</div><br>
|
| 104 |
-
<div>
|
| 105 |
-
<strong>Model Information:</strong><br>
|
| 106 |
-
ConfliBERT-cont-cased-SQuAD-v1: <a href='https://huggingface.co/salsarra/ConfliBERT-QA' target='_blank'>salsarra/ConfliBERT-QA</a><br>
|
| 107 |
-
BERT-base-cased-SQuAD-v1: <a href='https://huggingface.co/salsarra/BERT-base-cased-SQuAD-v1' target='_blank'>salsarra/BERT-base-cased-SQuAD-v1</a><br>
|
| 108 |
-
ChatGPT (GPT-3.5 Turbo): <a href='https://platform.openai.com/docs/models/gpt-3-5' target='_blank'>OpenAI API</a><br>
|
| 109 |
-
</div>
|
| 110 |
"""
|
| 111 |
elif language == "Spanish":
|
| 112 |
confli_answer_spanish = question_answering_spanish(context, question)
|
|
@@ -119,30 +190,22 @@ def compare_question_answering(language, context, question):
|
|
| 119 |
<h2 style='color: #2e8b57; font-weight: bold;'>Answers:</h2>
|
| 120 |
</div><br>
|
| 121 |
<div>
|
| 122 |
-
<strong>ConfliBERT-Spanish-Beto-Cased-NewsQA:</strong><br>{confli_answer_spanish}</div><br>
|
| 123 |
<div>
|
| 124 |
-
<strong>Beto-Spanish-Cased-NewsQA:</strong><br>{beto_answer_spanish}
|
| 125 |
</div><br>
|
| 126 |
<div>
|
| 127 |
-
<strong>ConfliBERT-Spanish-Beto-Cased-SQAC:</strong><br>{confli_sqac_answer_spanish}
|
| 128 |
</div><br>
|
| 129 |
<div>
|
| 130 |
-
<strong>Beto-Spanish-Cased-SQAC:</strong><br>{beto_sqac_answer_spanish}
|
| 131 |
</div><br>
|
| 132 |
<div>
|
| 133 |
-
<strong>ChatGPT:</strong><br>{chatgpt_answer_spanish}
|
| 134 |
</div><br>
|
| 135 |
-
<div>
|
| 136 |
-
<strong>Model Information:</strong><br>
|
| 137 |
-
ConfliBERT-Spanish-Beto-Cased-NewsQA: <a href='https://huggingface.co/salsarra/ConfliBERT-Spanish-Beto-Cased-NewsQA' target='_blank'>salsarra/ConfliBERT-Spanish-Beto-Cased-NewsQA</a><br>
|
| 138 |
-
Beto-Spanish-Cased-NewsQA: <a href='https://huggingface.co/salsarra/Beto-Spanish-Cased-NewsQA' target='_blank'>salsarra/Beto-Spanish-Cased-NewsQA</a><br>
|
| 139 |
-
ConfliBERT-Spanish-Beto-Cased-SQAC: <a href='https://huggingface.co/salsarra/ConfliBERT-Spanish-Beto-Cased-SQAC' target='_blank'>salsarra/ConfliBERT-Spanish-Beto-Cased-SQAC</a><br>
|
| 140 |
-
Beto-Spanish-Cased-SQAC: <a href='https://huggingface.co/salsarra/Beto-Spanish-Cased-SQAC' target='_blank'>salsarra/Beto-Spanish-Cased-SQAC</a><br>
|
| 141 |
-
ChatGPT (GPT-3.5 Turbo): <a href='https://platform.openai.com/docs/models/gpt-3-5' target='_blank'>OpenAI API</a><br>
|
| 142 |
-
</div>
|
| 143 |
"""
|
| 144 |
|
| 145 |
-
#
|
| 146 |
with gr.Blocks(css="""
|
| 147 |
body {
|
| 148 |
background-color: #f0f8ff;
|
|
@@ -162,19 +225,6 @@ with gr.Blocks(css="""
|
|
| 162 |
text-align: center;
|
| 163 |
font-size: 1.5em;
|
| 164 |
}
|
| 165 |
-
.gradio-container {
|
| 166 |
-
max-width: 100%;
|
| 167 |
-
margin: 10px auto;
|
| 168 |
-
padding: 10px;
|
| 169 |
-
background-color: #ffffff;
|
| 170 |
-
border-radius: 10px;
|
| 171 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 172 |
-
}
|
| 173 |
-
.button-row {
|
| 174 |
-
display: flex;
|
| 175 |
-
justify-content: center;
|
| 176 |
-
gap: 10px;
|
| 177 |
-
}
|
| 178 |
""") as demo:
|
| 179 |
|
| 180 |
gr.Markdown("# [ConfliBERT-QA](https://eventdata.utdallas.edu/conflibert/)", elem_id="title")
|
|
@@ -185,7 +235,7 @@ with gr.Blocks(css="""
|
|
| 185 |
question = gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question")
|
| 186 |
output = gr.HTML(label="Output")
|
| 187 |
|
| 188 |
-
with gr.Row(
|
| 189 |
clear_btn = gr.Button("Clear")
|
| 190 |
submit_btn = gr.Button("Submit")
|
| 191 |
|
|
|
|
| 22 |
bert_qa_tokenizer_v1 = AutoTokenizer.from_pretrained(bert_model_name_v1)
|
| 23 |
|
| 24 |
# Load Spanish models and tokenizers
|
| 25 |
+
confli_model_spanish_name = 'salsarra/ConfliBERT-Spanish-Beto-Cased-NewsQA'
|
| 26 |
+
confli_model_spanish = TFAutoModelForQuestionAnswering.from_pretrained(confli_model_spanish_name)
|
| 27 |
+
confli_tokenizer_spanish = AutoTokenizer.from_pretrained(confli_model_spanish_name)
|
| 28 |
|
| 29 |
+
beto_model_spanish_name = 'salsarra/Beto-Spanish-Cased-NewsQA'
|
| 30 |
+
beto_model_spanish = TFAutoModelForQuestionAnswering.from_pretrained(beto_model_spanish_name)
|
| 31 |
+
beto_tokenizer_spanish = AutoTokenizer.from_pretrained(beto_model_spanish_name)
|
| 32 |
|
| 33 |
+
# Load the additional Spanish models
|
| 34 |
confli_sqac_model_spanish = 'salsarra/ConfliBERT-Spanish-Beto-Cased-SQAC'
|
| 35 |
confli_sqac_model_spanish_qa = TFAutoModelForQuestionAnswering.from_pretrained(confli_sqac_model_spanish)
|
| 36 |
confli_sqac_tokenizer_spanish = AutoTokenizer.from_pretrained(confli_sqac_model_spanish)
|
|
|
|
| 56 |
|
| 57 |
return f"<span style='color: red; font-weight: bold;'>Error: {error_message}</span>"
|
| 58 |
|
| 59 |
+
# Define question_answering_v1 for ConfliBERT English with truncation=True
|
| 60 |
def question_answering_v1(context, question):
|
| 61 |
try:
|
| 62 |
inputs = qa_tokenizer_v1(question, context, return_tensors='tf', truncation=True)
|
|
|
|
| 66 |
answer = qa_tokenizer_v1.convert_tokens_to_string(
|
| 67 |
qa_tokenizer_v1.convert_ids_to_tokens(inputs['input_ids'].numpy()[0][answer_start:answer_end])
|
| 68 |
)
|
| 69 |
+
return f"<span style='font-weight: bold;'>{answer}</span>"
|
| 70 |
except Exception as e:
|
| 71 |
return handle_error_message(e)
|
| 72 |
+
|
| 73 |
+
# Define bert_question_answering_v1 for BERT English with truncation=True
|
| 74 |
def bert_question_answering_v1(context, question):
|
| 75 |
try:
|
| 76 |
inputs = bert_qa_tokenizer_v1(question, context, return_tensors='tf', truncation=True)
|
|
|
|
| 83 |
return f"<span style='font-weight: bold;'>{answer}</span>"
|
| 84 |
except Exception as e:
|
| 85 |
return handle_error_message(e)
|
| 86 |
+
|
| 87 |
+
# Define question_answering_spanish for ConfliBERT-Spanish-Beto-Cased-NewsQA
|
| 88 |
+
def question_answering_spanish(context, question):
|
| 89 |
+
try:
|
| 90 |
+
inputs = confli_tokenizer_spanish(question, context, return_tensors='tf', truncation=True)
|
| 91 |
+
outputs = confli_model_spanish(inputs)
|
| 92 |
+
answer_start = tf.argmax(outputs.start_logits, axis=1).numpy()[0]
|
| 93 |
+
answer_end = tf.argmax(outputs.end_logits, axis=1).numpy()[0] + 1
|
| 94 |
+
answer = confli_tokenizer_spanish.convert_tokens_to_string(
|
| 95 |
+
confli_tokenizer_spanish.convert_ids_to_tokens(inputs['input_ids'].numpy()[0][answer_start:answer_end])
|
| 96 |
+
)
|
| 97 |
+
return f"<span style='font-weight: bold;'>{answer}</span>"
|
| 98 |
+
except Exception as e:
|
| 99 |
+
return handle_error_message(e)
|
| 100 |
+
|
| 101 |
+
# Define beto_question_answering_spanish for Beto-Spanish-Cased-NewsQA
|
| 102 |
+
def beto_question_answering_spanish(context, question):
|
| 103 |
+
try:
|
| 104 |
+
inputs = beto_tokenizer_spanish(question, context, return_tensors='tf', truncation=True)
|
| 105 |
+
outputs = beto_model_spanish(inputs)
|
| 106 |
+
answer_start = tf.argmax(outputs.start_logits, axis=1).numpy()[0]
|
| 107 |
+
answer_end = tf.argmax(outputs.end_logits, axis=1).numpy()[0] + 1
|
| 108 |
+
answer = beto_tokenizer_spanish.convert_tokens_to_string(
|
| 109 |
+
beto_tokenizer_spanish.convert_ids_to_tokens(inputs['input_ids'].numpy()[0][answer_start:answer_end])
|
| 110 |
+
)
|
| 111 |
+
return f"<span style='font-weight: bold;'>{answer}</span>"
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return handle_error_message(e)
|
| 114 |
+
|
| 115 |
+
# Define confli_sqac_question_answering_spanish for ConfliBERT-Spanish-Beto-Cased-SQAC
|
| 116 |
+
def confli_sqac_question_answering_spanish(context, question):
|
| 117 |
+
inputs = confli_sqac_tokenizer_spanish.encode_plus(question, context, return_tensors="tf", truncation=True)
|
| 118 |
+
outputs = confli_sqac_model_spanish_qa(inputs)
|
| 119 |
+
answer_start = tf.argmax(outputs.start_logits, axis=1).numpy()[0]
|
| 120 |
+
answer_end = tf.argmax(outputs.end_logits, axis=1).numpy()[0] + 1
|
| 121 |
+
answer = confli_sqac_tokenizer_spanish.convert_tokens_to_string(
|
| 122 |
+
confli_sqac_tokenizer_spanish.convert_ids_to_tokens(inputs['input_ids'].numpy()[0][answer_start:answer_end])
|
| 123 |
+
)
|
| 124 |
+
return f"<span style='font-weight: bold;'>{answer}</span>"
|
| 125 |
+
|
| 126 |
+
# Define beto_sqac_question_answering_spanish for Beto-Spanish-Cased-SQAC
|
| 127 |
+
def beto_sqac_question_answering_spanish(context, question):
|
| 128 |
+
inputs = beto_sqac_tokenizer_spanish.encode_plus(question, context, return_tensors="tf", truncation=True)
|
| 129 |
+
outputs = beto_sqac_model_spanish_qa(inputs)
|
| 130 |
+
answer_start = tf.argmax(outputs.start_logits, axis=1).numpy()[0]
|
| 131 |
+
answer_end = tf.argmax(outputs.end_logits, axis=1).numpy()[0] + 1
|
| 132 |
+
answer = beto_sqac_tokenizer_spanish.convert_tokens_to_string(
|
| 133 |
+
beto_sqac_tokenizer_spanish.convert_ids_to_tokens(inputs['input_ids'].numpy()[0][answer_start:answer_end])
|
| 134 |
+
)
|
| 135 |
+
return f"<span style='font-weight: bold;'>{answer}</span>"
|
| 136 |
+
|
| 137 |
+
# Define a function to get ChatGPT's answer in English
|
| 138 |
+
def chatgpt_question_answering(context, question):
|
| 139 |
+
prompt = f"Context: {context}\nQuestion: {question}\nAnswer:"
|
| 140 |
+
response = openai.ChatCompletion.create(
|
| 141 |
+
model="gpt-3.5-turbo",
|
| 142 |
+
messages=[
|
| 143 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 144 |
+
{"role": "user", "content": prompt}
|
| 145 |
+
],
|
| 146 |
+
max_tokens=150
|
| 147 |
+
)
|
| 148 |
+
return response['choices'][0]['message']['content'].strip()
|
| 149 |
+
|
| 150 |
+
# Define a function to get ChatGPT's answer in Spanish
|
| 151 |
+
def chatgpt_question_answering_spanish(context, question):
|
| 152 |
+
prompt = f"Contexto: {context}\nPregunta: {question}\nRespuesta:"
|
| 153 |
+
response = openai.ChatCompletion.create(
|
| 154 |
+
model="gpt-3.5-turbo",
|
| 155 |
+
messages=[
|
| 156 |
+
{"role": "system", "content": "You are a helpful assistant that responds in Spanish."},
|
| 157 |
+
{"role": "user", "content": prompt}
|
| 158 |
+
],
|
| 159 |
+
max_tokens=150
|
| 160 |
+
)
|
| 161 |
+
return response['choices'][0]['message']['content'].strip()
|
| 162 |
+
|
| 163 |
# Main comparison function with language selection
|
| 164 |
def compare_question_answering(language, context, question):
|
| 165 |
if language == "English":
|
|
|
|
| 171 |
<h2 style='color: #2e8b57; font-weight: bold;'>Answers:</h2>
|
| 172 |
</div><br>
|
| 173 |
<div>
|
| 174 |
+
<strong style='color: green; font-weight: bold;'>ConfliBERT-cont-cased-SQuAD-v1:</strong><br><span style='font-weight: bold;'>{confli_answer_v1}</span></div><br>
|
| 175 |
<div>
|
| 176 |
+
<strong style='color: orange; font-weight: bold;'>BERT-base-cased-SQuAD-v1:</strong><br><span style='font-weight: bold;'>{bert_answer_v1}</span>
|
| 177 |
</div><br>
|
| 178 |
<div>
|
| 179 |
+
<strong style='color: #74AA9C; font-weight: bold;'>ChatGPT:</strong><br><span style='font-weight: bold;'>{chatgpt_answer}</span>
|
| 180 |
</div><br>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
"""
|
| 182 |
elif language == "Spanish":
|
| 183 |
confli_answer_spanish = question_answering_spanish(context, question)
|
|
|
|
| 190 |
<h2 style='color: #2e8b57; font-weight: bold;'>Answers:</h2>
|
| 191 |
</div><br>
|
| 192 |
<div>
|
| 193 |
+
<strong style='color: green; font-weight: bold;'>ConfliBERT-Spanish-Beto-Cased-NewsQA:</strong><br><span style='font-weight: bold;'>{confli_answer_spanish}</span></div><br>
|
| 194 |
<div>
|
| 195 |
+
<strong style='color: orange; font-weight: bold;'>Beto-Spanish-Cased-NewsQA:</strong><br><span style='font-weight: bold;'>{beto_answer_spanish}</span>
|
| 196 |
</div><br>
|
| 197 |
<div>
|
| 198 |
+
<strong style='color: green; font-weight: bold;'>ConfliBERT-Spanish-Beto-Cased-SQAC:</strong><br><span style='font-weight: bold;'>{confli_sqac_answer_spanish}</span>
|
| 199 |
</div><br>
|
| 200 |
<div>
|
| 201 |
+
<strong style='color: orange; font-weight: bold;'>Beto-Spanish-Cased-SQAC:</strong><br><span style='font-weight: bold;'>{beto_sqac_answer_spanish}</span>
|
| 202 |
</div><br>
|
| 203 |
<div>
|
| 204 |
+
<strong style='color: #74AA9C; font-weight: bold;'>ChatGPT:</strong><br><span style='font-weight: bold;'>{chatgpt_answer_spanish}
|
| 205 |
</div><br>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
"""
|
| 207 |
|
| 208 |
+
# Gradio interface setup
|
| 209 |
with gr.Blocks(css="""
|
| 210 |
body {
|
| 211 |
background-color: #f0f8ff;
|
|
|
|
| 225 |
text-align: center;
|
| 226 |
font-size: 1.5em;
|
| 227 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
""") as demo:
|
| 229 |
|
| 230 |
gr.Markdown("# [ConfliBERT-QA](https://eventdata.utdallas.edu/conflibert/)", elem_id="title")
|
|
|
|
| 235 |
question = gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question")
|
| 236 |
output = gr.HTML(label="Output")
|
| 237 |
|
| 238 |
+
with gr.Row():
|
| 239 |
clear_btn = gr.Button("Clear")
|
| 240 |
submit_btn = gr.Button("Submit")
|
| 241 |
|