Surendradjh commited on
Commit
a0ee982
·
verified ·
1 Parent(s): 103a03d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -136
app.py CHANGED
@@ -1,157 +1,39 @@
1
- # import streamlit as st
2
- # from transformers import pipeline
3
- # # import torch
4
-
5
- # # Sidebar
6
- # st.sidebar.title("Choose Model")
7
- # choice = st.sidebar.radio("Choose Task Type",
8
- # ("Sentiment Analysis", "Text Summarization", "Text Generation"))
9
-
10
- # # Load pipelines (load once at top)
11
- # sentiment = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest", device=-1)
12
- # summary = pipeline("summarization", model="facebook/bart-large-cnn", device=-1)
13
- # generation = pipeline("text-generation", model="openai-community/gpt2", device=-1)
14
-
15
- # # -------- Sentiment Analysis --------
16
- # if choice == "Sentiment Analysis":
17
- # st.title("Sentiment Analysis")
18
- # text = st.text_area("Enter your quote here")
19
-
20
- # if st.button("Find"):
21
- # if text.strip():
22
- # data = sentiment(text)
23
- # st.write("**Sentiment:**", data[0]['label'])
24
- # st.write("**Confidence:**", round(data[0]['score'], 2))
25
- # else:
26
- # st.warning("Please enter some text.")
27
-
28
- # # -------- Text Summarization --------
29
- # elif choice == "Text Summarization":
30
- # st.title("Text Summarization")
31
- # option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
32
- # col1, col2 = st.columns(2)
33
-
34
- # if option == "Sample Input":
35
- # text = """Title: The Lost Treasure of Eldoria
36
- # In the small village of Eldoria, nestled between towering mountains and lush green forests, lived a young girl named Lila. She was known for her adventurous spirit and her insatiable curiosity about the tales and legends of her land. Among all the stories, the one she cherished most was that of a hidden treasure, said to be buried somewhere within the depths of the Great Whispering Woods.
37
- # One sunny morning, Lila decided it was time to seek out the treasure that had captivated her imagination for so long. Armed with nothing but her trusty map, a compass, and a small satchel filled with snacks, she set off on her adventure. As she journeyed through the village, she waved goodbye to her friends, who doubted she would find the treasure but admired her courage.
38
- # Upon entering the Great Whispering Woods, Lila felt a mixture of excitement and trepidation. The trees seemed to whisper secrets, their leaves rustling in the gentle breeze. Following the map, she navigated through winding paths, encountering friendly animals and unusual plants along the way. Each step fueled her determination.
39
- # After hours of trekking, Lila stumbled upon an ancient stone monument, covered in moss and vines. According to her map, this was the landmark that would lead her closer to the treasure. She looked around and noticed a peculiar rock formation nearby, resembling the shape of a keyhole. Remembering a phrase from one of the legends about the treasure, she recited it aloud, and to her amazement, the ground trembled slightly, revealing a hidden entrance beneath the monument.
40
- # Heart racing, Lila cautiously descended into the darkness. Inside, the cavern glistened with crystals, and faint, magical lights danced around her. As she ventured deeper, she discovered chests overflowing with gold coins, jewels, and magnificent artifacts. However, what caught her eye was a beautifully crafted locket, which glowed with a warm light.
41
- # Lila picked up the locket and felt an indescribable connection to it. It had images of her ancestors engraved on it, reminding her of her family's history. She realized that the true treasure was not just the gold and jewels, but the legacy and stories of her people.
42
- # With the locket safely in her possession, Lila decided to share her discovery with the villagers, bringing back stories and treasures from her adventure. She returned home a hero, not because of the wealth she found, but because she understood the importance of her heritage and the bonds that tied her to her community.
43
- # From that day on, Lila not only inspired others with her bravery but also sparked a newfound appreciation for the history and legends of Eldoria. The village thrived, as the tales of their ancestors brought them closer together, and Lila became a beloved storyteller, passing down the stories of the lost treasure for generations to come."""
44
-
45
- # with col1:
46
- # st.subheader("Original Text")
47
- # st.write(text)
48
-
49
- # with col2:
50
- # st.subheader("Summary")
51
- # data = summary(text)
52
- # st.write(data[0]['summary_text'])
53
-
54
- # else:
55
- # with col1:
56
- # text = st.text_area("Enter your text here", height=300)
57
-
58
- # if st.button("Find"):
59
- # if text.strip():
60
- # with col1:
61
- # st.subheader("Original Text")
62
- # st.write(text)
63
-
64
- # with col2:
65
- # st.subheader("Summary")
66
- # data = summary(text)
67
- # st.write(data[0]['summary_text'])
68
- # else:
69
- # st.warning("Please enter some text.")
70
-
71
- # # -------- Text Generation --------
72
- # elif choice == "Text Generation":
73
- # st.title("Text Generation")
74
- # option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
75
-
76
- # if option == "Sample Input":
77
- # text = "What is data science?"
78
- # st.write(text)
79
- # data = generation(text, max_length=50, num_return_sequences=1)
80
- # st.subheader("Generated Text")
81
- # st.write(data[0]['generated_text'])
82
-
83
- # else:
84
- # text = st.text_area("Enter your prompt here", height=200)
85
- # if st.button("Find"):
86
- # if text.strip():
87
- # data = generation(text, max_length=50, num_return_sequences=1)
88
- # st.subheader("Generated Text")
89
- # st.write(data[0]['generated_text'])
90
- # else:
91
- # st.warning("Please enter a prompt.")
92
-
93
-
94
-
95
  import streamlit as st
