Spaces:
Running
Running
Update app.py
Browse files
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 |
-
#
|
10 |
-
|
11 |
"text-generation",
|
12 |
-
model="google/gemma-2b-it",
|
13 |
token=token
|
14 |
)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
"confidence": "اعتماد به نفس",
|
21 |
-
"learning": "یادگیری",
|
22 |
-
"attachment": "دلبستگی",
|
23 |
-
"communication": "ارتباط",
|
24 |
-
"stress": "استرس",
|
25 |
-
"parent": "والد",
|
26 |
-
"trauma": "آسیب روانی"
|
27 |
-
}
|
28 |
|
29 |
-
def
|
30 |
-
|
31 |
-
|
32 |
-
return
|
33 |
|
34 |
def generate_topics(field, major, keywords, audience, level):
|
|
|
35 |
prompt = f"""
|
36 |
-
|
37 |
-
|
|
|
38 |
"""
|
39 |
-
output = pipe(prompt, max_new_tokens=250)[0]['generated_text']
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
output = output[len(prompt.strip()):].strip()
|
44 |
|
45 |
-
# حذف
|
46 |
-
|
|
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
|
51 |
-
|
|
|
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=[
|