Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Initialize pipelines for each NLP task
|
5 |
+
ner = pipeline("ner")
|
6 |
+
qa = pipeline("question-answering")
|
7 |
+
text_gen = pipeline("text-generation")
|
8 |
+
summarization = pipeline("summarization")
|
9 |
+
|
10 |
+
def main():
|
11 |
+
"""
|
12 |
+
This function builds the Streamlit app with user input for NLP tasks.
|
13 |
+
"""
|
14 |
+
|
15 |
+
# Title and description for the app
|
16 |
+
st.title("Multi-Task NLP App")
|
17 |
+
st.write("Perform various NLP tasks on your text input.")
|
18 |
+
|
19 |
+
# Text input field
|
20 |
+
user_input = st.text_input("Enter Text Here:")
|
21 |
+
|
22 |
+
# Select task from dropdown menu
|
23 |
+
selected_task = st.selectbox("Choose NLP Task:", ["NER", "QA", "Text Generation", "Text Summarization"])
|
24 |
+
|
25 |
+
# Perform NLP task based on selection
|
26 |
+
if user_input and selected_task:
|
27 |
+
if selected_task == "NER":
|
28 |
+
analysis = ner(user_input)
|
29 |
+
st.write("**Named Entities:**")
|
30 |
+
for entity in analysis:
|
31 |
+
st.write(f"- {entity['word']} ({entity['entity_group']})")
|
32 |
+
elif selected_task == "QA":
|
33 |
+
# Provide context (optional) for QA
|
34 |
+
context = st.text_input("Enter Context (Optional):", "")
|
35 |
+
if context:
|
36 |
+
analysis = qa(question="Your question", context=context, padding="max_length")
|
37 |
+
else:
|
38 |
+
analysis = qa(question="Your question", context=user_input, padding="max_length")
|
39 |
+
st.write("**Answer:**", analysis['answer'])
|
40 |
+
elif selected_task == "Text Generation":
|
41 |
+
# Choose generation task from another dropdown
|
42 |
+
generation_task = st.selectbox("Choose Generation Task:", ["Text summarization (short)", "Poem", "Code"])
|
43 |
+
if generation_task == "Text summarization (short)":
|
44 |
+
analysis = summarization(user_input, max_length=50, truncation=True)
|
45 |
+
else:
|
46 |
+
# Experiment with different prompts and max_length for creative text generation
|
47 |
+
prompt = st.text_input("Enter Prompt (Optional):", "")
|
48 |
+
analysis = text_gen(prompt if prompt else user_input, max_length=50, truncation=True)
|
49 |
+
st.write("**Generated Text:**", analysis[0]['generated_text'])
|
50 |
+
else:
|
51 |
+
analysis = summarization(user_input, max_length=100, truncation=True)
|
52 |
+
st.write("**Summary:**", analysis[0]['summary_text'])
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
main()
|