|
import gradio as gr |
|
import re |
|
|
|
|
|
def analyze_post(content_type, link_to_post, post_likes, post_date, description, comments_likes, comments_count, total_comments_likes, all_comments): |
|
|
|
total_likes = int(post_likes) |
|
total_comments = int(comments_count) |
|
total_likes_on_comments = int(total_comments_likes) |
|
|
|
|
|
avg_likes_per_comment = total_likes_on_comments / total_comments if total_comments > 0 else 0 |
|
|
|
|
|
pattern = r"Фото профиля (\S+)\n(.+?)\n\d+ (?:ч|дн)\.(?:\"Нравится\": (\d+))?(?:Ответить)?" |
|
matches = re.findall(pattern, all_comments) |
|
|
|
|
|
users_output = "\n".join([match[0] for match in matches]) |
|
comments_output = "\n".join([match[1] for match in matches]) |
|
|
|
|
|
likes_chronology_output = "\n".join([match[2] if match[2] else "0" for match in matches]) |
|
|
|
|
|
total_likes_sum = sum(int(match[2]) for match in matches if match[2]) |
|
|
|
|
|
result = ( |
|
f"Content Type: {content_type}\n" |
|
f"Link to Post: {link_to_post}\n" |
|
f"Post Information\n" |
|
f"Likes: {total_likes}\n" |
|
f"Post Date: {post_date}\n" |
|
f"Description: {description}\n\n" |
|
f"Total Comments: {total_comments}\n" |
|
f"Average Likes per Comment: {avg_likes_per_comment:.2f}\n" |
|
) |
|
|
|
return result, users_output, comments_output, likes_chronology_output, total_likes_sum |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_post, |
|
inputs=[ |
|
gr.Radio(["Photo", "Video"], label="Content Type"), |
|
gr.Textbox(label="Link to Post", placeholder="Введите ссылку на пост"), |
|
gr.Number(label="Likes"), |
|
gr.Textbox(label="Post Date", placeholder="Введите дату публикации"), |
|
gr.Textbox(label="Description", placeholder="Введите описание поста"), |
|
gr.Number(label="Количество лайков на комментарии"), |
|
gr.Number(label="Количество комментариев"), |
|
gr.Number(label="Общее количество лайков на комментариях"), |
|
gr.Textbox(label="Все комментарии", placeholder="Введите комментарии в формате, как указано в примере") |
|
], |
|
outputs=["text", "text", "text", "text", "number"], |
|
title="Анализ Instagram-поста", |
|
description="Введите данные о посте в Instagram для анализа. Формат комментариев: 'Фото профиля [ник]\n[комментарий]\n[время]\"Нравится\": [число]Ответить'" |
|
) |
|
|
|
|
|
iface.launch() |