File size: 1,709 Bytes
1fb8bfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import re
import gradio as gr
from transformers import pipeline

# تحميل النموذج باستخدام 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 "لم يتم العثور على جملة كاملة بعد الإدخال مباشرة."

# واجهة Gradio
interface = gr.Interface(fn=complete_poem,
                         inputs="text",
                         outputs="text",
                         title="إكمال النصوص الشعرية",
                         description="تكملة لقصيدة متعب التركي.")

# تشغيل الواجهة
interface.launch()