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lottie
|
| 4 |
+
import json
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import requests
|
| 7 |
+
import torch
|
| 8 |
+
import tensorflow as tf
|
| 9 |
+
import streamlit as st
|
| 10 |
+
from streamlit_option_menu import option_menu
|
| 11 |
+
|
| 12 |
+
logo = "https://www.google.com/url?sa=i&url=https%3A%2F%2Ffr.depositphotos.com%2Fvector-images%2Frobot-logo.html&psig=AOvVaw14rAtmwJQVSpRFXFY6us7z&ust=1647274982461000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCPjhzdO_w_YCFQAAAAAdAAAAABAD"
|
| 13 |
+
st.set_page_config(page_icon = logo, page_title ="Bonsoir !", layout = "wide")
|
| 14 |
+
|
| 15 |
+
@st.cache(allow_output_mutation=True)
|
| 16 |
+
def load_tokenizer():
|
| 17 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2-large")
|
| 18 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2-large", pad_token_id=tokenizer.eos_token_id)
|
| 19 |
+
return tokenizer
|
| 20 |
+
|
| 21 |
+
@st.cache(allow_output_mutation=True)
|
| 22 |
+
def load_model():
|
| 23 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2-large", pad_token_id=tokenizer.eos_token_id)
|
| 24 |
+
return model
|
| 25 |
+
|
| 26 |
+
tokenizer =load_tokenizer()
|
| 27 |
+
model = load_model()
|
| 28 |
+
|
| 29 |
+
def reponse(question, temp=0.5, long=40):
|
| 30 |
+
|
| 31 |
+
input_ids = tokenizer.encode(question, return_tensors='pt')
|
| 32 |
+
output = model.generate(input_ids, max_length=long, temperature =temp, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
|
| 33 |
+
rep = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 34 |
+
return rep
|
| 35 |
+
|
| 36 |
+
def load_animation(url: str):
|
| 37 |
+
r = requests.get(url)
|
| 38 |
+
if r.status_code != 200 :
|
| 39 |
+
return None
|
| 40 |
+
return r.json()
|
| 41 |
+
|
| 42 |
+
url = "https://assets10.lottiefiles.com/packages/lf20_96bovdur.json"
|
| 43 |
+
robot = load_animation(url)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def contact_message():
|
| 47 |
+
st.header(":mailbox: Let's Get In Touch !")
|
| 48 |
+
|
| 49 |
+
name, message = st.columns((1,2))
|
| 50 |
+
with name:
|
| 51 |
+
contact_form = """<form action="https://formsubmit.co/maxime.letutour@gmail.com" method="POST">
|
| 52 |
+
<input type="text" name="name" placeholder = "Ton Nom" required>
|
| 53 |
+
<input type="email" name="email" placeholder = "Ton E-mail" required>
|
| 54 |
+
</form>"""
|
| 55 |
+
st.markdown(contact_form, unsafe_allow_html=True)
|
| 56 |
+
|
| 57 |
+
with message :
|
| 58 |
+
contact_form2 = """<form action="https://formsubmit.co/maxime.letutour@gmail.com" method="POST">
|
| 59 |
+
<textarea name="message" placeholder="Ecris moi !"></textarea>
|
| 60 |
+
<button type="submit">Send</button>
|
| 61 |
+
"""
|
| 62 |
+
st.markdown(contact_form2, unsafe_allow_html=True)
|
| 63 |
+
|
| 64 |
+
with open("style2.txt") as f:
|
| 65 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def robot():
|
| 70 |
+
robot = load_animation(url)
|
| 71 |
+
col1, col2, col3 = st.columns((5,1,5))
|
| 72 |
+
|
| 73 |
+
with col1:
|
| 74 |
+
|
| 75 |
+
st.subheader("Choose the length of my answer")
|
| 76 |
+
long = st.number_input("Be aware that long answers require more time to think !", min_value=10, max_value=250, step =10)
|
| 77 |
+
|
| 78 |
+
st.subheader("Ask me something")
|
| 79 |
+
question = st.text_input('Be aware that I speak only english for the moment !',max_chars = 60)
|
| 80 |
+
question = str(question)
|
| 81 |
+
|
| 82 |
+
ok = st.button('Ask me')
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
with col3:
|
| 86 |
+
st_lottie(robot, speed=1, loop=True, quality = "low",height =300, width = 300)
|
| 87 |
+
if ok:
|
| 88 |
+
rep = reponse(question, long = long)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
rep_style = f'<p style="font-family:Lucida Handwriting; color:#00008B; font-size: 20px;">{rep}</p>'
|
| 92 |
+
st.markdown(rep_style, unsafe_allow_html=True)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def main():
|
| 99 |
+
st.title("Shall we chat ? Ask me a question")
|
| 100 |
+
|
| 101 |
+
with st.sidebar:
|
| 102 |
+
|
| 103 |
+
choice = option_menu(
|
| 104 |
+
menu_title = "Ask Me",
|
| 105 |
+
options = ["Question", "Envoie Moi Un Message"],
|
| 106 |
+
icons=["chat","envelope"],
|
| 107 |
+
menu_icon="robot"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if choice == "Envoie Moi Un Message":
|
| 111 |
+
contact_message()
|
| 112 |
+
|
| 113 |
+
elif choice == "Question":
|
| 114 |
+
robot()
|
| 115 |
+
|
| 116 |
+
st.sidebar.subheader(":notebook_with_decorative_cover: Par Maxime Le Tutour")
|
| 117 |
+
|
| 118 |
+
st.sidebar.write(" :blue_book: [**Mon LinkedIn**](https://share.streamlit.io/mesmith027/streamlit_webapps/main/MC_pi/streamlit_app.py)", unsafe_allow_html =True)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
print("ok")
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
if __name__ == '__main__':
|
| 150 |
+
main()
|