asony999 commited on
Commit
9ac08a7
·
verified ·
1 Parent(s): e55a6c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -31
app.py CHANGED
@@ -1,31 +1,48 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
-
4
- # Title of the app
5
- st.title("Hugging Face Transformers Demo")
6
- st.write("Enter text to analyze sentiment or generate new text:")
7
-
8
- # Load pre-trained models
9
- sentiment_analyzer = pipeline("sentiment-analysis")
10
- text_generator = pipeline("text-generation", model="gpt2")
11
-
12
- # User input
13
- user_input = st.text_area("Input Text")
14
-
15
- # Sentiment Analysis
16
- if st.button("Analyze Sentiment"):
17
- if user_input:
18
- result = sentiment_analyzer(user_input)
19
- st.write("Sentiment Analysis Result:")
20
- st.write(result)
21
- else:
22
- st.write("Please enter some text to analyze.")
23
-
24
- # Text Generation
25
- if st.button("Generate Text"):
26
- if user_input:
27
- result = text_generator(user_input, max_length=50)
28
- st.write("Generated Text:")
29
- st.write(result[0]['generated_text'])
30
- else:
31
- st.write("Please enter some text to generate.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ # Title of the app
5
+ st.title("Multi-Task NLP App with Hugging Face Transformers")
6
+ st.write("Explore advanced NLP tasks: Sentiment Analysis, Text Summarization, and Question Answering.")
7
+
8
+ # Load pre-trained models
9
+ sentiment_analyzer = pipeline("sentiment-analysis")
10
+ summarizer = pipeline("summarization")
11
+ qa_pipeline = pipeline("question-answering")
12
+
13
+ # Sidebar for task selection
14
+ task = st.sidebar.selectbox("Choose a Task", ["Sentiment Analysis", "Text Summarization", "Question Answering"])
15
+
16
+ if task == "Sentiment Analysis":
17
+ st.header("Sentiment Analysis")
18
+ user_input = st.text_area("Enter text for sentiment analysis:")
19
+ if st.button("Analyze Sentiment"):
20
+ if user_input:
21
+ result = sentiment_analyzer(user_input)
22
+ st.write("Sentiment Analysis Result:")
23
+ st.write(result)
24
+ else:
25
+ st.write("Please enter some text to analyze.")
26
+
27
+ elif task == "Text Summarization":
28
+ st.header("Text Summarization")
29
+ user_input = st.text_area("Enter text to summarize:")
30
+ if st.button("Summarize"):
31
+ if user_input:
32
+ result = summarizer(user_input, max_length=50, min_length=25, do_sample=False)
33
+ st.write("Summary:")
34
+ st.write(result[0]['summary_text'])
35
+ else:
36
+ st.write("Please enter some text to summarize.")
37
+
38
+ elif task == "Question Answering":
39
+ st.header("Question Answering")
40
+ context = st.text_area("Enter context (the text to ask questions about):")
41
+ question = st.text_input("Enter your question:")
42
+ if st.button("Get Answer"):
43
+ if context and question:
44
+ result = qa_pipeline(question=question, context=context)
45
+ st.write("Answer:")
46
+ st.write(result['answer'])
47
+ else:
48
+ st.write("Please provide both context and question.")