YasirAbdali commited on
Commit
d85a345
1 Parent(s): f6b717f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer
3
+ import torch
4
+
5
+ @st.cache_resource
6
+ def load_model():
7
+ model_path = "YasirAbdali/qoura_roberta" # Replace with your actual model path
8
+ model = AutoModelForQuestionAnswering.from_pretrained(model_path)
9
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
10
+ return model, tokenizer
11
+
12
+ def answer_question(question, model, tokenizer):
13
+ inputs = tokenizer(question, return_tensors="pt", max_length=512, truncation=True, padding="max_length")
14
+
15
+ with torch.no_grad():
16
+ outputs = model(**inputs)
17
+
18
+ start_logits = outputs.start_logits
19
+ end_logits = outputs.end_logits
20
+
21
+ start_index = torch.argmax(start_logits)
22
+ end_index = torch.argmax(end_logits)
23
+
24
+ answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][start_index:end_index+1]))
25
+
26
+ return answer
27
+
28
+ st.title("Quora Question Answering")
29
+
30
+ model, tokenizer = load_model()
31
+
32
+ st.write("Enter a question, and the model will provide an answer based on its knowledge.")
33
+
34
+ question = st.text_input("Question")
35
+
36
+ if st.button("Get Answer"):
37
+ if question:
38
+ answer = answer_question(question, model, tokenizer)
39
+ st.write("Answer:", answer)
40
+ else:
41
+ st.write("Please provide a question.")
42
+
43
+ # Optional: Add some example questions
44
+ st.sidebar.header("Example Questions")
45
+ example_questions = [
46
+ "What is the capital of France?",
47
+ "Who wrote 'Romeo and Juliet'?",
48
+ "What is the boiling point of water?",
49
+ "What year did World War II end?",
50
+ ]
51
+ for example in example_questions:
52
+ if st.sidebar.button(example):
53
+ st.text_input("Question", value=example)