File size: 9,369 Bytes
c6eb2ba
 
 
c628af6
c6eb2ba
c628af6
c6eb2ba
162d617
c6eb2ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1220cb1
2a4ec62
1220cb1
 
 
 
 
1a82716
2a4ec62
c6eb2ba
 
 
 
5c5cb0e
162d617
5c5cb0e
0ec8eb5
2a4ec62
c6eb2ba
0ec8eb5
 
a1f0802
 
 
 
 
ff8d8da
 
c6eb2ba
5c5cb0e
 
 
 
0ec8eb5
2a4ec62
5c5cb0e
0ec8eb5
2a4ec62
0ec8eb5
 
2a4ec62
 
 
 
 
 
 
5c5cb0e
 
 
0ec8eb5
2a4ec62
5c5cb0e
0ec8eb5
 
 
ff8d8da
 
0ec8eb5
ff8d8da
 
 
5c5cb0e
 
 
 
0ec8eb5
2a4ec62
5c5cb0e
0ec8eb5
 
 
ff8d8da
 
0ec8eb5
ff8d8da
 
 
5c5cb0e
c6eb2ba
 
 
 
 
5c5cb0e
 
162d617
 
0ec8eb5
162d617
c6eb2ba
 
0ec8eb5
 
838ec03
 
c6eb2ba
 
 
2a4ec62
c6eb2ba
 
162d617
 
5c5cb0e
ab9903f
0ec8eb5
162d617
c6eb2ba
0ec8eb5
 
6ee92ad
 
c6eb2ba
 
5c5cb0e
162d617
 
 
 
 
5c5cb0e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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('''<br/> <p style='text-align:left;font-size:16px;'>'''+ translated_text +'''</p>''',  unsafe_allow_html=True)
                
            # text_out_spanish = col4.markdown('''<br/> <p style='text-align:left;font-size:16px;'>'''+ output_spanish[0] ["translation_text"] +'''</p>''',  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('''<br/> <p style='text-align:left;font-size:16px;'>'''+ translated_text +'''</p>''',  unsafe_allow_html=True)
                
                # text_out_french = col4.markdown('''<br/> <p style='text-align:left;font-size:16px;'>'''+ output_french[0] ["translation_text"] +'''</p>''',  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('''<br/> <p style='text-align:left;font-size:16px;'>'''+ translated_text +'''</p>''',  unsafe_allow_html=True)
                
                # text_out_chinese = col4.markdown('''<br/> <p style='text-align:left;font-size:16px;'>'''+ output_chinese[0] ["translation_text"] +'''</p>''',  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('''<br/> <p style='text-align:left;font-size:16px;'>'''+ translated_text +'''</p>''',  unsafe_allow_html=True)
                
                # text_out_german = col4.markdown('''<br/> <p style='text-align:left;font-size:16px;'>'''+ output_german[0] ["translation_text"] +'''</p>''',  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('''<h4 style='text-align:left;color:black;'> Answer </h4>''',  unsafe_allow_html=True) 
            output = query({"inputs": text_sum,})
            summary_text = output[0]["summary_text"]
            st.success(f"Response: {summary_text}")
            # text_out_sum = st.markdown('''<p style='text-align:left;font-size:16px;color:orange;font-weight:bold'>'''+ output_sum[0]["summary_text"] +'''</p>''',  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('''<br/> <h6 style='text-align:center;color:grey;font-size:11px;'> ยฉ 2024 - TOUNDE ZOGO - All rights reserved </h6>''',  unsafe_allow_html=True)

# for key in range(5):
#     try:
#         text_out = col4.markdown('''<br/> <p style='text-align:left;font-size:16px;'>'''+ output[0]["translation_text"] +'''</p>''',  unsafe_allow_html=True)
#     except KeyError:
#         print("Couldn't find a match for the key:", key)