File size: 2,396 Bytes
2543f2c
f35dc2e
 
8c853ac
2543f2c
f35dc2e
 
4f768b5
2543f2c
f35dc2e
 
 
 
 
 
 
1c68ea4
2543f2c
f35dc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2543f2c
 
 
 
 
 
 
f35dc2e
2543f2c
4f768b5
f35dc2e
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
import gradio as gr
import os
from openai import OpenAI
from deep_translator import GoogleTranslator

# دریافت کلید از Secret Env (در Hugging Face تنظیم می‌کنی، پایین توضیح می‌دم)
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

def generate_topics(field, major, keywords, audience, level):
    prompt = (
        f"Suggest 3 academic thesis topics based on the following:\n"
        f"Field: {field}\n"
        f"Specialization: {major}\n"
        f"Keywords: {keywords}\n"
        f"Target Audience: {audience}\n"
        f"Level: {level}"
    )

    try:
        completion = client.chat.completions.create(
            model="gpt-4",  # می‌تونی به gpt-3.5-turbo تغییر بدی برای ارزان‌تر بودن
            messages=[
                {"role": "system", "content": "You are an academic advisor assistant."},
                {"role": "user", "content": prompt}
            ]
        )

        english_output = completion.choices[0].message.content.strip()
        translated_output = GoogleTranslator(source='en', target='fa').translate(english_output)
        translated_output_html = translated_output.replace("\n", "<br>")

        html_output = (
            "<div dir='rtl' style='text-align: right; font-family: Tahoma; font-size: 16px; "
            "line-height: 1.8; min-height: 250px; max-height: 400px; overflow-y: auto; "
            "border: 1px solid #ccc; padding: 10px; background-color: #fdfdfd;'>"
            f"{translated_output_html}"
            "<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
            "<strong>021-88252497</strong></div>"
        )

        return html_output

    except Exception as e:
        return f"<div dir='rtl' style='color:red;'>❌ خطا در تماس با OpenAI API: {e}</div>"

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

iface.launch()