methodya commited on
Commit
7ca52d7
·
verified ·
1 Parent(s): 6a26ff6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+ import os
5
+ from huggingface_hub import login
6
+
7
+ # تسجيل الدخول
8
+ login(token=os.environ.get('HUGGING_FACE_HUB_TOKEN'))
9
+
10
+ # تهيئة النموذج
11
+ model_name = "google/gemma-2b-it"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModelForCausalLM.from_pretrained(model_name)
14
+
15
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
16
+ model = model.to(device)
17
+
18
+ def generate_summary(text):
19
+ prompt = f"""لخص هذا النص بدقة:
20
+ {text}
21
+
22
+ التلخيص يجب أن يتضمن:
23
+ - فقرة قصيرة تشرح الفكرة الأساسية
24
+ - ثلاث نقاط رئيسية
25
+ """
26
+
27
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
28
+ outputs = model.generate(
29
+ **inputs,
30
+ max_length=256, # تقليل الطول لتسريع الاستجابة
31
+ temperature=0.3 # تقليل العشوائية للحصول على نتائج أكثر دقة
32
+ )
33
+
34
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
35
+
36
+ css = ".rtl-text { direction: rtl; text-align: right; }"
37
+
38
+ interface = gr.Interface(
39
+ fn=generate_summary,
40
+ inputs=gr.Textbox(label="النص", lines=6, elem_classes="rtl-text"),
41
+ outputs=gr.Textbox(label="الملخص", lines=6, elem_classes="rtl-text"),
42
+ title="ملخص النصوص",
43
+ theme=gr.themes.Soft(),
44
+ css=css
45
+ )
46
+
47
+ interface.launch()