AdeebaZahra commited on
Commit
4ae81a6
·
verified ·
1 Parent(s): e7a648e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -17
app.py CHANGED
@@ -1,23 +1,62 @@
1
- import streamlit as st from transformers import pipeline from gtts import gTTS import os
 
2
 
3
- def translate_text(text, src_lang, tgt_lang): translator = pipeline("translation_{}-to-{}".format(src_lang, tgt_lang)) result = translator(text, max_length=100)[0]['translation_text'] return result
 
4
 
5
- def text_to_speech(text, lang): tts = gTTS(text=text, lang=lang) tts.save("output.mp3") return "output.mp3"
 
 
 
 
 
6
 
7
- def main(): st.title("Language Learning App") st.sidebar.header("Settings") src_lang = st.sidebar.selectbox("Select Source Language", ["en", "fr", "es", "de"]) tgt_lang = st.sidebar.selectbox("Select Target Language", ["en", "fr", "es", "de"])
 
 
 
 
 
 
 
8
 
9
- st.header("Translation")
10
- user_input = st.text_area("Enter text to translate:")
11
- if st.button("Translate"):
12
- if user_input:
13
- translation = translate_text(user_input, src_lang, tgt_lang)
14
- st.success("Translated Text: " + translation)
15
-
16
- if st.button("Listen to Translation"):
17
- audio_file = text_to_speech(translation, tgt_lang)
18
- st.audio(audio_file)
19
 
20
- st.header("Vocabulary Practice")
21
- st.write("(Coming Soon!)")
 
 
 
 
 
22
 
23
- if name == "main": main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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])