Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,19 @@ import requests
|
|
6 |
from langchain.prompts import ChatPromptTemplate
|
7 |
from langchain_community.llms import Ollama
|
8 |
import subprocess
|
|
|
9 |
|
10 |
from func_ai import classify_comment, retrieve_from_vdb, VECTOR_API_URL
|
11 |
from func_facebook import get_page_id, has_page_replied, get_unanswered_comments, reply_comment, hide_negative_comments
|
12 |
|
|
|
|
|
|
|
|
|
13 |
# Wait for the server to start
|
14 |
time.sleep(10)
|
15 |
llm = Ollama(model="llama3.1")
|
16 |
-
|
17 |
|
18 |
template = """
|
19 |
You are an assistant answering users' questions using the provided context. Your tasks:
|
@@ -33,9 +38,8 @@ You are an assistant answering users' questions using the provided context. Your
|
|
33 |
Question: {input}
|
34 |
"""
|
35 |
|
36 |
-
|
37 |
def upload_file_vdb(file):
|
38 |
-
|
39 |
API_URL = f"{VECTOR_API_URL}/upload/"
|
40 |
|
41 |
file_path = file
|
@@ -48,42 +52,40 @@ def upload_file_vdb(file):
|
|
48 |
|
49 |
# Обработка ответа от сервера
|
50 |
if response.status_code == 200:
|
51 |
-
|
52 |
-
return
|
53 |
else:
|
54 |
-
|
55 |
return f"Ошибка: {response.json().get('detail')}"
|
56 |
|
57 |
-
|
58 |
def generate_response(user_query):
|
59 |
-
|
60 |
prompt = ChatPromptTemplate.from_template(template)
|
61 |
|
62 |
documents = retrieve_from_vdb(user_query)
|
63 |
context = "\n".join(documents)
|
64 |
|
65 |
-
|
66 |
full_prompt = prompt.format(context=context, input=user_query)
|
67 |
|
68 |
response = llm.invoke(full_prompt)
|
69 |
-
|
70 |
return response
|
71 |
|
72 |
-
|
73 |
def process_comments(ACCESS_TOKEN):
|
74 |
-
|
75 |
hidden_comments_data = hide_negative_comments(ACCESS_TOKEN)
|
76 |
-
|
77 |
|
78 |
-
|
79 |
posts_with_unanswered_comments = get_unanswered_comments(ACCESS_TOKEN)
|
80 |
|
81 |
page_id = get_page_id(ACCESS_TOKEN)
|
82 |
if not page_id:
|
83 |
-
|
84 |
return {"status": "failed", "reason": "Не удалось получить ID страницы."}
|
85 |
|
86 |
-
|
87 |
|
88 |
processed_posts = []
|
89 |
|
@@ -96,12 +98,12 @@ def process_comments(ACCESS_TOKEN):
|
|
96 |
|
97 |
for comment in unanswered_comments:
|
98 |
message = comment['message']
|
99 |
-
|
100 |
classification = classify_comment(message)
|
101 |
-
|
102 |
if classification == "interrogative":
|
103 |
response_message = generate_response(message)
|
104 |
-
|
105 |
success = reply_comment(comment_id=comment['id'], message=response_message, token=ACCESS_TOKEN)
|
106 |
if success:
|
107 |
post_replies.append({
|
@@ -122,7 +124,6 @@ def process_comments(ACCESS_TOKEN):
|
|
122 |
"posts": processed_posts
|
123 |
}
|
124 |
|
125 |
-
|
126 |
with gr.Blocks() as demo:
|
127 |
with gr.Tab("Главная страница"):
|
128 |
gr.Markdown("# Facebook Comment Filter")
|
|
|
6 |
from langchain.prompts import ChatPromptTemplate
|
7 |
from langchain_community.llms import Ollama
|
8 |
import subprocess
|
9 |
+
from datetime import datetime
|
10 |
|
11 |
from func_ai import classify_comment, retrieve_from_vdb, VECTOR_API_URL
|
12 |
from func_facebook import get_page_id, has_page_replied, get_unanswered_comments, reply_comment, hide_negative_comments
|
13 |
|
14 |
+
def log_message(message):
|
15 |
+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
16 |
+
print(f"[{timestamp}] {message}")
|
17 |
+
|
18 |
# Wait for the server to start
|
19 |
time.sleep(10)
|
20 |
llm = Ollama(model="llama3.1")
|
21 |
+
log_message("Модель Ollama 'llama3.1' инициализирована.")
|
22 |
|
23 |
template = """
|
24 |
You are an assistant answering users' questions using the provided context. Your tasks:
|
|
|
38 |
Question: {input}
|
39 |
"""
|
40 |
|
|
|
41 |
def upload_file_vdb(file):
|
42 |
+
log_message("Загружаем файл")
|
43 |
API_URL = f"{VECTOR_API_URL}/upload/"
|
44 |
|
45 |
file_path = file
|
|
|
52 |
|
53 |
# Обработка ответа от сервера
|
54 |
if response.status_code == 200:
|
55 |
+
log_message("Файл успешно загружен.")
|
56 |
+
return "Файл успешно загружен."
|
57 |
else:
|
58 |
+
log_message(f"Ошибка при загрузке файла: {response.json().get('detail')}")
|
59 |
return f"Ошибка: {response.json().get('detail')}"
|
60 |
|
|
|
61 |
def generate_response(user_query):
|
62 |
+
log_message(f"Генерация ответа на запрос: {user_query}")
|
63 |
prompt = ChatPromptTemplate.from_template(template)
|
64 |
|
65 |
documents = retrieve_from_vdb(user_query)
|
66 |
context = "\n".join(documents)
|
67 |
|
68 |
+
log_message(f"Контекст из базы данных: {context[:100]}...")
|
69 |
full_prompt = prompt.format(context=context, input=user_query)
|
70 |
|
71 |
response = llm.invoke(full_prompt)
|
72 |
+
log_message(f"Сгенерированный ответ: {response}")
|
73 |
return response
|
74 |
|
|
|
75 |
def process_comments(ACCESS_TOKEN):
|
76 |
+
log_message("Начинаем процесс скрытия отрицательных комментариев.")
|
77 |
hidden_comments_data = hide_negative_comments(ACCESS_TOKEN)
|
78 |
+
log_message(f"Количество постов с скрытыми комментариями: {len(hidden_comments_data)}")
|
79 |
|
80 |
+
log_message("Получение неотвеченных комментариев.")
|
81 |
posts_with_unanswered_comments = get_unanswered_comments(ACCESS_TOKEN)
|
82 |
|
83 |
page_id = get_page_id(ACCESS_TOKEN)
|
84 |
if not page_id:
|
85 |
+
log_message("Не удалось получить ID страницы.")
|
86 |
return {"status": "failed", "reason": "Не удалось получить ID страницы."}
|
87 |
|
88 |
+
log_message(f"ID страницы: {page_id}")
|
89 |
|
90 |
processed_posts = []
|
91 |
|
|
|
98 |
|
99 |
for comment in unanswered_comments:
|
100 |
message = comment['message']
|
101 |
+
log_message(f"Обработка комментария: {message}")
|
102 |
classification = classify_comment(message)
|
103 |
+
log_message(f"Классификация комментария: {classification}")
|
104 |
if classification == "interrogative":
|
105 |
response_message = generate_response(message)
|
106 |
+
log_message(f"Ответ на комментарий: {response_message}")
|
107 |
success = reply_comment(comment_id=comment['id'], message=response_message, token=ACCESS_TOKEN)
|
108 |
if success:
|
109 |
post_replies.append({
|
|
|
124 |
"posts": processed_posts
|
125 |
}
|
126 |
|
|
|
127 |
with gr.Blocks() as demo:
|
128 |
with gr.Tab("Главная страница"):
|
129 |
gr.Markdown("# Facebook Comment Filter")
|