96
- from transformers import (
97
- pipeline,
98
- AutoTokenizer,
99
- AutoModelForSequenceClassification,
100
- AutoModelForSeq2SeqLM,
101
- AutoModelForCausalLM
102
- )
103
 
104
  # Sidebar
105
  st.sidebar.title("Choose Model")
106
- choice = st.sidebar.radio("Choose Task Type",
107
  ("Sentiment Analysis", "Text Summarization", "Text Generation"))
108
 
109
- # -------- Load Pipelines with Meta Tensor Fixes --------
110
-
111
- # Sentiment analysis (Roberta)
112
- sentiment_tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
113
- sentiment_model = AutoModelForSequenceClassification.from_pretrained(
114
- "cardiffnlp/twitter-roberta-base-sentiment-latest",
115
- low_cpu_mem_usage=False # 👈 critical fix
116
- )
117
- sentiment = pipeline("sentiment-analysis", model=sentiment_model, tokenizer=sentiment_tokenizer, device=-1)
118
-
119
- # Summarization (BART)
120
- summary_tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
121
- summary_model = AutoModelForSeq2SeqLM.from_pretrained(
122
- "facebook/bart-large-cnn",
123
- low_cpu_mem_usage=False
124
- )
125
- summary = pipeline("summarization", model=summary_model, tokenizer=summary_tokenizer, device=-1)
126
-
127
- # Text generation (GPT2)
128
- generation_tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
129
- generation_model = AutoModelForCausalLM.from_pretrained(
130
- "openai-community/gpt2",
131
- low_cpu_mem_usage=False
132
- )
133
- generation = pipeline("text-generation", model=generation_model, tokenizer=generation_tokenizer, device=-1)
134
-
135
- # -------- Streamlit UI --------
136
  if choice == "Sentiment Analysis":
137
  st.title("Sentiment Analysis")
138
  text = st.text_area("Enter your quote here")
139
-
140
  if st.button("Find"):
141
  if text.strip():
142
- data = sentiment(text)
143
  st.write("**Sentiment:**", data[0]['label'])
144
  st.write("**Confidence:**", round(data[0]['score'], 2))
145
  else:
146
  st.warning("Please enter some text.")
147
 
 
148
  elif choice == "Text Summarization":
149
  st.title("Text Summarization")
 
150
  option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
151
  col1, col2 = st.columns(2)
152
 
153
  if option == "Sample Input":
