Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,54 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
|
5 |
# گرفتن توکن از متغیر محیطی
|
6 |
token = os.environ.get("HF_TOKEN")
|
7 |
|
8 |
-
# استفاده از مدل
|
9 |
pipe = pipeline(
|
10 |
"text-generation",
|
11 |
-
model="google/gemma-2b-it",
|
12 |
token=token
|
13 |
)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def generate_topics(field, major, keywords, audience, level):
|
16 |
prompt = f"""
|
17 |
۳ موضوع پایاننامه در رشته {field} با گرایش {major} پیشنهاد بده که به کلیدواژههای "{keywords}" مربوط باشه و جامعه هدف آن "{audience}" باشد. مقطع: {level}.
|
18 |
موضوعات را فارسی بنویس.
|
19 |
"""
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
# حذف
|
23 |
-
|
24 |
-
response = response[len(prompt.strip()):].strip()
|
25 |
|
26 |
-
# افزودن
|
27 |
-
|
28 |
|
29 |
-
return
|
30 |
|
31 |
iface = gr.Interface(
|
32 |
fn=generate_topics,
|
|
|
1 |
import os
|
2 |
+
import re
|
3 |
import gradio as gr
|
4 |
from transformers import pipeline
|
5 |
|
6 |
# گرفتن توکن از متغیر محیطی
|
7 |
token = os.environ.get("HF_TOKEN")
|
8 |
|
9 |
+
# استفاده از مدل نسبتاً سبک و قابل اجرا در Hugging Face Spaces
|
10 |
pipe = pipeline(
|
11 |
"text-generation",
|
12 |
+
model="google/gemma-2b-it",
|
13 |
token=token
|
14 |
)
|
15 |
|
16 |
+
# تابع تشخیص و ترجمه کلمات انگلیسی رایج در حوزه روانشناسی
|
17 |
+
EN_FA_DICT = {
|
18 |
+
"behavior": "رفتار",
|
19 |
+
"self-esteem": "عزت نفس",
|
20 |
+
"confidence": "اعتماد به نفس",
|
21 |
+
"learning": "یادگیری",
|
22 |
+
"attachment": "دلبستگی",
|
23 |
+
"communication": "ارتباط",
|
24 |
+
"stress": "استرس",
|
25 |
+
"parent": "والد",
|
26 |
+
"trauma": "آسیب روانی"
|
27 |
+
}
|
28 |
+
|
29 |
+
def replace_english_words(text):
|
30 |
+
for eng, fa in EN_FA_DICT.items():
|
31 |
+
text = re.sub(rf"\b{eng}\b", fa, text, flags=re.IGNORECASE)
|
32 |
+
return text
|
33 |
+
|
34 |
def generate_topics(field, major, keywords, audience, level):
|
35 |
prompt = f"""
|
36 |
۳ موضوع پایاننامه در رشته {field} با گرایش {major} پیشنهاد بده که به کلیدواژههای "{keywords}" مربوط باشه و جامعه هدف آن "{audience}" باشد. مقطع: {level}.
|
37 |
موضوعات را فارسی بنویس.
|
38 |
"""
|
39 |
+
output = pipe(prompt, max_new_tokens=250)[0]['generated_text']
|
40 |
+
|
41 |
+
# حذف prompt در صورتی که مدل آن را تکرار کرده باشد
|
42 |
+
if output.startswith(prompt.strip()):
|
43 |
+
output = output[len(prompt.strip()):].strip()
|
44 |
|
45 |
+
# حذف فاصلههای اضافی و جایگزینی کلمات انگلیسی
|
46 |
+
output = replace_english_words(output.strip())
|
|
|
47 |
|
48 |
+
# افزودن پیام پایانی تبلیغاتی
|
49 |
+
output += "\n\nبرای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:\n02188252497"
|
50 |
|
51 |
+
return output
|
52 |
|
53 |
iface = gr.Interface(
|
54 |
fn=generate_topics,
|