Spaces:
Sleeping
Sleeping
File size: 2,826 Bytes
4ae81a6 f5f5122 4ae81a6 f5f5122 4ae81a6 f5f5122 4ae81a6 7de09ac f5f5122 4ae81a6 7de09ac f5f5122 4ae81a6 7de09ac f5f5122 4ae81a6 7de09ac 4ae81a6 7de09ac 4ae81a6 7de09ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import streamlit as st
from transformers import pipeline
# Set up the app title
st.title("Language Learning App with Hugging Face")
# Add a sidebar for user input
st.sidebar.header("Settings")
task = st.sidebar.selectbox(
"Choose a task",
["Translation", "Text Generation", "Sentiment Analysis"]
)
# Load the Hugging Face pipeline based on the selected task
if task == "Translation":
model_name = st.sidebar.selectbox(
"Choose a translation model",
["Helsinki-NLP/opus-mt-en-fr", "Helsinki-NLP/opus-mt-fr-en"]
)
st.sidebar.write(f"Selected model: {model_name}")
try:
translator = pipeline("translation", model=model_name)
except Exception as e:
st.error(f"Error loading translation model: {e}")
elif task == "Text Generation":
model_name = st.sidebar.selectbox(
"Choose a text generation model",
["gpt2", "EleutherAI/gpt-neo-125M"]
)
st.sidebar.write(f"Selected model: {model_name}")
try:
generator = pipeline("text-generation", model=model_name)
except Exception as e:
st.error(f"Error loading text generation model: {e}")
elif task == "Sentiment Analysis":
model_name = st.sidebar.selectbox(
"Choose a sentiment analysis model",
["distilbert-base-uncased-finetuned-sst-2-english"]
)
st.sidebar.write(f"Selected model: {model_name}")
try:
analyzer = pipeline("sentiment-analysis", model=model_name)
except Exception as e:
st.error(f"Error loading sentiment analysis model: {e}")
# Main app functionality
st.header(f"{task} Task")
if task == "Translation":
text = st.text_area("Enter text to translate", "Hello, how are you?")
if st.button("Translate"):
try:
translation = translator(text)
st.write("Translation:")
st.write(translation[0]['translation_text'])
except Exception as e:
st.error(f"Error during translation: {e}")
elif task == "Text Generation":
prompt = st.text_area("Enter a prompt", "Once upon a time")
max_length = st.slider("Max length", 10, 100, 50)
if st.button("Generate Text"):
try:
generated_text = generator(prompt, max_length=max_length)
st.write("Generated Text:")
st.write(generated_text[0]['generated_text'])
except Exception as e:
st.error(f"Error during text generation: {e}")
elif task == "Sentiment Analysis":
text = st.text_area("Enter text for sentiment analysis", "I love learning new languages!")
if st.button("Analyze Sentiment"):
try:
sentiment = analyzer(text)
st.write("Sentiment Analysis Result:")
st.write(sentiment[0])
except Exception as e:
st.error(f"Error during sentiment analysis: {e}") |