diginoron commited on
Commit
29c5484
·
verified ·
1 Parent(s): ca87238

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -3,18 +3,20 @@ import os
3
  from deep_translator import GoogleTranslator
4
  from huggingface_hub import InferenceClient
5
 
6
- # دریافت توکن از محیط
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):
 
15
  if not all([field.strip(), major.strip(), keywords.strip(), audience.strip()]):
16
  return "<div style='color: red;'>❌ لطفاً همه فیلدها را پر کنید.</div>"
17
 
 
18
  prompt = (
19
  f"Suggest 3 academic thesis topics based on the following:\n"
20
  f"Field: {field}\n"
@@ -25,10 +27,10 @@ def generate_topics(field, major, keywords, audience, level):
25
  )
26
 
27
  try:
28
- # اینجا از 'input' به‌جای 'inputs' استفاده می‌کنیم
29
  result = hf_client.text_generation(
30
- model="deepseek-ai/DeepSeek-Prover-V2-671B",
31
- input=prompt,
32
  max_new_tokens=512,
33
  temperature=0.7
34
  )
@@ -40,7 +42,7 @@ def generate_topics(field, major, keywords, audience, level):
40
  except Exception:
41
  translated_output = english_output
42
 
43
- # قالب‌بندی HTML
44
  translated_output_html = "<ol>" + \
45
  "".join(f"<li>{line}</li>" for line in translated_output.split("\n") if line) + \
46
  "</ol>"
@@ -48,7 +50,7 @@ def generate_topics(field, major, keywords, audience, level):
48
  return (
49
  "<div>"
50
  f"{translated_output_html}"
51
- "<br><br>📢 برای مشاوره تخصصی با کاسپین تماس بگیرید:<br>"
52
  "<strong>021-88252497</strong>"
53
  "</div>"
54
  )
@@ -56,6 +58,7 @@ def generate_topics(field, major, keywords, audience, level):
56
  except Exception as e:
57
  return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
58
 
 
59
  iface = gr.Interface(
60
  fn=generate_topics,
61
  inputs=[
@@ -67,6 +70,7 @@ iface = gr.Interface(
67
  ],
68
  outputs=gr.HTML(label="موضوعات پیشنهادی", elem_id="output_box"),
69
  title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین",
 
70
  css="""
71
  #output_box {
72
  min-height: 350px !important;
@@ -86,4 +90,5 @@ iface = gr.Interface(
86
  )
87
 
88
  if __name__ == "__main__":
 
89
  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):
15
+ # اعتبارسنجی ورودی‌ها
16
  if not all([field.strip(), major.strip(), keywords.strip(), audience.strip()]):
17
  return "<div style='color: red;'>❌ لطفاً همه فیلدها را پر کنید.</div>"
18
 
19
+ # ساخت پرامپت
20
  prompt = (
21
  f"Suggest 3 academic thesis topics based on the following:\n"
22
  f"Field: {field}\n"
 
27
  )
28
 
29
  try:
30
+ # فراخوانی مدل DeepSeek با آرگومان‌های موقعیتی
31
  result = hf_client.text_generation(
32
+ "deepseek-ai/DeepSeek-Prover-V2-671B",
33
+ prompt,
34
  max_new_tokens=512,
35
  temperature=0.7
36
  )
 
42
  except Exception:
43
  translated_output = english_output
44
 
45
+ # قالب‌بندی HTML به‌صورت لیست مرتب
46
  translated_output_html = "<ol>" + \
47
  "".join(f"<li>{line}</li>" for line in translated_output.split("\n") if line) + \
48
  "</ol>"
 
50
  return (
51
  "<div>"
52
  f"{translated_output_html}"
53
+ "<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
54
  "<strong>021-88252497</strong>"
55
  "</div>"
56
  )
 
58
  except Exception as e:
59
  return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
60
 
61
+ # رابط کاربری Gradio
62
  iface = gr.Interface(
63
  fn=generate_topics,
64
  inputs=[
 
70
  ],
71
  outputs=gr.HTML(label="موضوعات پیشنهادی", elem_id="output_box"),
72
  title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین",
73
+ theme="default",
74
  css="""
75
  #output_box {
76
  min-height: 350px !important;
 
90
  )
91
 
92
  if __name__ == "__main__":
93
+ # برای لینک عمومی: iface.launch(share=True)
94
  iface.launch()