import streamlit as st
from streamlit_option_menu import option_menu
import requests
import os
API_TOKEN = os.environ.get("API_TOKEN")
#changement du logo et du titre de mon application en anglais
st.set_page_config(page_title="TSC", page_icon="books", layout="centered", menu_items=None)
col1, col2, col3 = st.columns(3)
xx = col1
ar = col2.image("keyce.jpg", width=180)
yy = col3
# menu horizontal
selected = option_menu(None, ["Home", "Translate", "Summarization", "Chatbot"],
icons=['house', 'globe', "pencil", "chat"],
menu_icon="cast", default_index=0, orientation="horizontal",
styles={
"container": {"padding":"5!important", "background-color":"#000E38", "color":"white"},
"icon": {"color":"#fff", "font-size":"16px"},
"nav-link": {"font-size":"16px", "text-align":"left", "margin":"0px", "--hover-color":"#f9d1ac", "color":"white"},
"nav-link-selected": {"background-color": "#FF9633", "color": "white"},
})
if selected == "Home":
st.subheader("Welcome to our app !!")
st.info(":blue[We are the present of tomorrow! Please like it after use!]")
#Insertion d'une image
st.image("nlp1.png", width=None)
st.write("Hello to everybody! In this Artificial Intelligence app, we offer you a THREE-IN-ONE package. First of all, we've got the (Translate) menu, which lets you do a good job of translating into a few foreign languages, then we've got the (Summary) option, which lets you enter your text in order to summarize it, and finally, as a bonus, we've integrated a chatbot model in the (Chatbot) option, which lets you try out our chatbot, which we're currently perfecting.")
elif selected == 'Translate':
# Choose the translation language
translated_model = {
"english - spanish": "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es",
"english - french": "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-fr",
"english - chinese": "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-zh",
"english - german": "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-de"
}
languages = st.selectbox("", list(translated_model.keys()))
API_TRANSLATED = translated_model[languages]
col3, col4 = st.columns(2)
text_in = col3.text_area("", height=200, placeholder='type or paste the text you wish to translate.')
#Boutton pour effectuer la traduction
btn_translate = st.button("π Translate")
if btn_translate:
headers_spanish = {"Authorization": API_TOKEN}
if languages == "english - spanish" and text_in:
def query(payload):
response = requests.post(API_TRANSLATED, headers=headers_spanish, json=payload)
return response.json()
output = query_spanish({"inputs": text_in})
if not output[0]["translation_text"]:
error_message = output[0]["error"]
st.error(f"Le texte n'a pas pu Γͺtre traduit: {error_message}")
else:
translated_text = output[0]["translation_text"]
col5.markdown('''
'''+ translated_text +'''
''', unsafe_allow_html=True) # text_out_spanish = col4.markdown(''''''+ output_spanish[0] ["translation_text"] +'''
''', unsafe_allow_html=True) st.info(":blue[Please feel fit to like it after use! ππ]") elif languages == "english - french" and text_in: API_URL_french = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-fr" headers_french = {"Authorization": API_TOKEN} if btn_translate: def query(payload): response = requests.post(API_TRANSLATED, headers=headers_french, json=payload) return response.json() output = query({"inputs": text_in}) if not output[0]["translation_text"]: error_message = output[0]["error"] st.error(f"Le texte n'a pas pu Γͺtre traduit: {error_message}") else: translated_text = output_translate[0]["translation_text"] col5.markdown(''''''+ translated_text +'''
''', unsafe_allow_html=True) # text_out_french = col4.markdown(''''''+ output_french[0] ["translation_text"] +'''
''', unsafe_allow_html=True) # st.info(":blue[Please feel fit to like it after use! ππ]") elif languages == 'english - chinese' and text_in: headers_chinese = {"Authorization": API_TOKEN} if btn_translate: def query(payload): response = requests.post(API_TRANSLATED, headers=headers_chinese, json=payload) return response.json() output = query({"inputs": text_in}) if not output[0]["translation_text"]: error_message = output[0]["error"] st.error(f"Le texte n'a pas pu Γͺtre traduit: {error_message}") else: translated_text = output[0]["translation_text"] col5.markdown(''''''+ translated_text +'''
''', unsafe_allow_html=True) # text_out_chinese = col4.markdown(''''''+ output_chinese[0] ["translation_text"] +'''
''', unsafe_allow_html=True) st.info(":blue[Please feel fit to like it after use! ππ]") elif languages == "english - german" and text_in: headers_german = {"Authorization": API_TOKEN} if btn_translate: def query(payload): response = requests.post(API_TRANSLATED, headers=headers_german, json=payload) return response.json() output = query({"inputs": text_in}) if not output[0]["translation_text"]: error_message = output[0]["error"] st.error(f"Le texte n'a pas pu Γͺtre traduit: {error_message}") else: translated_text = output[0]["translation_text"] col5.markdown(''''''+ translated_text +'''
''', unsafe_allow_html=True) # text_out_german = col4.markdown(''''''+ output_german[0] ["translation_text"] +'''
''', unsafe_allow_html=True) st.info(":blue[Please feel fit to like it after use! ππ]") elif selected == 'Summarization': text_sum = st.text_area("", height=250, placeholder='Please type or paste the text you wish to summarise here.') #Boutton pour effectuer la prΓ©diction btn_sum = st.button("π Summary") if btn_sum: if text_sum: API_URL_summary = "https://api-inference.huggingface.co/models/Falconsai/text_summarization" headers_summary = {"Authorization": API_TOKEN} def query(payload): response = requests.post(API_URL_summary, headers=headers_summary, json=payload) return response.json() st.markdown(''''''+ output_sum[0]["summary_text"] +'''
''', unsafe_allow_html=True) st.info(":blue[Please feel fit to like it after use! ππ]") elif selected == 'Chatbot': text_chat = st.text_input("Enter your question") #Boutton pour effectuer le chatbot btn_chat = st.button("π¬ Submit") API_URL_chatbot = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct" headers_chatbot = {"Authorization": API_TOKEN} if btn_chat: if text_chat: def query(payload): response = requests.post(API_URL_chatbot, headers=headers_chatbot, json=payload) return response.json() output = query({"inputs": text_chat,}) generated_text = output[0]["generated_text"] st.success(f"Response: {generated_text}") # text_out_chat = st.info(':green['+ output_chat[0]["generated_text"] +']') st.info(":blue[Please feel fit to like it after use! ππ]") st.markdown(''''''+ output[0]["translation_text"] +'''
''', unsafe_allow_html=True) # except KeyError: # print("Couldn't find a match for the key:", key)