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