Spaces:
Runtime error
Runtime error
gigachat update
Browse files- app.py +36 -5
- notebooks/embeddings.ipynb +170 -0
app.py
CHANGED
|
@@ -1,15 +1,33 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import streamlit as st
|
| 3 |
import random
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
if 'button_clicked' not in st.session_state:
|
| 6 |
st.session_state.button_clicked = False
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def show_serial(number, score=None):
|
| 10 |
with st.container(border=True):
|
| 11 |
col1, col2, col3 = st.columns(3)
|
| 12 |
-
|
| 13 |
with col1:
|
| 14 |
st.image(data.iloc[number, 1])
|
| 15 |
with col2:
|
|
@@ -20,13 +38,26 @@ def show_serial(number, score=None):
|
|
| 20 |
st.write(f'{score[1]} metric: {score[0]:.4f}')
|
| 21 |
st.markdown(f'[Ссылка]({data.iloc[number, 0]})')
|
| 22 |
with col3:
|
| 23 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
st.title('Рекомендатор сериалов')
|
| 27 |
st.divider()
|
| 28 |
|
| 29 |
-
data =
|
| 30 |
|
| 31 |
cols = st.columns(4)
|
| 32 |
with cols[0]:
|
|
@@ -49,6 +80,6 @@ if st.button('Дай 10 случайных сериалов', use_container_widt
|
|
| 49 |
st.session_state.button_clicked = True
|
| 50 |
|
| 51 |
if st.session_state.button_clicked:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
show_serial(number)
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import streamlit as st
|
| 3 |
import random
|
| 4 |
+
from langchain.chat_models.gigachat import GigaChat
|
| 5 |
+
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
|
| 6 |
+
|
| 7 |
|
| 8 |
if 'button_clicked' not in st.session_state:
|
| 9 |
st.session_state.button_clicked = False
|
| 10 |
|
| 11 |
|
| 12 |
+
@st.cache_data
|
| 13 |
+
def load_data():
|
| 14 |
+
return pd.read_csv('data/data.csv', index_col=0)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_chat():
|
| 19 |
+
chat = GigaChat(
|
| 20 |
+
credentials='MjExZTNmMTUtMGFiZi00OWU5LWExODEtNTRmMWViODIzZTFlOjFjMWM0YjM5LTAwZjItNGUyMi1hZjE1LTQzZTAxMjZjZGYyYg==',
|
| 21 |
+
verify_ssl_certs=False)
|
| 22 |
+
return chat
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
chat = load_chat()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
def show_serial(number, score=None):
|
| 29 |
with st.container(border=True):
|
| 30 |
col1, col2, col3 = st.columns(3)
|
|
|
|
| 31 |
with col1:
|
| 32 |
st.image(data.iloc[number, 1])
|
| 33 |
with col2:
|
|
|
|
| 38 |
st.write(f'{score[1]} metric: {score[0]:.4f}')
|
| 39 |
st.markdown(f'[Ссылка]({data.iloc[number, 0]})')
|
| 40 |
with col3:
|
| 41 |
+
tab1, tab2 = st.tabs(["Аннотация", "Описание от бота"])
|
| 42 |
+
with tab1:
|
| 43 |
+
st.text_area(label='Аннотация', value=data.iloc[number, 4],
|
| 44 |
+
height=250, disabled=True, label_visibility='hidden')
|
| 45 |
+
with tab2:
|
| 46 |
+
setting = "ты умеешь кратко в несколько предложений описывать содержание книги по ее названию"
|
| 47 |
+
system_message_prompt = SystemMessagePromptTemplate.from_template(setting)
|
| 48 |
+
human_template = "Кратко опиши cюжет сериала под названием: {title}"
|
| 49 |
+
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
| 50 |
+
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
|
| 51 |
+
formatted_prompt = chat_prompt.format_prompt(title=data.iloc[number, 2])
|
| 52 |
+
response = chat(formatted_prompt.to_messages())
|
| 53 |
+
st.text_area(label='Чат', value=response.content,
|
| 54 |
+
height=250, disabled=True, label_visibility='hidden')
|
| 55 |
|
| 56 |
|
| 57 |
st.title('Рекомендатор сериалов')
|
| 58 |
st.divider()
|
| 59 |
|
| 60 |
+
data = load_data()
|
| 61 |
|
| 62 |
cols = st.columns(4)
|
| 63 |
with cols[0]:
|
|
|
|
| 80 |
st.session_state.button_clicked = True
|
| 81 |
|
| 82 |
if st.session_state.button_clicked:
|
| 83 |
+
indices = random.sample(range(data.shape[0]), 10)
|
| 84 |
+
for number in indices:
|
| 85 |
show_serial(number)
|
notebooks/embeddings.ipynb
CHANGED
|
@@ -136,6 +136,176 @@
|
|
| 136 |
"id": "cf07f22fb88110ed",
|
| 137 |
"outputs": [],
|
| 138 |
"execution_count": 8
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
],
|
| 141 |
"metadata": {
|
|
|
|
| 136 |
"id": "cf07f22fb88110ed",
|
| 137 |
"outputs": [],
|
| 138 |
"execution_count": 8
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"metadata": {
|
| 142 |
+
"ExecuteTime": {
|
| 143 |
+
"end_time": "2024-08-15T19:32:48.089368Z",
|
| 144 |
+
"start_time": "2024-08-15T19:32:47.803520Z"
|
| 145 |
+
}
|
| 146 |
+
},
|
| 147 |
+
"cell_type": "code",
|
| 148 |
+
"source": [
|
| 149 |
+
"from langchain.schema import HumanMessage, SystemMessage\n",
|
| 150 |
+
"from langchain_community.chat_models.gigachat import GigaChat"
|
| 151 |
+
],
|
| 152 |
+
"id": "2702e7fc3039396f",
|
| 153 |
+
"outputs": [],
|
| 154 |
+
"execution_count": 1
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"metadata": {
|
| 158 |
+
"ExecuteTime": {
|
| 159 |
+
"end_time": "2024-08-15T19:35:09.946960Z",
|
| 160 |
+
"start_time": "2024-08-15T19:35:09.837Z"
|
| 161 |
+
}
|
| 162 |
+
},
|
| 163 |
+
"cell_type": "code",
|
| 164 |
+
"source": "chat = GigaChat(credentials='MjExZTNmMTUtMGFiZi00OWU5LWExODEtNTRmMWViODIzZTFlOjFjMWM0YjM5LTAwZjItNGUyMi1hZjE1LTQzZTAxMjZjZGYyYg==', verify_ssl_certs=False)",
|
| 165 |
+
"id": "da55b67751c1ae48",
|
| 166 |
+
"outputs": [],
|
| 167 |
+
"execution_count": 2
|
| 168 |
+
},
|
| 169 |
+
{
|
| 170 |
+
"metadata": {
|
| 171 |
+
"ExecuteTime": {
|
| 172 |
+
"end_time": "2024-08-15T19:36:04.668354Z",
|
| 173 |
+
"start_time": "2024-08-15T19:35:32.837378Z"
|
| 174 |
+
}
|
| 175 |
+
},
|
| 176 |
+
"cell_type": "code",
|
| 177 |
+
"source": [
|
| 178 |
+
"messages = [\n",
|
| 179 |
+
" SystemMessage(\n",
|
| 180 |
+
" content=\"Ты - знаток сериалов, который без проблем может кратко описать любой сюжет\"\n",
|
| 181 |
+
" )\n",
|
| 182 |
+
"]\n",
|
| 183 |
+
"\n",
|
| 184 |
+
"# while(True):\n",
|
| 185 |
+
"# user_input = input(\"User: \")\n",
|
| 186 |
+
"# messages.append(HumanMessage(content=user_input))\n",
|
| 187 |
+
"# res = chat.invoke(messages)\n",
|
| 188 |
+
"# messages.append(res)\n",
|
| 189 |
+
"# print(\"Bot: \", res.content)"
|
| 190 |
+
],
|
| 191 |
+
"id": "fbabc0f3fec5efb3",
|
| 192 |
+
"outputs": [
|
| 193 |
+
{
|
| 194 |
+
"name": "stdout",
|
| 195 |
+
"output_type": "stream",
|
| 196 |
+
"text": [
|
| 197 |
+
"Bot: Всё просто замечательно! А у тебя как дела?\n",
|
| 198 |
+
"Bot: Да, я твой брат.\n",
|
| 199 |
+
"Bot: Что случилось?\n",
|
| 200 |
+
"Bot: Я здесь, чтобы помочь тебе.\n",
|
| 201 |
+
"Bot: Расскажи мне, что тебя беспокоит.\n",
|
| 202 |
+
"Bot: Хорошо.\n"
|
| 203 |
+
]
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"name": "stderr",
|
| 207 |
+
"output_type": "stream",
|
| 208 |
+
"text": [
|
| 209 |
+
"\n",
|
| 210 |
+
"KeyboardInterrupt\n",
|
| 211 |
+
"\n"
|
| 212 |
+
]
|
| 213 |
+
}
|
| 214 |
+
],
|
| 215 |
+
"execution_count": 3
|
| 216 |
+
},
|
| 217 |
+
{
|
| 218 |
+
"metadata": {
|
| 219 |
+
"ExecuteTime": {
|
| 220 |
+
"end_time": "2024-08-15T19:38:56.230640Z",
|
| 221 |
+
"start_time": "2024-08-15T19:38:56.210747Z"
|
| 222 |
+
}
|
| 223 |
+
},
|
| 224 |
+
"cell_type": "code",
|
| 225 |
+
"source": [
|
| 226 |
+
"user_input = HumanMessage(content=\"привет! расскажи про сериал Во все тяжкие\")\n",
|
| 227 |
+
"chat.invoke(user_input)"
|
| 228 |
+
],
|
| 229 |
+
"id": "db1a3aeb008cfb2b",
|
| 230 |
+
"outputs": [
|
| 231 |
+
{
|
| 232 |
+
"ename": "ValueError",
|
| 233 |
+
"evalue": "Invalid input type <class 'langchain_core.messages.human.HumanMessage'>. Must be a PromptValue, str, or list of BaseMessages.",
|
| 234 |
+
"output_type": "error",
|
| 235 |
+
"traceback": [
|
| 236 |
+
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
|
| 237 |
+
"\u001B[0;31mValueError\u001B[0m Traceback (most recent call last)",
|
| 238 |
+
"Cell \u001B[0;32mIn[6], line 2\u001B[0m\n\u001B[1;32m 1\u001B[0m user_input \u001B[38;5;241m=\u001B[39m HumanMessage(content\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mпривет! расскажи про сериал Во все тяжкие\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m----> 2\u001B[0m chat\u001B[38;5;241m.\u001B[39minvoke(user_input)\n",
|
| 239 |
+
"File \u001B[0;32m~/anaconda3/envs/torch_env/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py:292\u001B[0m, in \u001B[0;36mBaseChatModel.invoke\u001B[0;34m(self, input, config, stop, **kwargs)\u001B[0m\n\u001B[1;32m 280\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21minvoke\u001B[39m(\n\u001B[1;32m 281\u001B[0m \u001B[38;5;28mself\u001B[39m,\n\u001B[1;32m 282\u001B[0m \u001B[38;5;28minput\u001B[39m: LanguageModelInput,\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 286\u001B[0m \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs: Any,\n\u001B[1;32m 287\u001B[0m ) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m BaseMessage:\n\u001B[1;32m 288\u001B[0m config \u001B[38;5;241m=\u001B[39m ensure_config(config)\n\u001B[1;32m 289\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m cast(\n\u001B[1;32m 290\u001B[0m ChatGeneration,\n\u001B[1;32m 291\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mgenerate_prompt(\n\u001B[0;32m--> 292\u001B[0m [\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_convert_input(\u001B[38;5;28minput\u001B[39m)],\n\u001B[1;32m 293\u001B[0m stop\u001B[38;5;241m=\u001B[39mstop,\n\u001B[1;32m 294\u001B[0m callbacks\u001B[38;5;241m=\u001B[39mconfig\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcallbacks\u001B[39m\u001B[38;5;124m\"\u001B[39m),\n\u001B[1;32m 295\u001B[0m tags\u001B[38;5;241m=\u001B[39mconfig\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtags\u001B[39m\u001B[38;5;124m\"\u001B[39m),\n\u001B[1;32m 296\u001B[0m metadata\u001B[38;5;241m=\u001B[39mconfig\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmetadata\u001B[39m\u001B[38;5;124m\"\u001B[39m),\n\u001B[1;32m 297\u001B[0m run_name\u001B[38;5;241m=\u001B[39mconfig\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrun_name\u001B[39m\u001B[38;5;124m\"\u001B[39m),\n\u001B[1;32m 298\u001B[0m run_id\u001B[38;5;241m=\u001B[39mconfig\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrun_id\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m),\n\u001B[1;32m 299\u001B[0m \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs,\n\u001B[1;32m 300\u001B[0m )\u001B[38;5;241m.\u001B[39mgenerations[\u001B[38;5;241m0\u001B[39m][\u001B[38;5;241m0\u001B[39m],\n\u001B[1;32m 301\u001B[0m )\u001B[38;5;241m.\u001B[39mmessage\n",
|
| 240 |
+
"File \u001B[0;32m~/anaconda3/envs/torch_env/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py:275\u001B[0m, in \u001B[0;36mBaseChatModel._convert_input\u001B[0;34m(self, input)\u001B[0m\n\u001B[1;32m 273\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m ChatPromptValue(messages\u001B[38;5;241m=\u001B[39mconvert_to_messages(\u001B[38;5;28minput\u001B[39m))\n\u001B[1;32m 274\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m--> 275\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[1;32m 276\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mInvalid input type \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mtype\u001B[39m(\u001B[38;5;28minput\u001B[39m)\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m. \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m 277\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mMust be a PromptValue, str, or list of BaseMessages.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m 278\u001B[0m )\n",
|
| 241 |
+
"\u001B[0;31mValueError\u001B[0m: Invalid input type <class 'langchain_core.messages.human.HumanMessage'>. Must be a PromptValue, str, or list of BaseMessages."
|
| 242 |
+
]
|
| 243 |
+
}
|
| 244 |
+
],
|
| 245 |
+
"execution_count": 6
|
| 246 |
+
},
|
| 247 |
+
{
|
| 248 |
+
"metadata": {
|
| 249 |
+
"ExecuteTime": {
|
| 250 |
+
"end_time": "2024-08-15T19:43:44.568611Z",
|
| 251 |
+
"start_time": "2024-08-15T19:43:44.565821Z"
|
| 252 |
+
}
|
| 253 |
+
},
|
| 254 |
+
"cell_type": "code",
|
| 255 |
+
"source": "user_input = HumanMessage(content=\"Опиши мне сюжет сериала Во все тяжкие без спойлеров. Длина ответа - примерно 200 слов\")",
|
| 256 |
+
"id": "62a4e7b95a72d5ab",
|
| 257 |
+
"outputs": [],
|
| 258 |
+
"execution_count": 20
|
| 259 |
+
},
|
| 260 |
+
{
|
| 261 |
+
"metadata": {
|
| 262 |
+
"ExecuteTime": {
|
| 263 |
+
"end_time": "2024-08-15T19:43:45.044277Z",
|
| 264 |
+
"start_time": "2024-08-15T19:43:45.042388Z"
|
| 265 |
+
}
|
| 266 |
+
},
|
| 267 |
+
"cell_type": "code",
|
| 268 |
+
"source": "messages.append(user_input)",
|
| 269 |
+
"id": "7e2c4b3918e45d05",
|
| 270 |
+
"outputs": [],
|
| 271 |
+
"execution_count": 21
|
| 272 |
+
},
|
| 273 |
+
{
|
| 274 |
+
"metadata": {
|
| 275 |
+
"ExecuteTime": {
|
| 276 |
+
"end_time": "2024-08-15T19:43:47.901159Z",
|
| 277 |
+
"start_time": "2024-08-15T19:43:45.484119Z"
|
| 278 |
+
}
|
| 279 |
+
},
|
| 280 |
+
"cell_type": "code",
|
| 281 |
+
"source": "res = chat.invoke(messages)",
|
| 282 |
+
"id": "13d44152b30e675e",
|
| 283 |
+
"outputs": [],
|
| 284 |
+
"execution_count": 22
|
| 285 |
+
},
|
| 286 |
+
{
|
| 287 |
+
"metadata": {
|
| 288 |
+
"ExecuteTime": {
|
| 289 |
+
"end_time": "2024-08-15T19:43:48.571511Z",
|
| 290 |
+
"start_time": "2024-08-15T19:43:48.567732Z"
|
| 291 |
+
}
|
| 292 |
+
},
|
| 293 |
+
"cell_type": "code",
|
| 294 |
+
"source": "res.content",
|
| 295 |
+
"id": "e7b8bea4dbf0e56c",
|
| 296 |
+
"outputs": [
|
| 297 |
+
{
|
| 298 |
+
"data": {
|
| 299 |
+
"text/plain": [
|
| 300 |
+
"'Сериал «Во все тяжкие» — это история о преподавателе химии Уолтере Уайте, который узнаёт, что болен раком лёгких. Он решает использовать свои знания в области химии для создания мета, чтобы заработать деньги на лечение и обеспечить свою семью после смерти. В процессе он становится партнёром наркодилера Джесси Пинкмана, с которым они начинают производить и продавать метамфетамин. Уолтер пытается балансировать между своей новой жизнью и жизнью обычного человека, скрывая от семьи свою двойную жизнь. Но постепенно он всё больше погружается в мир криминала и наркотиков, становясь всё более жестоким и беспринципным.'"
|
| 301 |
+
]
|
| 302 |
+
},
|
| 303 |
+
"execution_count": 23,
|
| 304 |
+
"metadata": {},
|
| 305 |
+
"output_type": "execute_result"
|
| 306 |
+
}
|
| 307 |
+
],
|
| 308 |
+
"execution_count": 23
|
| 309 |
}
|
| 310 |
],
|
| 311 |
"metadata": {
|