import gradio as gr import os from deep_translator import GoogleTranslator from huggingface_hub import InferenceClient # دریافت توکن از محیط (در Hugging Face Secrets تنظیم شود) HF_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN") if not HF_TOKEN: raise RuntimeError("Missing HUGGINGFACE_API_TOKEN secret") # ساخت کلاینت جدید hf_client = InferenceClient(token=HF_TOKEN) # تابع تولید موضوعات پایان‌نامه def generate_topics(field, major, keywords, audience, level): # اعتبارسنجی ورودی‌ها if not all([field.strip(), major.strip(), keywords.strip(), audience.strip()]): return "
❌ لطفاً همه فیلدها را پر کنید.
" # ساخت پرامپت پایه base_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}\n" ) # افزودن جزئیات بیشتر برای دکتری if level == "دکتری": extra = ( "Since this is a doctoral-level project, focus on proposing theoretical frameworks, " "advanced modeling approaches, and in-depth methodological contributions." ) else: extra = ( "Focus on practical and applied thesis topics suitable for a master's level student." ) prompt = base_prompt + extra try: # فراخوانی مدل DeepSeek با endpoint چت response = hf_client.chat.completions.create( model="deepseek-ai/DeepSeek-Prover-V2-671B", messages=[ {"role": "system", "content": "You are an academic advisor assistant."}, {"role": "user", "content": prompt} ], temperature=0.7, max_tokens=512 ) english_output = response.choices[0].message.content.strip() # ترجمه به فارسی try: translated = GoogleTranslator(source='en', target='fa').translate(english_output) except Exception: translated = english_output # قالب‌بندی HTML به لیست مرتب items = [f"
  • {line}
  • " for line in translated.split("\n") if line.strip()] translated_html = "
      " + "".join(items) + "
    " return ( "
    " f"{translated_html}" "

    📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:
    " "021-88252497" "
    " ) except Exception as e: return f"
    ❌ خطا در تماس با مدل DeepSeek: {e}
    " # CSS برای لودر (ساعت شنی) css = """ /* اسپینر (ساعت شنی) */ .spinner { display: none; width: 60px; height: 60px; margin: 20px auto; border: 6px solid #444; border-top: 6px solid #1e88e5; border-radius: 50%; animation: spin 1s linear infinite; } @keyframes spin { to { transform: rotate(360deg); } } /* هنگام پردازش (processing)، اسپینر را نمایش بده */ .gradio-container.processing .spinner { display: block !important; } """ # ساخت برنامه با Blocks with gr.Blocks(css=css, theme="default") as app: gr.Markdown("## 🎓 پیشنهادگر موضوع پایان‌نامه کاسپین") with gr.Row(): with gr.Column(scale=1): field = gr.Textbox(label="رشته", placeholder="مثال: کامپیوتر") major = gr.Textbox(label="گرایش", placeholder="مثال: هوش مصنوعی") keywords = gr.Textbox(label="کلیدواژه‌ها", placeholder="مثال: یادگیری عمیق، بینایی ماشین") audience = gr.Textbox(label="جامعه هدف", placeholder="مثال: دانشجویان دکتری") level = gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع") submit = gr.Button("🎯 پیشنهاد موضوع") with gr.Column(scale=1): spinner = gr.HTML("
    ") output = gr.HTML(elem_id="output_box") # اتصال دکمه به تابع submit.click( fn=generate_topics, inputs=[field, major, keywords, audience, level], outputs=output ) if __name__ == "__main__": app.launch()