Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# تحميل الموديل من الهاغينغ فيس
|
| 6 |
+
model_name = "USERNAME/t5-title-generator-ar" # بدّلك USERNAME باسم حسابك
|
| 7 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 8 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
model = model.to(device)
|
| 12 |
+
|
| 13 |
+
def generate_title(abstract):
|
| 14 |
+
inputs = tokenizer(
|
| 15 |
+
"summarize: " + abstract,
|
| 16 |
+
return_tensors="pt",
|
| 17 |
+
max_length=256,
|
| 18 |
+
truncation=True
|
| 19 |
+
).to(device)
|
| 20 |
+
|
| 21 |
+
outputs = model.generate(
|
| 22 |
+
**inputs,
|
| 23 |
+
max_new_tokens=32,
|
| 24 |
+
num_beams=5,
|
| 25 |
+
num_return_sequences=3,
|
| 26 |
+
early_stopping=True,
|
| 27 |
+
no_repeat_ngram_size=2
|
| 28 |
+
)
|
| 29 |
+
titles = [tokenizer.decode(out, skip_special_tokens=True) for out in outputs]
|
| 30 |
+
return "\n".join([f"{i+1}. {title}" for i, title in enumerate(titles)])
|
| 31 |
+
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=generate_title,
|
| 34 |
+
inputs=gr.Textbox(label="الملخص"),
|
| 35 |
+
outputs=gr.Textbox(label="العناوين المولّدة"),
|
| 36 |
+
title="مولد عناوين للأبحاث 🧠📄",
|
| 37 |
+
description="أدخل ملخص بحث وسنولد لك 3 عناوين محتملة باستخدام T5"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
iface.launch(share=True, show_error=True)
|