Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from langchain.chains import LLMChain, RetrievalQA
|
|
| 8 |
import gspread
|
| 9 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 10 |
import json
|
|
|
|
| 11 |
import gspread
|
| 12 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 13 |
import json
|
|
@@ -21,17 +22,16 @@ service_account_info = json.loads(GOOGLE_SERVICE_ACCOUNT_JSON)
|
|
| 21 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(service_account_info, scope)
|
| 22 |
client = gspread.authorize(creds)
|
| 23 |
sheet = client.open("users feedback").sheet1 # Replace with your Google Sheet name
|
| 24 |
-
|
| 25 |
-
# Function to save user feedback to Google Sheets
|
| 26 |
def save_feedback(user_input, bot_response, rating, comment):
|
| 27 |
feedback = [user_input, bot_response, rating, comment]
|
| 28 |
sheet.append_row(feedback)
|
| 29 |
|
| 30 |
-
# Hugging Face
|
| 31 |
from huggingface_hub import login
|
| 32 |
login(token=st.secrets["HF_TOKEN"])
|
| 33 |
|
| 34 |
-
#
|
| 35 |
db = FAISS.load_local("faiss_index", HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2'), allow_dangerous_deserialization=True)
|
| 36 |
retriever = db.as_retriever(search_type="mmr", search_kwargs={'k': 1})
|
| 37 |
|
|
@@ -57,16 +57,16 @@ mistral_llm = HuggingFaceEndpoint(
|
|
| 57 |
repo_id=repo_id, max_length=2048, temperature=0.05, huggingfacehub_api_token=st.secrets["HF_TOKEN"]
|
| 58 |
)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
prompt = PromptTemplate(
|
| 62 |
input_variables=["question"],
|
| 63 |
template=prompt_template,
|
| 64 |
)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
qa = RetrievalQA.from_chain_type(
|
| 71 |
llm=mistral_llm,
|
| 72 |
chain_type="stuff",
|
|
@@ -74,24 +74,24 @@ qa = RetrievalQA.from_chain_type(
|
|
| 74 |
chain_type_kwargs={"prompt": prompt},
|
| 75 |
)
|
| 76 |
|
| 77 |
-
# Streamlit
|
| 78 |
st.set_page_config(page_title="Alter-IA Chat", page_icon="🤖")
|
| 79 |
|
| 80 |
-
#
|
| 81 |
def chatbot_response(user_input):
|
| 82 |
response = qa.run(user_input)
|
| 83 |
return response
|
| 84 |
|
| 85 |
-
#
|
| 86 |
col1, col2, col3 = st.columns([2, 3, 2])
|
| 87 |
|
| 88 |
with col1:
|
| 89 |
-
st.image("Design 3_22.png", width=150, use_column_width=True) #
|
| 90 |
|
| 91 |
with col3:
|
| 92 |
-
st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True) #
|
| 93 |
|
| 94 |
-
# Streamlit
|
| 95 |
st.markdown("""
|
| 96 |
<style>
|
| 97 |
.centered-text {
|
|
@@ -104,36 +104,36 @@ st.markdown("""
|
|
| 104 |
</style>
|
| 105 |
""", unsafe_allow_html=True)
|
| 106 |
|
| 107 |
-
#
|
| 108 |
st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖</h3>', unsafe_allow_html=True)
|
| 109 |
st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique"</p>', unsafe_allow_html=True)
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
if submit_button:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
#
|
| 138 |
st.markdown("---")
|
| 139 |
st.markdown("La collaboration est la clé du succès. Chaque question trouve sa réponse, chaque défi devient une opportunité.")
|
|
|
|
| 8 |
import gspread
|
| 9 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 10 |
import json
|
| 11 |
+
|
| 12 |
import gspread
|
| 13 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 14 |
import json
|
|
|
|
| 22 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(service_account_info, scope)
|
| 23 |
client = gspread.authorize(creds)
|
| 24 |
sheet = client.open("users feedback").sheet1 # Replace with your Google Sheet name
|
| 25 |
+
# Fonction pour enregistrer les retours utilisateur dans Google Sheets
|
|
|
|
| 26 |
def save_feedback(user_input, bot_response, rating, comment):
|
| 27 |
feedback = [user_input, bot_response, rating, comment]
|
| 28 |
sheet.append_row(feedback)
|
| 29 |
|
| 30 |
+
# Connexion API Hugging Face
|
| 31 |
from huggingface_hub import login
|
| 32 |
login(token=st.secrets["HF_TOKEN"])
|
| 33 |
|
| 34 |
+
# Initialiser les composants LangChain
|
| 35 |
db = FAISS.load_local("faiss_index", HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2'), allow_dangerous_deserialization=True)
|
| 36 |
retriever = db.as_retriever(search_type="mmr", search_kwargs={'k': 1})
|
| 37 |
|
|
|
|
| 57 |
repo_id=repo_id, max_length=2048, temperature=0.05, huggingfacehub_api_token=st.secrets["HF_TOKEN"]
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# Créer le prompt à partir du modèle de prompt
|
| 61 |
prompt = PromptTemplate(
|
| 62 |
input_variables=["question"],
|
| 63 |
template=prompt_template,
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Créer la chaîne LLM
|
| 67 |
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)
|
| 68 |
|
| 69 |
+
# Créer la chaîne RetrievalQA
|
| 70 |
qa = RetrievalQA.from_chain_type(
|
| 71 |
llm=mistral_llm,
|
| 72 |
chain_type="stuff",
|
|
|
|
| 74 |
chain_type_kwargs={"prompt": prompt},
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# Interface Streamlit avec une esthétique améliorée
|
| 78 |
st.set_page_config(page_title="Alter-IA Chat", page_icon="🤖")
|
| 79 |
|
| 80 |
+
# Définir la fonction pour gérer l'entrée utilisateur et afficher la réponse du chatbot
|
| 81 |
def chatbot_response(user_input):
|
| 82 |
response = qa.run(user_input)
|
| 83 |
return response
|
| 84 |
|
| 85 |
+
# Créer des colonnes pour les logos
|
| 86 |
col1, col2, col3 = st.columns([2, 3, 2])
|
| 87 |
|
| 88 |
with col1:
|
| 89 |
+
st.image("Design 3_22.png", width=150, use_column_width=True) # Ajustez le chemin et la taille de l'image selon vos besoins
|
| 90 |
|
| 91 |
with col3:
|
| 92 |
+
st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True) # Ajustez le chemin et la taille de l'image selon vos besoins
|
| 93 |
|
| 94 |
+
# Composants Streamlit
|
| 95 |
st.markdown("""
|
| 96 |
<style>
|
| 97 |
.centered-text {
|
|
|
|
| 104 |
</style>
|
| 105 |
""", unsafe_allow_html=True)
|
| 106 |
|
| 107 |
+
# Utiliser les classes CSS pour styliser le texte
|
| 108 |
st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖</h3>', unsafe_allow_html=True)
|
| 109 |
st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique"</p>', unsafe_allow_html=True)
|
| 110 |
|
| 111 |
+
# Interface utilisateur avec formulaire
|
| 112 |
+
with st.form(key='feedback_form'):
|
| 113 |
+
user_input = st.text_input("You:")
|
| 114 |
+
submit_button = st.form_submit_button("Ask 📨")
|
| 115 |
+
|
| 116 |
+
if submit_button:
|
| 117 |
+
if user_input.strip() != "":
|
| 118 |
+
bot_response = chatbot_response(user_input)
|
| 119 |
+
st.markdown("### Bot:")
|
| 120 |
+
st.text_area("", value=bot_response, height=600)
|
| 121 |
+
|
| 122 |
+
# Formulaire de retour d'information
|
| 123 |
+
st.markdown("### Rate the response:")
|
| 124 |
+
rating = st.slider("Select a rating:", min_value=1, max_value=5, value=1)
|
| 125 |
+
|
| 126 |
+
st.markdown("### Leave a comment:")
|
| 127 |
+
comment = st.text_area("")
|
| 128 |
+
|
| 129 |
+
# Soumettre les retours d'information
|
| 130 |
+
if st.form_submit_button("Submit Feedback"):
|
| 131 |
+
if comment.strip() and rating:
|
| 132 |
+
save_feedback(user_input, bot_response, rating, comment)
|
| 133 |
+
st.success("Thank you for your feedback!")
|
| 134 |
+
else:
|
| 135 |
+
st.warning("⚠️ Please provide a comment and a rating.")
|
| 136 |
+
|
| 137 |
+
# Citation motivante en bas de page
|
| 138 |
st.markdown("---")
|
| 139 |
st.markdown("La collaboration est la clé du succès. Chaque question trouve sa réponse, chaque défi devient une opportunité.")
|