Surendradjh's picture
Update app.py
bc5fcf7 verified
import streamlit as st
from transformers import pipeline
# import torch
# Sidebar
st.sidebar.title("Choose Model")
choice = st.sidebar.radio("Choose Task Type",
("Sentiment Analysis", "Text Summarization", "Text Generation"))
# Load pipelines (load once at top)
# sentiment = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest", device=-1)
# summary = pipeline("summarization", model="facebook/bart-large-cnn", device=-1)
# generation = pipeline("text-generation", model="openai-community/gpt2", device=-1)
# -------- Sentiment Analysis --------
if choice == "Sentiment Analysis":
st.title("Sentiment Analysis")
text = st.text_area("Enter your quote here")
pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest", device=-1)
if st.button("Find"):
if text.strip():
data = pipe(text)
st.write("**Sentiment:**", data[0]['label'])
st.write("**Confidence:**", round(data[0]['score'], 2))
else:
st.warning("Please enter some text.")
# -------- Text Summarization --------
elif choice == "Text Summarization":
st.title("Text Summarization")
pipe = pipeline("summarization", model="facebook/bart-large-cnn", device=-1)
option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
col1, col2 = st.columns(2)
if option == "Sample Input":
text = """Title: The Lost Treasure of Eldoria
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.
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.
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.
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.
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.
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.
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.
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."""
with col1:
st.subheader("Original Text")
st.write(text)
with col2:
st.subheader("Summary")
data = pipe(text)
st.write(data[0]['summary_text'])
else:
with col1:
text = st.text_area("Enter your text here", height=300)
if st.button("Find"):
if text.strip():
with col1:
st.subheader("Original Text")
st.write(text)
with col2:
st.subheader("Summary")
data = pipe(text)
st.write(data[0]['summary_text'])
else:
st.warning("Please enter some text.")
# -------- Text Generation --------
elif choice == "Text Generation":
st.title("Text Generation")
pipe = pipeline("text-generation", model="openai-community/gpt2", device=-1)
option = st.selectbox("Choose Input Type", ["Sample Input", "Enter your own"])
if option == "Sample Input":
text = "What is data science?"
st.write(text)
data = pipe(text, max_length=50, num_return_sequences=1)
st.subheader("Generated Text")
st.write(data[0]['generated_text'])
else:
text = st.text_area("Enter your prompt here", height=200)
if st.button("Find"):
if text.strip():
data = pipe(text, max_length=50, num_return_sequences=1)
st.subheader("Generated Text")
st.write(data[0]['generated_text'])
else:
st.warning("Please enter a prompt.")