Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the tokenizer and model
|
6 |
+
model_name = 'roberta-base'
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForQuestionAnswering.from_pretrained('./results') # Path to your fine-tuned model
|
9 |
+
|
10 |
+
# Set the title for the Streamlit app
|
11 |
+
st.title("Movie Trivia Question Answering")
|
12 |
+
|
13 |
+
# Text inputs for the user
|
14 |
+
context = st.text_area("Enter the context (movie-related text):")
|
15 |
+
question = st.text_area("Enter your question:")
|
16 |
+
|
17 |
+
def get_answer(context, question):
|
18 |
+
inputs = tokenizer.encode_plus(question, context, return_tensors='pt', truncation=True, padding=True)
|
19 |
+
input_ids = inputs['input_ids'].tolist()[0]
|
20 |
+
|
21 |
+
# Get the model's answer
|
22 |
+
outputs = model(**inputs)
|
23 |
+
answer_start_scores = outputs.start_logits
|
24 |
+
answer_end_scores = outputs.end_logits
|
25 |
+
|
26 |
+
# Get the most likely beginning and end of the answer span
|
27 |
+
answer_start = torch.argmax(answer_start_scores)
|
28 |
+
answer_end = torch.argmax(answer_end_scores) + 1
|
29 |
+
|
30 |
+
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
|
31 |
+
return answer
|
32 |
+
|
33 |
+
if st.button("Get Answer"):
|
34 |
+
if context and question:
|
35 |
+
answer = get_answer(context, question)
|
36 |
+
st.subheader("Answer")
|
37 |
+
st.write(answer)
|
38 |
+
else:
|
39 |
+
st.warning("Please enter both context and question.")
|
40 |
+
|
41 |
+
# Optionally, add instructions or information about the app
|
42 |
+
st.write("""
|
43 |
+
Enter a movie-related context and a question related to the context above. The model will provide the answer based on the context provided.
|
44 |
+
""")
|