diginoron commited on
Commit
0c0559d
·
verified ·
1 Parent(s): effcdd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -33
app.py CHANGED
@@ -1,55 +1,51 @@
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,
55
  inputs=[
 
1
  import os
 
2
  import gradio as gr
3
+ from transformers import pipeline, MarianMTModel, MarianTokenizer
4
 
5
+ # گرفتن توکن از متغیر محیطی (برای Hugging Face API اگر نیاز بود)
6
  token = os.environ.get("HF_TOKEN")
7
 
8
+ # ⬇️ مدل تولید متن انگلیسی (پیشنهاد موضوع)
9
+ text_gen = pipeline(
10
  "text-generation",
11
+ model="google/gemma-2b-it", # نسخه سبک و سازگار
12
  token=token
13
  )
14
 
15
+ # ⬇️ مدل ترجمه انگلیسی به فارسی
16
+ translation_model_name = "Helsinki-NLP/opus-mt-en-fa"
17
+ translator_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
18
+ translator_model = MarianMTModel.from_pretrained(translation_model_name)
 
 
 
 
 
 
 
 
19
 
20
+ def translate_to_persian(text):
21
+ inputs = translator_tokenizer(text, return_tensors="pt", padding=True, truncation=True)
22
+ translated = translator_model.generate(**inputs)
23
+ return translator_tokenizer.decode(translated[0], skip_special_tokens=True)
24
 
25
  def generate_topics(field, major, keywords, audience, level):
26
+ # ساخت پرامپت برای مدل انگلیسی
27
  prompt = f"""
28
+ Suggest 3 thesis topics in the field of {field}, with a specialization in {major},
29
+ related to the keywords "{keywords}", and targeting the audience "{audience}".
30
+ The academic level is {level}. Just list the topics briefly.
31
  """
 
32
 
33
+ # تولید متن توسط مدل زبان
34
+ raw_output = text_gen(prompt, max_new_tokens=250)[0]['generated_text']
 
35
 
36
+ # حذف متن prompt تکراری (اگر مدل تکرار کرد)
37
+ if raw_output.startswith(prompt.strip()):
38
+ raw_output = raw_output[len(prompt.strip()):].strip()
39
 
40
+ # ترجمه خروجی به فارسی
41
+ translated_output = translate_to_persian(raw_output.strip())
42
 
43
+ # افزودن پیام تبلیغاتی در پایان
44
+ final_output = translated_output + "\n\nبرای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:\n02188252497"
45
 
46
+ return final_output
47
+
48
+ # رابط Gradio
49
  iface = gr.Interface(
50
  fn=generate_topics,
51
  inputs=[