154
- text = """Title: The Lost Treasure of Eldoria
155
  In the small village of Eldoria, nestled between towering mountains and lush green forests, lived a young girl named Lila. She was known for her adventurous spirit and her insatiable curiosity about the tales and legends of her land. Among all the stories, the one she cherished most was that of a hidden treasure, said to be buried somewhere within the depths of the Great Whispering Woods.
156
  One sunny morning, Lila decided it was time to seek out the treasure that had captivated her imagination for so long. Armed with nothing but her trusty map, a compass, and a small satchel filled with snacks, she set off on her adventure. As she journeyed through the village, she waved goodbye to her friends, who doubted she would find the treasure but admired her courage.
157
  Upon entering the Great Whispering Woods, Lila felt a mixture of excitement and trepidation. The trees seemed to whisper secrets, their leaves rustling in the gentle breeze. Following the map, she navigated through winding paths, encountering friendly animals and unusual plants along the way. Each step fueled her determination.
@@ -164,34 +46,39 @@ elif choice == "Text Summarization":
164
  with col1:
165
  st.subheader("Original Text")
166
  st.write(text)
 
167
  with col2:
168
  st.subheader("Summary")
169
- data = summary(text)
170
  st.write(data[0]['summary_text'])
171
 
172
  else:
173
  with col1:
174
  text = st.text_area("Enter your text here", height=300)
 
175
  if st.button("Find"):
176
  if text.strip():
177
  with col1:
178
  st.subheader("Original Text")
179
  st.write(text)
 
180
  with col2:
181
  st.subheader("Summary")
182
- data = summary(text)
183
  st.write(data[0]['summary_text'])
184
  else:
185
  st.warning("Please enter some text.")
186
 
 
187
  elif choice == "Text Generation":
188
  st.title("Text Generation")
 
189
  option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
190
 
191
  if option == "Sample Input":
192
  text = "What is data science?"
193
  st.write(text)
194
- data = generation(text, max_length=50, num_return_sequences=1)
195
  st.subheader("Generated Text")
196
  st.write(data[0]['generated_text'])
197
 
@@ -199,8 +86,123 @@ elif choice == "Text Generation":
199
  text = st.text_area("Enter your prompt here", height=200)
200
  if st.button("Find"):
201
  if text.strip():
202
- data = generation(text, max_length=50, num_return_sequences=1)
203
  st.subheader("Generated Text")
204
  st.write(data[0]['generated_text'])
205
  else:
206
  st.warning("Please enter a prompt.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import pipeline
3
+ # import torch
 
 
 
 
 
4
 
5
  # Sidebar
6
  st.sidebar.title("Choose Model")
7
+ choice = st.sidebar.radio("Choose Task Type",
8
  ("Sentiment Analysis", "Text Summarization", "Text Generation"))
9
 
10
+ # Load pipelines (load once at top)
11
+ # sentiment = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest", device=-1)
12
+ # summary = pipeline("summarization", model="facebook/bart-large-cnn", device=-1)
13
+ # generation = pipeline("text-generation", model="openai-community/gpt2", device=-1)
14
+
15
+ # -------- Sentiment Analysis --------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  if choice == "Sentiment Analysis":
17
  st.title("Sentiment Analysis")
18
  text = st.text_area("Enter your quote here")
19
+ pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest", device=-1)
20
  if st.button("Find"):
21
  if text.strip():
22
+ data = pipe(text)
23
  st.write("**Sentiment:**", data[0]['label'])
24
  st.write("**Confidence:**", round(data[0]['score'], 2))
25
  else:
26
  st.warning("Please enter some text.")
27
 
28
+ # -------- Text Summarization --------
29
  elif choice == "Text Summarization":
30
  st.title("Text Summarization")
31
+ pipe = pipeline("summarization", model="facebook/bart-large-cnn", device=-1)
32
  option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
33
  col1, col2 = st.columns(2)
34
 
35
  if option == "Sample Input":
36
+ text = """Title: The Lost Treasure of Eldoria
37
  In the small village of Eldoria, nestled between towering mountains and lush green forests, lived a young girl named Lila. She was known for her adventurous spirit and her insatiable curiosity about the tales and legends of her land. Among all the stories, the one she cherished most was that of a hidden treasure, said to be buried somewhere within the depths of the Great Whispering Woods.
38
  One sunny morning, Lila decided it was time to seek out the treasure that had captivated her imagination for so long. Armed with nothing but her trusty map, a compass, and a small satchel filled with snacks, she set off on her adventure. As she journeyed through the village, she waved goodbye to her friends, who doubted she would find the treasure but admired her courage.
