trashchenkov commited on
Commit
4e3ae23
1 Parent(s): 7ea0e11

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain_core.prompts import ChatPromptTemplate
3
+ from langchain.chains.combine_documents import create_stuff_documents_chain
4
+ from langchain.chains import create_retrieval_chain
5
+ from langchain.chat_models.gigachat import GigaChat
6
+ from langchain_community.vectorstores import FAISS
7
+ from langchain.embeddings import HuggingFaceEmbeddings
8
+ import os
9
+ import telebot
10
+
11
+
12
+ def get_yt_links(contexts):
13
+ html = '''
14
+ <iframe width="100%" height="200" src="{}?start={}" \
15
+ title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; \
16
+ encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" \
17
+ allowfullscreen></iframe>
18
+ '''
19
+ yt_htmls = []
20
+ for context in contexts:
21
+ link = context.metadata['link']
22
+ start = context.metadata['time']
23
+ yt_htmls.append(html.format(link, start))
24
+ return yt_htmls
25
+
26
+
27
+ def process_input(text):
28
+ response = retrieval_chain.invoke({"input": text})
29
+ bot.send_message(int(user_id), str(response))
30
+ youtube_links = get_yt_links(response['context'])
31
+ return response['answer'], youtube_links[0], youtube_links[1], youtube_links[2]
32
+
33
+ giga = os.getenv('GIGA')
34
+ token = os.getenv('BOT')
35
+ user_id = os.getenv('CREATOR')
36
+ bot = telebot.TeleBot(token)
37
+ model_name = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
38
+ model_kwargs = {'device': 'cpu'}
39
+ encode_kwargs = {'normalize_embeddings': False}
40
+ embedding = HuggingFaceEmbeddings(model_name=model_name,
41
+ model_kwargs=model_kwargs,
42
+ encode_kwargs=encode_kwargs)
43
+
44
+ vector_db = FAISS.load_local('faiss_index',
45
+ embeddings=embedding,
46
+ allow_dangerous_deserialization=True)
47
+ llm = GigaChat(credentials=giga, verify_ssl_certs=False, profanity_check=False)
48
+
49
+ prompt = ChatPromptTemplate.from_template('''Ответь на вопрос пользователя. \
50
+ Используй при этом только информацию из контекста. Если в контексте нет \
51
+ информации для ответа, сообщи об этом пользователю.
52
+ Контекст: {context}
53
+ Вопрос: {input}
54
+ Ответ:'''
55
+ )
56
+
57
+ embedding_retriever = vector_store.as_retriever(search_kwargs={"k": 3})
58
+
59
+ document_chain = create_stuff_documents_chain(
60
+ llm=llm,
61
+ prompt=prompt
62
+ )
63
+
64
+ retrieval_chain = create_retrieval_chain(embedding_retriever, document_chain)
65
+
66
+ with gr.Blocks() as demo:
67
+ with gr.Row():
68
+ with gr.Column():
69
+ text_input = gr.Textbox(label="Введите запрос")
70
+ submit_btn = gr.Button("Отправить запрос")
71
+ text_output = gr.Textbox(label="Ответ", interactive=False)
72
+
73
+ with gr.Column():
74
+ youtube_video1 = gr.HTML()
75
+ youtube_video2 = gr.HTML()
76
+ youtube_video3 = gr.HTML()
77
+
78
+ submit_btn.click(process_input, text_input, [text_output, youtube_video1, youtube_video2, youtube_video3])
79
+
80
+
81
+ demo.launch()