ahmadsanafarooq commited on
Commit
a7df232
·
verified ·
1 Parent(s): 648d3f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ st.set_page_config(page_title="Multi-Task NLP Demo", page_icon="🤖")
5
+ st.title("🤖 Multi-Task NLP with Hugging Face Transformers")
6
+ st.write(
7
+ "Choose an NLP task and enter your text below. Powered by Hugging Face Transformers!"
8
+ )
9
+
10
+ TASKS = {
11
+ "Sentiment Analysis": {
12
+ "pipeline": "sentiment-analysis",
13
+ "model": "distilbert-base-uncased-finetuned-sst-2-english",
14
+ "description": "Classify text as positive or negative sentiment.",
15
+ },
16
+ "Summarization": {
17
+ "pipeline": "summarization",
18
+ "model": "sshleifer/distilbart-cnn-12-6",
19
+ "description": "Summarize long articles or text passages.",
20
+ },
21
+ "Translation (English → French)": {
22
+ "pipeline": "translation_en_to_fr",
23
+ "model": "Helsinki-NLP/opus-mt-en-fr",
24
+ "description": "Translate English text to French.",
25
+ },
26
+ "Text Generation": {
27
+ "pipeline": "text-generation",
28
+ "model": "gpt2",
29
+ "description": "Generate text based on a prompt.",
30
+ },
31
+ }
32
+
33
+ task = st.selectbox("Choose NLP Task:", list(TASKS.keys()))
34
+ st.caption(TASKS[task]["description"])
35
+
36
+ user_input = st.text_area("Enter your text:", height=150)
37
+
38
+ @st.cache_resource
39
+ def get_pipeline(task_name):
40
+ t = TASKS[task_name]
41
+ if task_name == "Translation (English → French)":
42
+ return pipeline("translation_en_to_fr", model=t["model"])
43
+ else:
44
+ return pipeline(t["pipeline"], model=t["model"])
45
+
46
+ nlp = get_pipeline(task)
47
+
48
+ if st.button("Run"):
49
+ if not user_input.strip():
50
+ st.warning("Please enter some text first.")
51
+ else:
52
+ with st.spinner("Processing..."):
53
+ if task == "Summarization":
54
+ # Summarization expects max 1024 tokens, so we truncate for demo
55
+ result = nlp(user_input[:1024], max_length=130, min_length=30, do_sample=False)
56
+ summary = result[0]["summary_text"]
57
+ st.markdown("**Summary:**")
58
+ st.success(summary)
59
+ elif task == "Sentiment Analysis":
60
+ result = nlp(user_input)
61
+ label = result[0]["label"]
62
+ score = result[0]["score"]
63
+ st.markdown(f"**Sentiment:** `{label}` \n**Confidence:** `{score:.2%}`")
64
+ elif task == "Translation (English → French)":
65
+ result = nlp(user_input)
66
+ translation = result[0]["translation_text"]
67
+ st.markdown("**French Translation:**")
68
+ st.success(translation)
69
+ elif task == "Text Generation":
70
+ # For demo, limit to 50 tokens
71
+ result = nlp(user_input, max_length=50, num_return_sequences=1)
72
+ generated = result[0]["generated_text"]
73
+ st.markdown("**Generated Text:**")
74
+ st.info(generated)