Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -75,18 +75,16 @@ def delete_faiss_index():
|
|
75 |
# return f"Ошибка: {response.json().get('detail')}"
|
76 |
|
77 |
|
78 |
-
def
|
79 |
-
log_message(f"Генерация
|
80 |
prompt = ChatPromptTemplate.from_template(template)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
# Используем llm.batch для получения ответов на все промпты
|
86 |
-
responses = llm.batch(full_prompts)
|
87 |
-
log_message("Ответы сгенерированы.")
|
88 |
-
return responses
|
89 |
|
|
|
|
|
|
|
90 |
|
91 |
|
92 |
def process_comments(ACCESS_TOKEN, user_context):
|
@@ -105,16 +103,15 @@ def process_comments(ACCESS_TOKEN, user_context):
|
|
105 |
log_message(f"ID страницы: {page_id}")
|
106 |
|
107 |
processed_posts = []
|
108 |
-
processed_comment_ids = set()
|
109 |
-
|
110 |
-
all_comments = []
|
111 |
-
all_comment_ids = []
|
112 |
|
113 |
for post_data in posts_with_unanswered_comments:
|
114 |
post_id = post_data['post_id']
|
115 |
post_message = post_data['post_message']
|
116 |
unanswered_comments = post_data['unanswered_comments']
|
117 |
|
|
|
|
|
118 |
for comment in unanswered_comments:
|
119 |
comment_id = comment['id']
|
120 |
if comment_id in processed_comment_ids:
|
@@ -123,34 +120,24 @@ def process_comments(ACCESS_TOKEN, user_context):
|
|
123 |
processed_comment_ids.add(comment_id)
|
124 |
|
125 |
message = comment['message']
|
126 |
-
log_message(f"
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
134 |
|
135 |
-
# Отправляем ответы на соответствующие комментарии
|
136 |
-
for comment_id, response_message in zip(all_comment_ids, responses):
|
137 |
-
log_message(f"Ответ на комментарий {comment_id}: {response_message}")
|
138 |
-
success = reply_comment(comment_id=comment_id, message=response_message, token=ACCESS_TOKEN)
|
139 |
-
if success:
|
140 |
-
log_message(f"Успешно отправлен ответ на комментарий {comment_id}")
|
141 |
-
else:
|
142 |
-
log_message(f"Ошибка при отправке ответа на комментарий {comment_id}")
|
143 |
-
|
144 |
-
# Формируем информацию о обработанных постах
|
145 |
-
for post_data in posts_with_unanswered_comments:
|
146 |
-
post_id = post_data['post_id']
|
147 |
-
post_message = post_data['post_message']
|
148 |
processed_posts.append({
|
149 |
'post_id': post_id,
|
150 |
'post_message': post_message,
|
151 |
'hidden_comments': next(
|
152 |
(item['hidden_comments'] for item in hidden_comments_data if item['post_id'] == post_id), []),
|
153 |
-
|
154 |
})
|
155 |
|
156 |
return {
|
@@ -159,7 +146,6 @@ def process_comments(ACCESS_TOKEN, user_context):
|
|
159 |
}
|
160 |
|
161 |
|
162 |
-
|
163 |
with gr.Blocks() as demo:
|
164 |
with gr.Tab("Главная страница"):
|
165 |
gr.Markdown("# Facebook Comment Filter")
|
|
|
75 |
# return f"Ошибка: {response.json().get('detail')}"
|
76 |
|
77 |
|
78 |
+
def generate_response(user_query, context):
|
79 |
+
log_message(f"Генерация ответа на запрос: {user_query}")
|
80 |
prompt = ChatPromptTemplate.from_template(template)
|
81 |
|
82 |
+
log_message(f"Контекст из базы данных: {context[:100]}...")
|
83 |
+
full_prompt = prompt.format(context=context, input=user_query)
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
response = llm.invoke(full_prompt)
|
86 |
+
log_message(f"Сгенерированный ответ: {response}")
|
87 |
+
return response
|
88 |
|
89 |
|
90 |
def process_comments(ACCESS_TOKEN, user_context):
|
|
|
103 |
log_message(f"ID страницы: {page_id}")
|
104 |
|
105 |
processed_posts = []
|
106 |
+
processed_comment_ids = set() # Отслеживание обработанных комментариев
|
|
|
|
|
|
|
107 |
|
108 |
for post_data in posts_with_unanswered_comments:
|
109 |
post_id = post_data['post_id']
|
110 |
post_message = post_data['post_message']
|
111 |
unanswered_comments = post_data['unanswered_comments']
|
112 |
|
113 |
+
post_replies = []
|
114 |
+
|
115 |
for comment in unanswered_comments:
|
116 |
comment_id = comment['id']
|
117 |
if comment_id in processed_comment_ids:
|
|
|
120 |
processed_comment_ids.add(comment_id)
|
121 |
|
122 |
message = comment['message']
|
123 |
+
log_message(f"Обработка комментария: {message}")
|
124 |
+
|
125 |
+
response_message = generate_response(message, context=user_context)
|
126 |
+
log_message(f"Ответ на комментарий: {response_message}")
|
127 |
+
success = reply_comment(comment_id=comment['id'], message=response_message, token=ACCESS_TOKEN)
|
128 |
+
if success:
|
129 |
+
post_replies.append({
|
130 |
+
'comment_id': comment['id'],
|
131 |
+
'comment_message': comment['message'],
|
132 |
+
'reply_message': response_message
|
133 |
+
})
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
processed_posts.append({
|
136 |
'post_id': post_id,
|
137 |
'post_message': post_message,
|
138 |
'hidden_comments': next(
|
139 |
(item['hidden_comments'] for item in hidden_comments_data if item['post_id'] == post_id), []),
|
140 |
+
'replies': post_replies
|
141 |
})
|
142 |
|
143 |
return {
|
|
|
146 |
}
|
147 |
|
148 |
|
|
|
149 |
with gr.Blocks() as demo:
|
150 |
with gr.Tab("Главная страница"):
|
151 |
gr.Markdown("# Facebook Comment Filter")
|