Spaces:
Build error
Build error
| import os | |
| import streamlit as st | |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, TranslationPipeline, pipeline | |
| print("Loading the models...") | |
| hf_token = os.getenv("HF_AUTH_TOKEN") | |
| if not hf_token: | |
| raise ValueError("Hugging Face token not found. Please set the HF_AUTH_TOKEN environment variable.") | |
| st.title("Translation and Summarization Web App") | |
| st.write(""" | |
| ### Powered by Hugging Face and Streamlit | |
| This app uses pre-trained NLP models from Hugging Face to translate text between languages and summarize it. | |
| Enter text in the source language, select source and target languages, and see the translated and summarized text! | |
| """) | |
| def load_translation_pipeline(): | |
| print("Loading translation model...") | |
| model = AutoModelForSeq2SeqLM.from_pretrained( | |
| 'issai/tilmash', | |
| use_auth_token=hf_token | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| "issai/tilmash", | |
| use_auth_token=hf_token | |
| ) | |
| return TranslationPipeline(model=model, tokenizer=tokenizer, max_length=1000) | |
| def load_summarization_pipeline(): | |
| print("Loading summarization model...") | |
| return pipeline("summarization", model="facebook/bart-large-cnn") | |
| tilmash = load_translation_pipeline() | |
| summarizer = load_summarization_pipeline() | |
| languages = { | |
| "Kazakh (Cyrillic)": "kaz_Cyrl", | |
| "Russian (Cyrillic)": "rus_Cyrl", | |
| "English (Latin)": "eng_Latn", | |
| "Turkish (Latin)": "tur_Latn" | |
| } | |
| src_lang = st.selectbox("Select source language:", options=list(languages.keys()), index=0) | |
| tgt_lang = st.selectbox("Select target language:", options=list(languages.keys()), index=2) | |
| user_input = st.text_area("Enter text to translate:", "") | |
| if st.button("Translate and Summarize Text"): | |
| if user_input.strip(): | |
| translation_result = tilmash(user_input, src_lang=languages[src_lang], tgt_lang=languages[tgt_lang]) | |
| translated_text = translation_result[0]['translation_text'] | |
| st.subheader("Translation Result") | |
| st.write(f"**Translated Text:** {translated_text}") | |
| if len(translated_text) > 20: | |
| summary_result = summarizer(translated_text, max_length=130, min_length=30, do_sample=False) | |
| summarized_text = summary_result[0]['summary_text'] | |
| st.subheader("Summarization Result") | |
| st.write(f"**Summarized Text:** {summarized_text}") | |
| else: | |
| st.warning("Translated text is too short for summarization!") | |
| else: | |
| st.warning("Please enter some text to translate!") | |
| st.sidebar.title("About") | |
| st.sidebar.info(""" | |
| This app demonstrates the use of Hugging Face's NLP models with Streamlit. | |
| It uses the `issai/tilmash` model for translation and `facebook/bart-large-cnn` for summarization. | |
| """) | |
| print('After translation and summarization operation') | |