|
import gradio as gr |
|
import re |
|
|
|
|
|
def process_text(input_text, comment_count): |
|
|
|
print(f"Входные данные: {input_text}") |
|
print(f"Ожидаемое количество комментариев: {comment_count}") |
|
|
|
|
|
pattern = r"([a-zA-Z0-9_@]+(?:[a-zA-Z0-9_]+)?)\s*(.*?)\s*(?:Нравится:\s*(\d+))" |
|
|
|
|
|
if not input_text.strip(): |
|
return "Пожалуйста, введите текст." |
|
|
|
|
|
matches = re.findall(pattern, input_text) |
|
|
|
|
|
print(f"Найденные совпадения: {matches}") |
|
|
|
|
|
if not matches: |
|
return "Совпадения не найдены. Пожалуйста, проверьте ввод." |
|
|
|
|
|
actual_comment_count = len(matches) |
|
if actual_comment_count != int(comment_count): |
|
return (f"Предупреждение: найдено {actual_comment_count} комментариев, " |
|
f"хотя указано {comment_count}.\n" |
|
"Пожалуйста, проверьте ввод.") |
|
|
|
|
|
output = [] |
|
for i, match in enumerate(matches, 1): |
|
username, text, likes = match |
|
output.append(f'{i}. Пользователь: "{username}", Текст: "{text.strip()}", Лайков: {likes or 0}') |
|
|
|
return "\n".join(output) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_text, |
|
inputs=[ |
|
gr.Textbox(lines=10, placeholder="Введите текст сюда..."), |
|
gr.Number(label="Количество комментариев по данным Instagram") |
|
], |
|
outputs=gr.Textbox(lines=10, placeholder="Результат...") |
|
) |
|
|
|
|
|
iface.launch() |