Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ def log_message(message):
|
|
17 |
|
18 |
|
19 |
# Wait for the server to start
|
20 |
-
|
21 |
llm = Ollama(model="llama3.1")
|
22 |
log_message("Модель Ollama 'llama3.1' инициализирована.")
|
23 |
|
@@ -53,38 +53,40 @@ def delete_faiss_index():
|
|
53 |
log_message(f"Ошибка при удалении FAISS индекса: {response.json().get('detail')}")
|
54 |
return {"status": "error", "message": response.json().get("detail", "Ошибка при удалении FAISS индекса.")}
|
55 |
|
56 |
-
|
57 |
-
def upload_file_vdb(file):
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
def
|
79 |
-
log_message(f"Генерация
|
80 |
prompt = ChatPromptTemplate.from_template(template)
|
81 |
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
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,15 +105,16 @@ 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,24 +123,34 @@ def process_comments(ACCESS_TOKEN, user_context):
|
|
120 |
processed_comment_ids.add(comment_id)
|
121 |
|
122 |
message = comment['message']
|
123 |
-
log_message(f"
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
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 |
-
|
141 |
})
|
142 |
|
143 |
return {
|
@@ -146,6 +159,7 @@ def process_comments(ACCESS_TOKEN, user_context):
|
|
146 |
}
|
147 |
|
148 |
|
|
|
149 |
with gr.Blocks() as demo:
|
150 |
with gr.Tab("Главная страница"):
|
151 |
gr.Markdown("# Facebook Comment Filter")
|
@@ -173,4 +187,5 @@ if __name__ == "__main__":
|
|
173 |
debug=True,
|
174 |
server_port=7860,
|
175 |
server_name="0.0.0.0",
|
|
|
176 |
)
|
|
|
17 |
|
18 |
|
19 |
# Wait for the server to start
|
20 |
+
time.sleep(10)
|
21 |
llm = Ollama(model="llama3.1")
|
22 |
log_message("Модель Ollama 'llama3.1' инициализирована.")
|
23 |
|
|
|
53 |
log_message(f"Ошибка при удалении FAISS индекса: {response.json().get('detail')}")
|
54 |
return {"status": "error", "message": response.json().get("detail", "Ошибка при удалении FAISS индекса.")}
|
55 |
|
56 |
+
#
|
57 |
+
# def upload_file_vdb(file):
|
58 |
+
# log_message("Загружаем файл")
|
59 |
+
# API_URL = f"{VECTOR_API_URL}/upload/"
|
60 |
+
#
|
61 |
+
# file_path = file
|
62 |
+
# file_name = os.path.basename(file_path)
|
63 |
+
#
|
64 |
+
# # Открываем файл в бинарном режиме
|
65 |
+
# with open(file_path, 'rb') as f:
|
66 |
+
# files = {'file': (file_name, f)}
|
67 |
+
# response = requests.post(API_URL, files=files)
|
68 |
+
#
|
69 |
+
# # Обработка ответа от сервера
|
70 |
+
# if response.status_code == 200:
|
71 |
+
# log_message("Файл успешно загружен.")
|
72 |
+
# return "Файл успешно загружен."
|
73 |
+
# else:
|
74 |
+
# log_message(f"Ошибка при загрузке файла: {response.json().get('detail')}")
|
75 |
+
# return f"Ошибка: {response.json().get('detail')}"
|
76 |
+
|
77 |
+
|
78 |
+
def generate_responses(user_queries, context):
|
79 |
+
log_message(f"Генерация ответов на {len(user_queries)} запросов")
|
80 |
prompt = ChatPromptTemplate.from_template(template)
|
81 |
|
82 |
+
# Формируем список полных промптов для каждого запроса
|
83 |
+
full_prompts = [prompt.format(context=context, input=query) for query in user_queries]
|
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 |
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 |
processed_comment_ids.add(comment_id)
|
124 |
|
125 |
message = comment['message']
|
126 |
+
log_message(f"Добавление комментария для обработки: {message}")
|
127 |
+
|
128 |
+
# Добавляем комментарий и его ID в списки
|
129 |
+
all_comments.append(message)
|
130 |
+
all_comment_ids.append(comment_id)
|
131 |
+
|
132 |
+
# Генерируем ответы на ��се комментарии батчем
|
133 |
+
responses = generate_responses(all_comments, context=user_context)
|
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 |
}
|
160 |
|
161 |
|
162 |
+
|
163 |
with gr.Blocks() as demo:
|
164 |
with gr.Tab("Главная страница"):
|
165 |
gr.Markdown("# Facebook Comment Filter")
|
|
|
187 |
debug=True,
|
188 |
server_port=7860,
|
189 |
server_name="0.0.0.0",
|
190 |
+
|
191 |
)
|