File size: 2,173 Bytes
2543f2c
e63032a
8c853ac
d751a8f
2543f2c
4f768b5
e63032a
4f768b5
 
 
e63032a
4f768b5
e63032a
1c68ea4
4f768b5
d751a8f
2543f2c
 
1c68ea4
e63032a
b98b6c1
 
 
 
e63032a
 
1c68ea4
e63032a
d751a8f
 
 
 
4f768b5
8c853ac
1c68ea4
2543f2c
1c68ea4
 
 
 
 
 
 
4f768b5
2543f2c
4f768b5
2543f2c
 
 
 
 
 
 
 
 
4f768b5
b98b6c1
2543f2c
 
d751a8f
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
import gradio as gr
from transformers import AutoTokenizer, GemmaForCausalLM
from deep_translator import GoogleTranslator
import torch

# مشخصات مدل
model_id = "google/gemma-3-4b-it"

# بارگذاری مدل و توکنایزر
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = GemmaForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
    device_map="auto"
)
model.eval()

def generate_topics(field, major, keywords, audience, level):
    # ساخت پرامپت
    prompt = f"""<bos>[INST]Suggest 3 academic thesis topics based on the following information:
Field: {field}
Specialization: {major}
Keywords: {keywords}
Target audience: {audience}
Level: {level}[/INST]"""

    # تولید خروجی
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=256)
    english_output = tokenizer.decode(outputs[0], skip_special_tokens=True)

    # ترجمه به فارسی
    translated_output = GoogleTranslator(source='en', target='fa').translate(english_output)
    translated_output_html = translated_output.strip().replace("\n", "<br>")

    # HTML راست‌چین
    html_output = (
        "<div dir='rtl' style='text-align: right; font-family: Tahoma, sans-serif; font-size: 16px; "
        f"line-height: 1.8;'>{translated_output_html}"
        "<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
        "<strong>021-88252497</strong></div>"
    )
    return html_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=gr.HTML(label="موضوعات پیشنهادی"),
    title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین"
)

iface.launch()