39
  Upon entering the Great Whispering Woods, Lila felt a mixture of excitement and trepidation. The trees seemed to whisper secrets, their leaves rustling in the gentle breeze. Following the map, she navigated through winding paths, encountering friendly animals and unusual plants along the way. Each step fueled her determination.
 
46
  with col1:
47
  st.subheader("Original Text")
48
  st.write(text)
49
+
50
  with col2:
51
  st.subheader("Summary")
52
+ data = pipe(text)
53
  st.write(data[0]['summary_text'])
54
 
55
  else:
56
  with col1:
57
  text = st.text_area("Enter your text here", height=300)
58
+
59
  if st.button("Find"):
60
  if text.strip():
61
  with col1:
62
  st.subheader("Original Text")
63
  st.write(text)
64
+
65
  with col2:
66
  st.subheader("Summary")
67
+ data = pipe(text)
68
  st.write(data[0]['summary_text'])
69
  else:
70
  st.warning("Please enter some text.")
71
 
72
+ # -------- Text Generation --------
73
  elif choice == "Text Generation":
74
  st.title("Text Generation")
75
+ pipe = pipeline("text-generation", model="openai-community/gpt2", device=-1)
76
  option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
77
 
78
  if option == "Sample Input":
79
  text = "What is data science?"
80
  st.write(text)
81
+ data = pipe(text, max_length=50, num_return_sequences=1)
82
  st.subheader("Generated Text")
83
  st.write(data[0]['generated_text'])
84
 
 
86
  text = st.text_area("Enter your prompt here", height=200)
87
  if st.button("Find"):
88
  if text.strip():
89
+ data = pipe(text, max_length=50, num_return_sequences=1)
90
  st.subheader("Generated Text")
91
  st.write(data[0]['generated_text'])
92
  else:
93
  st.warning("Please enter a prompt.")
