File size: 2,502 Bytes
d059e89
7db8f01
0c0559d
7db8f01
0c0559d
d059e89
 
0c0559d
 
d059e89
0c0559d
d059e89
 
7db8f01
0c0559d
0a78368
0c0559d
 
effcdd9
0c0559d
 
 
 
effcdd9
7db8f01
0c0559d
7db8f01
0c0559d
 
 
0a7ae43
effcdd9
0c0559d
 
0a7ae43
0c0559d
 
 
0a7ae43
0c0559d
 
0a7ae43
0c0559d
 
7db8f01
0c0559d
 
 
7db8f01
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import gradio as gr
from transformers import pipeline, MarianMTModel, MarianTokenizer

# گرفتن توکن از متغیر محیطی (برای Hugging Face API اگر نیاز بود)
token = os.environ.get("HF_TOKEN")

# ⬇️ مدل تولید متن انگلیسی (پیشنهاد موضوع)
text_gen = pipeline(
    "text-generation",
    model="google/gemma-2b-it",  # نسخه سبک و سازگار
    token=token
)

# ⬇️ مدل ترجمه انگلیسی به فارسی
translation_model_name = "Helsinki-NLP/opus-mt-en-fa-opus"
translator_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
translator_model = MarianMTModel.from_pretrained(translation_model_name)

def translate_to_persian(text):
    inputs = translator_tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    translated = translator_model.generate(**inputs)
    return translator_tokenizer.decode(translated[0], skip_special_tokens=True)

def generate_topics(field, major, keywords, audience, level):
    # ساخت پرامپت برای مدل انگلیسی
    prompt = f"""
Suggest 3 thesis topics in the field of {field}, with a specialization in {major}, 
related to the keywords "{keywords}", and targeting the audience "{audience}". 
The academic level is {level}. Just list the topics briefly.
"""

    # تولید متن توسط مدل زبان
    raw_output = text_gen(prompt, max_new_tokens=250)[0]['generated_text']

    # حذف متن prompt تکراری (اگر مدل تکرار کرد)
    if raw_output.startswith(prompt.strip()):
        raw_output = raw_output[len(prompt.strip()):].strip()

    # ترجمه خروجی به فارسی
    translated_output = translate_to_persian(raw_output.strip())

    # افزودن پیام تبلیغاتی در پایان
    final_output = translated_output + "\n\nبرای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:\n02188252497"

    return final_output

# رابط Gradio
iface = gr.Interface(
    fn=generate_topics,
    inputs=[
        gr.Textbox(label="رشته"),
        gr.Textbox(label="گرایش"),
        gr.Textbox(label="کلیدواژه‌ها"),
        gr.Textbox(label="جامعه هدف"),
        gr.Dropdown(choices=["کارشناسی ارشد", "دکتری"], label="مقطع")
    ],
    outputs="text",
    title="پیشنهادگر هوشمند موضوع پایان‌نامه کاسپین 🎓"
)

iface.launch()