elariz commited on
Commit
49f96e6
·
verified ·
1 Parent(s): b116a39

Create test-ml-code.py

Browse files
Files changed (1) hide show
  1. test-ml-code.py +50 -0
test-ml-code.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import re
3
+ import spacy
4
+ from transformers import pipeline
5
+
6
+ # Load spaCy's English model
7
+ nlp = spacy.load("en_core_web_sm")
8
+
9
+ # Basic preprocessing: lowercasing, removing special characters
10
+ def preprocess_text(text):
11
+ doc = nlp(text.lower()) # Tokenize and lowercase the text
12
+ tokens = [token.text for token in doc if not token.is_punct] # Remove punctuation
13
+ return tokens
14
+
15
+ # Load pre-trained question-answering model
16
+ qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
17
+
18
+ # Function to answer question
19
+ def answer_question(question, context):
20
+ result = qa_model(question=question, context=context)
21
+ return result['answer']
22
+
23
+ # Streamlit App Layout
24
+ st.title("Question Answering App")
25
+ st.write("Upload a text file, ask a question, and get an answer from the text!")
26
+
27
+ # File uploader
28
+ uploaded_file = st.file_uploader("Upload a text file", type=["txt"])
29
+
30
+ if uploaded_file is not None:
31
+ # Read file
32
+ data = uploaded_file.read().decode('utf-8')
33
+
34
+ # Show the content of the file
35
+ st.write("### File Content")
36
+ st.write(data)
37
+
38
+ # Preprocess the text data
39
+ processed_data = preprocess_text(data)
40
+
41
+ # Ask question
42
+ question = st.text_input("Enter your question")
43
+
44
+ if st.button("Get Answer"):
45
+ if question:
46
+ # Get the answer from the QA model
47
+ answer = answer_question(question, data)
48
+ st.write(f"**Answer:** {answer}")
49
+ else:
50
+ st.write("Please enter a question.")