Iliyasss commited on
Commit
71dd60b
·
1 Parent(s): 302b21a

translation

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -1,47 +1,53 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
 
4
  print("Loading the model...")
5
 
6
  # Title and Description
7
- st.title("Sentiment Analysis Web App")
8
  st.write("""
9
  ### Powered by Hugging Face and Streamlit
10
- This app uses a pre-trained NLP model from Hugging Face to analyze the sentiment of the text you enter.
11
- Try entering a sentence to see if it's positive, negative, or neutral!
12
  """)
13
 
14
- # Initialize Hugging Face Sentiment Analysis Pipeline
15
  @st.cache_resource
16
- def load_model():
17
- print("before load model")
18
- return pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
 
 
19
 
20
- sentiment_analyzer = load_model()
21
 
22
- # Input Text from User
23
- user_input = st.text_area("Enter some text to analyze:", "Streamlit and Hugging Face make NLP fun!")
 
 
 
 
24
 
25
- # Analyze Sentiment
26
- if st.button("Analyze Sentiment"):
27
- print("button click")
 
 
 
28
  if user_input.strip():
29
- result = sentiment_analyzer(user_input)[0]
30
- sentiment = result['label']
31
- score = result['score']
32
-
33
- # Display the Result
34
- st.subheader("Sentiment Analysis Result")
35
- st.write(f"**Sentiment:** {sentiment}")
36
- st.write(f"**Confidence Score:** {score:.2f}")
37
  else:
38
- st.warning("Please enter some text to analyze!")
39
 
40
  # Sidebar with About Information
41
  st.sidebar.title("About")
42
  st.sidebar.info("""
43
  This app demonstrates the use of Hugging Face's NLP models with Streamlit.
44
- It uses the `distilbert-base-uncased-finetuned-sst-2-english` model for sentiment analysis.
45
  """)
46
 
47
- print('after')
 
1
  import streamlit as st
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, TranslationPipeline
3
 
4
  print("Loading the model...")
5
 
6
  # Title and Description
7
+ st.title("Translation Web App")
8
  st.write("""
9
  ### Powered by Hugging Face and Streamlit
10
+ This app uses a pre-trained NLP model from Hugging Face to translate text between languages.
11
+ Enter text in the source language, select source and target languages, and see the translation!
12
  """)
13
 
14
+ # Initialize Hugging Face Translation Pipeline
15
  @st.cache_resource
16
+ def load_translation_pipeline():
17
+ print("Loading translation model...")
18
+ model = AutoModelForSeq2SeqLM.from_pretrained('issai/tilmash')
19
+ tokenizer = AutoTokenizer.from_pretrained("issai/tilmash")
20
+ return TranslationPipeline(model=model, tokenizer=tokenizer, max_length=1000)
21
 
22
+ tilmash = load_translation_pipeline()
23
 
24
+ languages = {
25
+ "Kazakh (Cyrillic)": "kaz_Cyrl",
26
+ "Russian (Cyrillic)": "rus_Cyrl",
27
+ "English (Latin)": "eng_Latn",
28
+ "Turkish (Latin)": "tur_Latn"
29
+ }
30
 
31
+ src_lang = st.selectbox("Select source language:", options=list(languages.keys()), index=0)
32
+ tgt_lang = st.selectbox("Select target language:", options=list(languages.keys()), index=2)
33
+
34
+ user_input = st.text_area("Enter text to translate:", "")
35
+
36
+ if st.button("Translate Text"):
37
  if user_input.strip():
38
+ result = tilmash(user_input, src_lang=languages[src_lang], tgt_lang=languages[tgt_lang])
39
+ translation = result[0]['translation_text']
40
+
41
+ st.subheader("Translation Result")
42
+ st.write(f"**Translated Text:** {translation}")
 
 
 
43
  else:
44
+ st.warning("Please enter some text to translate!")
45
 
46
  # Sidebar with About Information
47
  st.sidebar.title("About")
48
  st.sidebar.info("""
49
  This app demonstrates the use of Hugging Face's NLP models with Streamlit.
50
+ It uses the `issai/tilmash` model for translation between languages such as Kazakh, Russian, English, and Turkish.
51
  """)
52
 
53
+ print('After translation operation')