|
import re |
|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
text_generator = pipeline("text-generation", model="aubmindlab/aragpt2-medium") |
|
|
|
|
|
def load_poem(file_path): |
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
text = file.read() |
|
return text |
|
|
|
|
|
def complete_poem(user_input): |
|
|
|
file_text = load_poem("poem.txt") |
|
|
|
|
|
if user_input not in file_text: |
|
return "النص المدخل ليس جزءًا من القصيدة." |
|
|
|
|
|
combined_text = file_text + "\n" + user_input |
|
completion = text_generator(combined_text, max_length=300, num_return_sequences=1) |
|
|
|
|
|
generated_text = completion[0]["generated_text"] |
|
match = re.search(re.escape(user_input) + r'([\s\S]*?)[.!?]', generated_text) |
|
if match: |
|
return match.group(1).strip() |
|
else: |
|
return "لم يتم العثور على جملة كاملة بعد الإدخال مباشرة." |
|
|
|
|
|
interface = gr.Interface(fn=complete_poem, |
|
inputs="text", |
|
outputs="text", |
|
title="إكمال النصوص الشعرية", |
|
description="تكملة لقصيدة متعب التركي.") |
|
|
|
|
|
interface.launch() |
|
|