Spaces:
Running
Running
| import gradio as gr | |
| import os | |
| from openai import OpenAI | |
| from deep_translator import GoogleTranslator | |
| # دریافت کلید از محیط (در Hugging Face از Secrets استفاده کن) | |
| 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-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>" | |
| f"{translated_output_html}" | |
| "<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>" | |
| "<strong>021-88252497</strong>" | |
| "</div>" | |
| ) | |
| return html_output | |
| except Exception as e: | |
| return f"<div style='color: red;'>❌ خطا در تماس با OpenAI API: {e}</div>" | |
| # رابط کاربری Gradio با ظاهر اصلاحشده برای نمایش درست خروجی | |
| 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="موضوعات پیشنهادی", | |
| elem_id="output_box" | |
| ), | |
| title="🎓 پیشنهادگر موضوع پایاننامه کاسپین", | |
| theme="default", | |
| css=""" | |
| #output_box { | |
| min-height: 350px !important; | |
| max-height: 600px !important; | |
| overflow-y: auto !important; | |
| background-color: #1e1e1e !important; /* پسزمینه تیره */ | |
| color: white !important; /* متن سفید */ | |
| padding: 20px; | |
| border: 2px solid #ccc; | |
| font-family: 'Tahoma', sans-serif; | |
| font-size: 16px; | |
| text-align: right; | |
| direction: rtl; | |
| line-height: 1.8; | |
| } | |
| """ | |
| ) | |
| iface.launch() | |