|
import gradio as gr |
|
import re |
|
|
|
|
|
def analyze_post(description, likes, comments, likes_on_comments, all_comments): |
|
|
|
total_likes = int(likes) |
|
total_comments = int(comments) |
|
total_likes_on_comments = int(likes_on_comments) |
|
|
|
|
|
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"Описание поста: {description}\n" |
|
f"Количество лайков: {total_likes}\n" |
|
f"Количество комментариев: {total_comments}\n" |
|
f"Среднее количество лайков на комментарий: {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.Textbox(label="Описание поста", 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() |