Yereque commited on
Commit
badb581
·
1 Parent(s): f7b4690

first commit

Browse files
Files changed (1) hide show
  1. app.py +33 -31
app.py CHANGED
@@ -1,47 +1,49 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
 
4
- print("Loading the model...")
5
-
6
- # Title and Description
7
- st.title("Sentiment Analysis Web App")
8
  st.write("""
9
  ### Powered by Hugging Face and Streamlit
10
- This app uses a pre-trained NLP model from Hugging Face to analyze the sentiment of the text you enter.
11
- Try entering a sentence to see if it's positive, negative, or neutral!
12
  """)
13
 
14
- # Initialize Hugging Face Sentiment Analysis Pipeline
15
  @st.cache_resource
16
  def load_model():
17
- print("before load model")
18
- return pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
19
-
20
- sentiment_analyzer = load_model()
21
-
22
- # Input Text from User
23
- user_input = st.text_area("Enter some text to analyze:", "Streamlit and Hugging Face make NLP fun!")
24
-
25
- # Analyze Sentiment
26
- if st.button("Analyze Sentiment"):
27
- print("button click")
28
- if user_input.strip():
29
- result = sentiment_analyzer(user_input)[0]
30
- sentiment = result['label']
31
- score = result['score']
32
-
33
- # Display the Result
34
- st.subheader("Sentiment Analysis Result")
35
- st.write(f"**Sentiment:** {sentiment}")
36
- st.write(f"**Confidence Score:** {score:.2f}")
 
 
 
 
 
 
37
  else:
38
- st.warning("Please enter some text to analyze!")
39
 
40
  # Sidebar with About Information
41
  st.sidebar.title("About")
42
  st.sidebar.info("""
43
  This app demonstrates the use of Hugging Face's NLP models with Streamlit.
44
- It uses the distilbert-base-uncased-finetuned-sst-2-english model for sentiment analysis.
45
  """)
46
 
47
- print('after')
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
3
 
4
+ st.title("Question Answering Web App")
 
 
 
5
  st.write("""
6
  ### Powered by Hugging Face and Streamlit
7
+ This app uses a pre-trained NLP model from Hugging Face to answer questions based on the text you provide.
8
+ Try entering a context and a question to get an answer!
9
  """)
10
 
11
+ # Load the tokenizer and model
12
  @st.cache_resource
13
  def load_model():
14
+ tokenizer = AutoTokenizer.from_pretrained("Rakib/roberta-base-on-cuad")
15
+ model = AutoModelForQuestionAnswering.from_pretrained("Rakib/roberta-base-on-cuad")
16
+ return tokenizer, model
17
+
18
+ tokenizer, model = load_model()
19
+
20
+ # Define the question-answering pipeline
21
+ @st.cache_resource
22
+ def get_qa_pipeline():
23
+ return pipeline("question-answering", model=model, tokenizer=tokenizer)
24
+
25
+ qa_pipeline = get_qa_pipeline()
26
+
27
+ # UI input for context and question
28
+ context = st.text_area("Enter the context:", "Type the paragraph here where the answer will be extracted.")
29
+ question = st.text_input("Enter the question:", "What is being asked here?")
30
+
31
+ # Button to perform question answering
32
+ if st.button("Answer Question"):
33
+ if context and question:
34
+ result = qa_pipeline(question=question, context=context)
35
+ answer = result['answer']
36
+
37
+ # Display the result
38
+ st.subheader("Answer")
39
+ st.write(f"**Answer:** {answer}")
40
  else:
41
+ st.warning("Please enter both context and question!")
42
 
43
  # Sidebar with About Information
44
  st.sidebar.title("About")
45
  st.sidebar.info("""
46
  This app demonstrates the use of Hugging Face's NLP models with Streamlit.
47
+ It uses the `Rakib/roberta-base-on-cuad` model for question answering tasks.
48
  """)
49