diginoron commited on
Commit
df8f630
·
verified ·
1 Parent(s): cad9104

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -43
app.py CHANGED
@@ -11,12 +11,13 @@ if not HF_TOKEN:
11
  # ساخت کلاینت جدید
12
  hf_client = InferenceClient(token=HF_TOKEN)
13
 
 
14
  def generate_topics(field, major, keywords, audience, level):
15
  # اعتبارسنجی ورودی‌ها
16
  if not all([field.strip(), major.strip(), keywords.strip(), audience.strip()]):
17
  return "<div style='color: red;'>❌ لطفاً همه فیلدها را پر کنید.</div>"
18
 
19
- # بخش مشترک پرامپت
20
  base_prompt = (
21
  f"Suggest 3 academic thesis topics based on the following:\n"
22
  f"Field: {field}\n"
@@ -25,8 +26,7 @@ def generate_topics(field, major, keywords, audience, level):
25
  f"Target Audience: {audience}\n"
26
  f"Level: {level}\n"
27
  )
28
-
29
- # اگر دکتری است، تاکید روی ارائه الگو و مدل‌سازی پیچیده‌تر
30
  if level == "دکتری":
31
  extra = (
32
  "Since this is a doctoral-level project, focus on proposing theoretical frameworks, "
@@ -36,7 +36,6 @@ def generate_topics(field, major, keywords, audience, level):
36
  extra = (
37
  "Focus on practical and applied thesis topics suitable for a master's level student."
38
  )
39
-
40
  prompt = base_prompt + extra
41
 
42
  try:
@@ -54,18 +53,17 @@ def generate_topics(field, major, keywords, audience, level):
54
 
55
  # ترجمه به فارسی
56
  try:
57
- translated_output = GoogleTranslator(source='en', target='fa').translate(english_output)
58
  except Exception:
59
- translated_output = english_output
60
 
61
- # قالب‌بندی HTML به‌صورت لیست مرتب
62
- translated_output_html = "<ol>" + \
63
- "".join(f"<li>{line}</li>" for line in translated_output.split("\n") if line) + \
64
- "</ol>"
65
 
66
  return (
67
  "<div>"
68
- f"{translated_output_html}"
69
  "<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
70
  "<strong>021-88252497</strong>"
71
  "</div>"
@@ -74,37 +72,48 @@ def generate_topics(field, major, keywords, audience, level):
74
  except Exception as e:
75
  return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
76
 
77
- # رابط کاربری Gradio
78
- iface = gr.Interface(
79
- fn=generate_topics,
80
- inputs=[
81
- gr.Textbox(label="رشته", placeholder="مثال: کامپیوتر"),
82
- gr.Textbox(label="گرایش", placeholder="مثال: هوش مصنوعی"),
83
- gr.Textbox(label="کلیدواژه‌ها", placeholder="مثال: یادگیری عمیق، بینایی ماشین"),
84
- gr.Textbox(label="جامعه هدف", placeholder="مثال: دانشجویان دکتری"),
85
- gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع")
86
- ],
87
- outputs=gr.HTML(label="موضوعات پیشنهادی", elem_id="output_box"),
88
- title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین",
89
- theme="default",
90
- css="""
91
- #output_box {
92
- min-height: 350px !important;
93
- max-height: 600px !important;
94
- overflow-y: auto !important;
95
- background-color: #1e1e1e !important;
96
- color: white !important;
97
- padding: 20px;
98
- border: 2px solid #ccc;
99
- font-family: 'Tahoma', sans-serif;
100
- font-size: 16px;
101
- text-align: right;
102
- direction: rtl;
103
- line-height: 1.8;
104
- }
105
- """
106
- )
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  if __name__ == "__main__":
109
- # برای لینک عمومی: iface.launch(share=True)
110
- iface.launch()
 
11
  # ساخت کلاینت جدید
12
  hf_client = InferenceClient(token=HF_TOKEN)
13
 
14
+ # تابع تولید موضوعات پایان‌نامه
15
  def generate_topics(field, major, keywords, audience, level):
16
  # اعتبارسنجی ورودی‌ها
17
  if not all([field.strip(), major.strip(), keywords.strip(), audience.strip()]):
18
  return "<div style='color: red;'>❌ لطفاً همه فیلدها را پر کنید.</div>"
19
 
20
+ # ساخت پرامپت پایه
21
  base_prompt = (
22
  f"Suggest 3 academic thesis topics based on the following:\n"
23
  f"Field: {field}\n"
 
26
  f"Target Audience: {audience}\n"
27
  f"Level: {level}\n"
28
  )
29
+ # افزودن جزئیات بیشتر برای دکتری
 
30
  if level == "دکتری":
31
  extra = (
32
  "Since this is a doctoral-level project, focus on proposing theoretical frameworks, "
 
36
  extra = (
37
  "Focus on practical and applied thesis topics suitable for a master's level student."
38
  )
 
39
  prompt = base_prompt + extra
40
 
41
  try:
 
53
 
54
  # ترجمه به فارسی
55
  try:
56
+ translated = GoogleTranslator(source='en', target='fa').translate(english_output)
57
  except Exception:
58
+ translated = english_output
59
 
60
+ # قالب‌بندی HTML به لیست مرتب
61
+ items = [f"<li>{line}</li>" for line in translated.split("\n") if line.strip()]
62
+ translated_html = "<ol>" + "".join(items) + "</ol>"
 
63
 
64
  return (
65
  "<div>"
66
+ f"{translated_html}"
67
  "<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
68
  "<strong>021-88252497</strong>"
69
  "</div>"
 
72
  except Exception as e:
73
  return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
74
 
75
+ # CSS برای لودر (ساعت شنی)
76
+ css = """
77
+ /* اسپینر (ساعت شنی) */
78
+ .spinner {
79
+ display: none;
80
+ width: 60px;
81
+ height: 60px;
82
+ margin: 20px auto;
83
+ border: 6px solid #444;
84
+ border-top: 6px solid #1e88e5;
85
+ border-radius: 50%;
86
+ animation: spin 1s linear infinite;
87
+ }
88
+ @keyframes spin { to { transform: rotate(360deg); } }
89
+
90
+ /* هنگام پردازش (processing)، اسپینر را نمایش بده */
91
+ .gradio-container.processing .spinner {
92
+ display: block !important;
93
+ }
94
+ """
95
+
96
+ # ساخت برنامه با Blocks
97
+ with gr.Blocks(css=css, theme="default") as app:
98
+ gr.Markdown("## 🎓 پیشنهادگر موضوع پایان‌نامه کاسپین")
99
+ with gr.Row():
100
+ with gr.Column(scale=1):
101
+ field = gr.Textbox(label="رشته", placeholder="مثال: کامپیوتر")
102
+ major = gr.Textbox(label="گرایش", placeholder="مثال: هوش مصنوعی")
103
+ keywords = gr.Textbox(label="کلیدواژه‌ها", placeholder="مثال: یادگیری عمیق، بینایی ماشین")
104
+ audience = gr.Textbox(label="جامعه هدف", placeholder="مثال: دانشجویان دکتری")
105
+ level = gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع")
106
+ submit = gr.Button("🎯 پیشنهاد موضوع")
107
+ with gr.Column(scale=1):
108
+ spinner = gr.HTML("<div class='spinner'></div>")
109
+ output = gr.HTML(elem_id="output_box")
110
+
111
+ # اتصال دکمه به تابع
112
+ submit.click(
113
+ fn=generate_topics,
114
+ inputs=[field, major, keywords, audience, level],
115
+ outputs=output
116
+ )
117
 
118
  if __name__ == "__main__":
119
+ app.launch()