|
import os |
|
import streamlit as st |
|
from langchain_community.vectorstores import FAISS |
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
import gspread |
|
from oauth2client.service_account import ServiceAccountCredentials |
|
import json |
|
from langchain_huggingface import HuggingFaceEndpoint |
|
|
|
from langchain.prompts import PromptTemplate |
|
from langchain.schema.runnable import RunnablePassthrough |
|
from langchain.chains import LLMChain |
|
|
|
GOOGLE_SERVICE_ACCOUNT_JSON = st.secrets["GOOGLE_SERVICE_ACCOUNT_JSON"] |
|
|
|
|
|
scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"] |
|
service_account_info = json.loads(GOOGLE_SERVICE_ACCOUNT_JSON) |
|
creds = ServiceAccountCredentials.from_json_keyfile_dict(service_account_info, scope) |
|
client = gspread.authorize(creds) |
|
spreadsheet_id = '1Jf1k7Q71ihsxBf-XQYyucamMy14q7IjhUDlU8ZzR_Nc' |
|
sheet = client.open_by_key(spreadsheet_id).sheet1 |
|
|
|
|
|
def save_feedback(user_input, bot_response, rating, comment): |
|
feedback = [user_input, bot_response, rating, comment] |
|
sheet.append_row(feedback) |
|
|
|
from huggingface_hub import login |
|
login(token=st.secrets["HF_TOKEN"]) |
|
|
|
from langchain_community.document_loaders import TextLoader |
|
from langchain_text_splitters import CharacterTextSplitter |
|
from langchain_community.document_loaders import PyPDFLoader |
|
from langchain.chains import RetrievalQA |
|
from langchain.prompts import PromptTemplate |
|
from langchain.embeddings.huggingface import HuggingFaceEmbeddings |
|
|
|
db = FAISS.load_local("faiss_index", HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2'),allow_dangerous_deserialization=True) |
|
|
|
|
|
|
|
retriever = db.as_retriever( |
|
search_type="similarity", |
|
search_kwargs={'k': 2} |
|
) |
|
|
|
|
|
prompt_template = """ |
|
### [INST] |
|
Instruction: You are a Q&A assistant. Your goal is to answer questions as accurately as possible based on the instructions and context provided without using prior knowledge.You answer in FRENCH |
|
Analyse carefully the context and provide a direct answer based on the context. If the user said Bonjour or Hello your only answer will be Hi! comment puis-je vous aider? |
|
Answer in french only |
|
|
|
{context} |
|
Vous devez répondre aux questions en français. |
|
|
|
### QUESTION: |
|
{question} |
|
[/INST] |
|
Answer in french only |
|
Vous devez répondre aux questions en français. please |
|
|
|
""" |
|
|
|
repo_id = "mistralai/Mistral-7B-Instruct-v0.3" |
|
|
|
mistral_llm = HuggingFaceEndpoint( |
|
repo_id=repo_id, max_length=2048, temperature=0.02, huggingfacehub_api_token=st.secrets["HF_TOKEN"] |
|
) |
|
|
|
|
|
prompt = PromptTemplate( |
|
input_variables=["question"], |
|
template=prompt_template, |
|
) |
|
|
|
|
|
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt) |
|
|
|
|
|
retriever.search_kwargs = {'k':4} |
|
qa = RetrievalQA.from_chain_type( |
|
llm=mistral_llm, |
|
chain_type="stuff", |
|
retriever=retriever, |
|
chain_type_kwargs={"prompt": prompt}, |
|
) |
|
|
|
import streamlit as st |
|
|
|
|
|
st.set_page_config(page_title="Alter-IA Chat", page_icon="🤖") |
|
|
|
|
|
def chatbot_response(user_input): |
|
response = qa.run(user_input) |
|
return response |
|
|
|
|
|
if 'user_input' not in st.session_state: |
|
st.session_state.user_input = "" |
|
if 'bot_response' not in st.session_state: |
|
st.session_state.bot_response = "" |
|
|
|
|
|
col1, col2, col3 = st.columns([2, 3, 2]) |
|
|
|
with col1: |
|
st.image("Design 3_22.png", width=150, use_column_width=True) |
|
|
|
with col3: |
|
st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True) |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.centered-text { |
|
text-align: center; |
|
} |
|
.centered-orange-text { |
|
text-align: center; |
|
color: darkorange; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖 </h3>', unsafe_allow_html=True) |
|
st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique "</p>', unsafe_allow_html=True) |
|
|
|
|
|
with st.form(key='interaction_form'): |
|
st.session_state.user_input = st.text_input("You:", key="user_input_input") |
|
ask_button = st.form_submit_button("Ask 📨") |
|
|
|
if ask_button and st.session_state.user_input.strip(): |
|
st.session_state.bot_response = chatbot_response(st.session_state.user_input) |
|
|
|
|
|
if st.session_state.bot_response: |
|
st.markdown("### Bot:") |
|
st.text_area("", value=st.session_state.bot_response, height=600) |
|
|
|
|
|
with st.form(key='feedback_form'): |
|
st.markdown("### Évaluez la réponse :") |
|
rating = st.slider("Select a rating:", min_value=1, max_value=5, value=1, key="rating") |
|
|
|
st.markdown("##### Laissez un commentaire ici:") |
|
comment = st.text_area("", key="comment") |
|
|
|
|
|
feedback_submit_button = st.form_submit_button("Soumettre ton Feedback") |
|
|
|
if feedback_submit_button: |
|
if comment.strip(): |
|
save_feedback(st.session_state.user_input, st.session_state.bot_response, rating, comment) |
|
st.success("Merci pour votre feedback!") |
|
|
|
st.session_state.user_input = "" |
|
st.session_state.bot_response = "" |
|
else: |
|
st.warning("Veuillez fournir un commentaire avant de soumettre votre retour.") |
|
|
|
st.markdown("---") |
|
st.markdown("La collaboration est la clé du succès. Chaque question trouve sa réponse, chaque défi devient une opportunité.") |
|
|