94
+
95
+
96
+
97
+ # import streamlit as st
98
+ # from transformers import (
99
+ # pipeline,
100
+ # AutoTokenizer,
101
+ # AutoModelForSequenceClassification,
102
+ # AutoModelForSeq2SeqLM,
103
+ # AutoModelForCausalLM
104
+ # )
105
+
106
+ # # Sidebar
107
+ # st.sidebar.title("Choose Model")
108
+ # choice = st.sidebar.radio("Choose Task Type",
109
+ # ("Sentiment Analysis", "Text Summarization", "Text Generation"))
110
+
111
+ # # -------- Load Pipelines with Meta Tensor Fixes --------
112
+
113
+ # # Sentiment analysis (Roberta)
114
+ # sentiment_tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
115
+ # sentiment_model = AutoModelForSequenceClassification.from_pretrained(
116
+ # "cardiffnlp/twitter-roberta-base-sentiment-latest",
117
+ # low_cpu_mem_usage=False
118
+ # )
119
+ # sentiment = pipeline("sentiment-analysis", model=sentiment_model, tokenizer=sentiment_tokenizer, device=-1)
120
+
121
+ # # Summarization (BART)
122
+ # summary_tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
123
+ # summary_model = AutoModelForSeq2SeqLM.from_pretrained(
124
+ # "facebook/bart-large-cnn",
125
+ # low_cpu_mem_usage=False
126
+ # )
127
+ # summary = pipeline("summarization", model=summary_model, tokenizer=summary_tokenizer, device=-1)
128
+
129
+ # # Text generation (GPT2)
130
+ # generation_tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
131
+ # generation_model = AutoModelForCausalLM.from_pretrained(
132
+ # "openai-community/gpt2",
133
+ # low_cpu_mem_usage=False
134
+ # )
135
+ # generation = pipeline("text-generation", model=generation_model, tokenizer=generation_tokenizer, device=-1)
136
+
137
+ # # -------- Streamlit UI --------
138
+ # if choice == "Sentiment Analysis":
139
+ # st.title("Sentiment Analysis")
140
+ # text = st.text_area("Enter your quote here")
141
+
142
+ # if st.button("Find"):
143
+ # if text.strip():
144
+ # data = sentiment(text)
145
+ # st.write("**Sentiment:**", data[0]['label'])
146
+ # st.write("**Confidence:**", round(data[0]['score'], 2))
147
+ # else:
148
+ # st.warning("Please enter some text.")
149
+
150
+ # elif choice == "Text Summarization":
151
+ # st.title("Text Summarization")
152
+ # option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
153
+ # col1, col2 = st.columns(2)
154
+
155
+ # if option == "Sample Input":
156
+ # text = """Title: The Lost Treasure of Eldoria
157
+ # In the small village of Eldoria, nestled between towering mountains and lush green forests, lived a young girl named Lila. She was known for her adventurous spirit and her insatiable curiosity about the tales and legends of her land. Among all the stories, the one she cherished most was that of a hidden treasure, said to be buried somewhere within the depths of the Great Whispering Woods.
158
+ # One sunny morning, Lila decided it was time to seek out the treasure that had captivated her imagination for so long. Armed with nothing but her trusty map, a compass, and a small satchel filled with snacks, she set off on her adventure. As she journeyed through the village, she waved goodbye to her friends, who doubted she would find the treasure but admired her courage.
159
+ # Upon entering the Great Whispering Woods, Lila felt a mixture of excitement and trepidation. The trees seemed to whisper secrets, their leaves rustling in the gentle breeze. Following the map, she navigated through winding paths, encountering friendly animals and unusual plants along the way. Each step fueled her determination.
160
+ # After hours of trekking, Lila stumbled upon an ancient stone monument, covered in moss and vines. According to her map, this was the landmark that would lead her closer to the treasure. She looked around and noticed a peculiar rock formation nearby, resembling the shape of a keyhole. Remembering a phrase from one of the legends about the treasure, she recited it aloud, and to her amazement, the ground trembled slightly, revealing a hidden entrance beneath the monument.
161
+ # Heart racing, Lila cautiously descended into the darkness. Inside, the cavern glistened with crystals, and faint, magical lights danced around her. As she ventured deeper, she discovered chests overflowing with gold coins, jewels, and magnificent artifacts. However, what caught her eye was a beautifully crafted locket, which glowed with a warm light.
162
+ # Lila picked up the locket and felt an indescribable connection to it. It had images of her ancestors engraved on it, reminding her of her family's history. She realized that the true treasure was not just the gold and jewels, but the legacy and stories of her people.
163
+ # With the locket safely in her possession, Lila decided to share her discovery with the villagers, bringing back stories and treasures from her adventure. She returned home a hero, not because of the wealth she found, but because she understood the importance of her heritage and the bonds that tied her to her community.
164
+ # From that day on, Lila not only inspired others with her bravery but also sparked a newfound appreciation for the history and legends of Eldoria. The village thrived, as the tales of their ancestors brought them closer together, and Lila became a beloved storyteller, passing down the stories of the lost treasure for generations to come."""
165
+
166
+ # with col1:
167
+ # st.subheader("Original Text")
168
+ # st.write(text)
169
+ # with col2:
170
+ # st.subheader("Summary")
171
+ # data = summary(text)
172
+ # st.write(data[0]['summary_text'])
173
+
174
+ # else:
175
+ # with col1:
176
+ # text = st.text_area("Enter your text here", height=300)
177
+ # if st.button("Find"):
178
+ # if text.strip():
179
+ # with col1:
180
+ # st.subheader("Original Text")
181
+ # st.write(text)
182
+ # with col2:
183
+ # st.subheader("Summary")
184
+ # data = summary(text)
185
+ # st.write(data[0]['summary_text'])
186
+ # else:
187
+ # st.warning("Please enter some text.")
188
+
189
+ # elif choice == "Text Generation":
190
+ # st.title("Text Generation")
191
+ # option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
192
+
193
+ # if option == "Sample Input":
194
+ # text = "What is data science?"
195
+ # st.write(text)
196
+ # data = generation(text, max_length=50, num_return_sequences=1)
197
+ # st.subheader("Generated Text")
198
+ # st.write(data[0]['generated_text'])
199
+
200
+ # else:
201
+ # text = st.text_area("Enter your prompt here", height=200)
202
+ # if st.button("Find"):
203
+ # if text.strip():
204
+ # data = generation(text, max_length=50, num_return_sequences=1)
205
+ # st.subheader("Generated Text")
206
+ # st.write(data[0]['generated_text'])
207
+ # else:
208
+ # st.warning("Please enter a prompt.")