diginoron commited on
Commit
1bd73ac
·
verified ·
1 Parent(s): b27b053

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -34
app.py CHANGED
@@ -3,10 +3,12 @@ import os
3
  from deep_translator import GoogleTranslator
4
  from huggingface_hub import InferenceClient
5
 
 
6
  HF_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN")
7
  if not HF_TOKEN:
8
  raise RuntimeError("Missing HUGGINGFACE_API_TOKEN secret")
9
 
 
10
  hf_client = InferenceClient(token=HF_TOKEN)
11
 
12
  def generate_topics(field, major, keywords, audience, level):
@@ -41,12 +43,10 @@ def generate_topics(field, major, keywords, audience, level):
41
  max_tokens=512
42
  )
43
  english_output = response.choices[0].message.content.strip()
44
-
45
  try:
46
  translated = GoogleTranslator(source='en', target='fa').translate(english_output)
47
  except:
48
  translated = english_output
49
-
50
  items = [f"<li>{line}</li>" for line in translated.split("\n") if line.strip()]
51
  translated_html = "<ol>" + "".join(items) + "</ol>"
52
 
@@ -57,40 +57,50 @@ def generate_topics(field, major, keywords, audience, level):
57
  "<strong>021-88252497</strong>"
58
  "</div>"
59
  )
60
-
61
  except Exception as e:
62
  return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
63
 
64
- iface = gr.Interface(
65
- fn=generate_topics,
66
- inputs=[
67
- gr.Textbox(label="رشته", placeholder="مثال: کامپیوتر"),
68
- gr.Textbox(label="گرایش", placeholder="مثال: هوش مصنوعی"),
69
- gr.Textbox(label="کلیدواژه‌ها", placeholder="مثال: یادگیری عمیق، بینایی ماشین"),
70
- gr.Textbox(label="جامعه هدف", placeholder="مثال: دانشجویان دکتری"),
71
- gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع")
72
- ],
73
- outputs=gr.HTML(label="موضوعات پیشنهادی", elem_id="output_box"),
74
- title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین",
75
- logo="logo.png", # ← لوگوی شما
76
- theme="default",
77
- css="""
78
- #output_box {
79
- min-height: 350px !important;
80
- max-height: 600px !important;
81
- overflow-y: auto !important;
82
- background-color: #1e1e1e !important;
83
- color: white !important;
84
- padding: 20px;
85
- border: 2px solid #ccc;
86
- font-family: 'Tahoma', sans-serif;
87
- font-size: 16px;
88
- text-align: right;
89
- direction: rtl;
90
- line-height: 1.8;
91
- }
92
- """
93
- )
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  if __name__ == "__main__":
96
- iface.launch()
 
3
  from deep_translator import GoogleTranslator
4
  from huggingface_hub import InferenceClient
5
 
6
+ # دریافت توکن از محیط (در Hugging Face Secrets تنظیم شود)
7
  HF_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN")
8
  if not HF_TOKEN:
9
  raise RuntimeError("Missing HUGGINGFACE_API_TOKEN secret")
10
 
11
+ # ساخت کلاینت جدید
12
  hf_client = InferenceClient(token=HF_TOKEN)
13
 
14
  def generate_topics(field, major, keywords, audience, level):
 
43
  max_tokens=512
44
  )
45
  english_output = response.choices[0].message.content.strip()
 
46
  try:
47
  translated = GoogleTranslator(source='en', target='fa').translate(english_output)
48
  except:
49
  translated = english_output
 
50
  items = [f"<li>{line}</li>" for line in translated.split("\n") if line.strip()]
51
  translated_html = "<ol>" + "".join(items) + "</ol>"
52
 
 
57
  "<strong>021-88252497</strong>"
58
  "</div>"
59
  )
 
60
  except Exception as e:
61
  return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
62
 
63
+ # CSS برای زیبایی خروجی
64
+ css = """
65
+ #output_box {
66
+ min-height: 350px !important;
67
+ max-height: 600px !important;
68
+ overflow-y: auto !important;
69
+ background-color: #1e1e1e !important;
70
+ color: white !important;
71
+ padding: 20px;
72
+ border: 2px solid #ccc;
73
+ font-family: 'Tahoma', sans-serif;
74
+ font-size: 16px;
75
+ text-align: right;
76
+ direction: rtl;
77
+ line-height: 1.8;
78
+ }
79
+ """
80
+
81
+ with gr.Blocks(css=css, title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین") as app:
82
+ # لوگو بالای صفحه
83
+ gr.HTML(
84
+ "<div style='text-align:center;margin-bottom:10px;'>"
85
+ "<img src='/file=logo.png' width='200'/>"
86
+ "</div>"
87
+ )
88
+ with gr.Row():
89
+ with gr.Column():
90
+ field = gr.Textbox(label="رشته", placeholder="مثال: کامپیوتر")
91
+ major = gr.Textbox(label="گرایش", placeholder="مثال: هوش مصنوعی")
92
+ keywords = gr.Textbox(label="کلیدواژه‌ها", placeholder="مثال: یادگیری عمیق، بینایی ماشین")
93
+ audience = gr.Textbox(label="جامعه هدف", placeholder="مثال: دانشجویان دکتری")
94
+ level = gr.Dropdown(["کارشناسی ارشد","دکتری"], label="مقطع")
95
+ submit = gr.Button("🎯 پیشنهاد موضوع")
96
+ with gr.Column():
97
+ output = gr.HTML(elem_id="output_box")
98
+
99
+ submit.click(
100
+ fn=generate_topics,
101
+ inputs=[field, major, keywords, audience, level],
102
+ outputs=output
103
+ )
104
 
105
  if __name__ == "__main__":
106
+ app.launch()