Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,31 +9,27 @@ import gspread
|
|
9 |
from oauth2client.service_account import ServiceAccountCredentials
|
10 |
import json
|
11 |
|
12 |
-
#
|
13 |
GOOGLE_SERVICE_ACCOUNT_JSON = st.secrets["GOOGLE_SERVICE_ACCOUNT_JSON"]
|
14 |
|
15 |
-
#
|
16 |
scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
|
17 |
service_account_info = json.loads(GOOGLE_SERVICE_ACCOUNT_JSON)
|
18 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(service_account_info, scope)
|
19 |
client = gspread.authorize(creds)
|
20 |
-
|
21 |
-
sheet = client.open_by_key(
|
22 |
|
23 |
-
#
|
24 |
def save_feedback(user_input, bot_response, rating, comment):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
except Exception as e:
|
30 |
-
st.error(f"Erreur lors de l'enregistrement des retours : {e}")
|
31 |
-
|
32 |
-
# Connexion API Hugging Face
|
33 |
from huggingface_hub import login
|
34 |
login(token=st.secrets["HF_TOKEN"])
|
35 |
|
36 |
-
#
|
37 |
db = FAISS.load_local("faiss_index", HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2'), allow_dangerous_deserialization=True)
|
38 |
retriever = db.as_retriever(search_type="mmr", search_kwargs={'k': 1})
|
39 |
|
@@ -58,16 +54,16 @@ mistral_llm = HuggingFaceEndpoint(
|
|
58 |
repo_id=repo_id, max_length=2048, temperature=0.05, huggingfacehub_api_token=st.secrets["HF_TOKEN"]
|
59 |
)
|
60 |
|
61 |
-
#
|
62 |
prompt = PromptTemplate(
|
63 |
input_variables=["question"],
|
64 |
template=prompt_template,
|
65 |
)
|
66 |
|
67 |
-
#
|
68 |
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)
|
69 |
|
70 |
-
#
|
71 |
qa = RetrievalQA.from_chain_type(
|
72 |
llm=mistral_llm,
|
73 |
chain_type="stuff",
|
@@ -75,24 +71,24 @@ qa = RetrievalQA.from_chain_type(
|
|
75 |
chain_type_kwargs={"prompt": prompt},
|
76 |
)
|
77 |
|
78 |
-
#
|
79 |
st.set_page_config(page_title="Alter-IA Chat", page_icon="🤖")
|
80 |
|
81 |
-
#
|
82 |
def chatbot_response(user_input):
|
83 |
response = qa.run(user_input)
|
84 |
return response
|
85 |
|
86 |
-
#
|
87 |
col1, col2, col3 = st.columns([2, 3, 2])
|
88 |
|
89 |
with col1:
|
90 |
-
st.image("Design 3_22.png", width=150, use_column_width=True) #
|
91 |
|
92 |
with col3:
|
93 |
-
st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True) #
|
94 |
|
95 |
-
#
|
96 |
st.markdown("""
|
97 |
<style>
|
98 |
.centered-text {
|
@@ -105,11 +101,10 @@ st.markdown("""
|
|
105 |
</style>
|
106 |
""", unsafe_allow_html=True)
|
107 |
|
108 |
-
# Utiliser les classes CSS pour styliser le texte
|
109 |
st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖</h3>', unsafe_allow_html=True)
|
110 |
st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique"</p>', unsafe_allow_html=True)
|
111 |
|
112 |
-
#
|
113 |
with st.form(key='feedback_form'):
|
114 |
user_input = st.text_input("You:")
|
115 |
submit_button = st.form_submit_button("Ask 📨")
|
@@ -120,20 +115,19 @@ with st.form(key='feedback_form'):
|
|
120 |
st.markdown("### Bot:")
|
121 |
st.text_area("", value=bot_response, height=600)
|
122 |
|
123 |
-
#
|
124 |
st.markdown("### Rate the response:")
|
125 |
rating = st.slider("Select a rating:", min_value=1, max_value=5, value=1)
|
126 |
|
127 |
st.markdown("### Leave a comment:")
|
128 |
comment = st.text_area("")
|
129 |
|
130 |
-
#
|
131 |
-
|
|
|
|
|
132 |
if comment.strip() and rating:
|
133 |
save_feedback(user_input, bot_response, rating, comment)
|
|
|
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é.")
|
|
|
9 |
from oauth2client.service_account import ServiceAccountCredentials
|
10 |
import json
|
11 |
|
12 |
+
# Load Google service account credentials from Hugging Face secrets
|
13 |
GOOGLE_SERVICE_ACCOUNT_JSON = st.secrets["GOOGLE_SERVICE_ACCOUNT_JSON"]
|
14 |
|
15 |
+
# Google Sheets setup
|
16 |
scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
|
17 |
service_account_info = json.loads(GOOGLE_SERVICE_ACCOUNT_JSON)
|
18 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(service_account_info, scope)
|
19 |
client = gspread.authorize(creds)
|
20 |
+
sheet_id = "1Jf1k7Q71ihsxBf-XQYyucamMy14q7IjhUDlU8ZzR_Nc" # Replace with your Sheet ID
|
21 |
+
sheet = client.open_by_key(sheet_id).sheet1 # Use the sheet ID to open the sheet
|
22 |
|
23 |
+
# Function to save feedback to Google Sheets
|
24 |
def save_feedback(user_input, bot_response, rating, comment):
|
25 |
+
feedback = [user_input, bot_response, rating, comment]
|
26 |
+
sheet.append_row(feedback)
|
27 |
+
|
28 |
+
# Connect to Hugging Face API
|
|
|
|
|
|
|
|
|
29 |
from huggingface_hub import login
|
30 |
login(token=st.secrets["HF_TOKEN"])
|
31 |
|
32 |
+
# Initialize LangChain components
|
33 |
db = FAISS.load_local("faiss_index", HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2'), allow_dangerous_deserialization=True)
|
34 |
retriever = db.as_retriever(search_type="mmr", search_kwargs={'k': 1})
|
35 |
|
|
|
54 |
repo_id=repo_id, max_length=2048, temperature=0.05, huggingfacehub_api_token=st.secrets["HF_TOKEN"]
|
55 |
)
|
56 |
|
57 |
+
# Create prompt from prompt template
|
58 |
prompt = PromptTemplate(
|
59 |
input_variables=["question"],
|
60 |
template=prompt_template,
|
61 |
)
|
62 |
|
63 |
+
# Create LLM chain
|
64 |
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)
|
65 |
|
66 |
+
# Create RetrievalQA chain
|
67 |
qa = RetrievalQA.from_chain_type(
|
68 |
llm=mistral_llm,
|
69 |
chain_type="stuff",
|
|
|
71 |
chain_type_kwargs={"prompt": prompt},
|
72 |
)
|
73 |
|
74 |
+
# Streamlit interface setup
|
75 |
st.set_page_config(page_title="Alter-IA Chat", page_icon="🤖")
|
76 |
|
77 |
+
# Function to handle user input and display chatbot response
|
78 |
def chatbot_response(user_input):
|
79 |
response = qa.run(user_input)
|
80 |
return response
|
81 |
|
82 |
+
# Create columns for logos
|
83 |
col1, col2, col3 = st.columns([2, 3, 2])
|
84 |
|
85 |
with col1:
|
86 |
+
st.image("Design 3_22.png", width=150, use_column_width=True) # Adjust image path and size as needed
|
87 |
|
88 |
with col3:
|
89 |
+
st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True) # Adjust image path and size as needed
|
90 |
|
91 |
+
# Streamlit components
|
92 |
st.markdown("""
|
93 |
<style>
|
94 |
.centered-text {
|
|
|
101 |
</style>
|
102 |
""", unsafe_allow_html=True)
|
103 |
|
|
|
104 |
st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖</h3>', unsafe_allow_html=True)
|
105 |
st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique"</p>', unsafe_allow_html=True)
|
106 |
|
107 |
+
# Streamlit form for user interaction
|
108 |
with st.form(key='feedback_form'):
|
109 |
user_input = st.text_input("You:")
|
110 |
submit_button = st.form_submit_button("Ask 📨")
|
|
|
115 |
st.markdown("### Bot:")
|
116 |
st.text_area("", value=bot_response, height=600)
|
117 |
|
118 |
+
# Form for feedback
|
119 |
st.markdown("### Rate the response:")
|
120 |
rating = st.slider("Select a rating:", min_value=1, max_value=5, value=1)
|
121 |
|
122 |
st.markdown("### Leave a comment:")
|
123 |
comment = st.text_area("")
|
124 |
|
125 |
+
# Submit feedback
|
126 |
+
feedback_submit_button = st.form_submit_button("Submit Feedback")
|
127 |
+
|
128 |
+
if feedback_submit_button:
|
129 |
if comment.strip() and rating:
|
130 |
save_feedback(user_input, bot_response, rating, comment)
|
131 |
+
st.success("Thank you for your feedback!")
|
132 |
else:
|
133 |
st.warning("⚠️ Please provide a comment and a rating.")
|
|
|
|
|
|
|
|