diginoron commited on
Commit
88a1e0a
·
verified ·
1 Parent(s): 44fe883

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -25
app.py CHANGED
@@ -1,13 +1,27 @@
1
  import gradio as gr
2
  import os
3
- from openai import OpenAI
4
  from deep_translator import GoogleTranslator
 
5
 
6
- # دریافت کلید از محیط (در Hugging Face از Secrets استفاده کن)
7
- client = OpenAI(api_key=os.environ["OPENAI"]) # ✅ درست شد
 
 
 
 
 
 
 
 
8
 
9
  # تابع اصلی پیشنهاد موضوع پایان‌نامه
 
10
  def generate_topics(field, major, keywords, audience, level):
 
 
 
 
 
11
  prompt = (
12
  f"Suggest 3 academic thesis topics based on the following:\n"
13
  f"Field: {field}\n"
@@ -18,17 +32,24 @@ def generate_topics(field, major, keywords, audience, level):
18
  )
19
 
20
  try:
21
- completion = client.chat.completions.create(
22
- model="gpt-3.5-turbo", # ✅ مدل معتبر و قابل استفاده عمومی
23
- messages=[
24
- {"role": "system", "content": "You are an academic advisor assistant."},
25
- {"role": "user", "content": prompt}
26
- ]
27
  )
 
 
 
 
 
 
 
 
28
 
29
- english_output = completion.choices[0].message.content.strip()
30
- translated_output = GoogleTranslator(source='en', target='fa').translate(english_output)
31
- translated_output_html = translated_output.replace("\n", "<br>")
 
32
 
33
  html_output = (
34
  "<div>"
@@ -41,22 +62,19 @@ def generate_topics(field, major, keywords, audience, level):
41
  return html_output
42
 
43
  except Exception as e:
44
- return f"<div style='color: red;'>❌ خطا در تماس با OpenAI API: {e}</div>"
45
 
46
- # رابط کاربری Gradio با ظاهر اصلاح‌شده برای نمایش درست خروجی
47
  iface = gr.Interface(
48
  fn=generate_topics,
49
  inputs=[
50
- gr.Textbox(label="رشته"),
51
- gr.Textbox(label="گرایش"),
52
- gr.Textbox(label="کلیدواژه‌ها"),
53
- gr.Textbox(label="جامعه هدف"),
54
  gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع")
55
  ],
56
- outputs=gr.HTML(
57
- label="موضوعات پیشنهادی",
58
- elem_id="output_box"
59
- ),
60
  title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین",
61
  theme="default",
62
  css="""
@@ -64,8 +82,8 @@ iface = gr.Interface(
64
  min-height: 350px !important;
65
  max-height: 600px !important;
66
  overflow-y: auto !important;
67
- background-color: #1e1e1e !important; /* پس‌زمینه تیره */
68
- color: white !important; /* متن سفید */
69
  padding: 20px;
70
  border: 2px solid #ccc;
71
  font-family: 'Tahoma', sans-serif;
@@ -77,4 +95,5 @@ iface = gr.Interface(
77
  """
78
  )
79
 
80
- iface.launch()
 
 
1
  import gradio as gr
2
  import os
 
3
  from deep_translator import GoogleTranslator
4
+ from huggingface_hub import InferenceApi
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
+ # ایجاد کلاینت برای تماس با مدل DeepSeek
12
+ hf_api = InferenceApi(
13
+ repo_id="deepseek-ai/DeepSeek-Prover-V2-671B",
14
+ token=HF_TOKEN
15
+ )
16
 
17
  # تابع اصلی پیشنهاد موضوع پایان‌نامه
18
+
19
  def generate_topics(field, major, keywords, audience, level):
20
+ # اعتبارسنجی ورودی‌ها
21
+ if not all([field.strip(), major.strip(), keywords.strip(), audience.strip()]):
22
+ return "<div style='color: red;'>❌ لطفاً همه فیلدها را پر کنید.</div>"
23
+
24
+ # ساخت پرامپت
25
  prompt = (
26
  f"Suggest 3 academic thesis topics based on the following:\n"
27
  f"Field: {field}\n"
 
32
  )
33
 
34
  try:
35
+ # فراخوانی مدل DeepSeek
36
+ response = hf_api(
37
+ inputs=prompt,
38
+ parameters={"max_new_tokens": 512, "temperature": 0.7}
 
 
39
  )
40
+ # استخراج متن پاسخ (در صورت نیاز به تنظیم بر اساس فرمت خروجی)
41
+ english_output = response[0]['generated_text'].strip() if isinstance(response, list) else response.strip()
42
+
43
+ # ترجمه به فارسی
44
+ try:
45
+ translated_output = GoogleTranslator(source='en', target='fa').translate(english_output)
46
+ except Exception:
47
+ translated_output = english_output
48
 
49
+ # قالب‌بندی خروجی HTML
50
+ translated_output_html = "<ol>" + \
51
+ "".join(f"<li>{line}</li>" for line in translated_output.split("\n") if line) + \
52
+ "</ol>"
53
 
54
  html_output = (
55
  "<div>"
 
62
  return html_output
63
 
64
  except Exception as e:
65
+ return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
66
 
67
+ # رابط کاربری Gradio
68
  iface = gr.Interface(
69
  fn=generate_topics,
70
  inputs=[
71
+ gr.Textbox(label="رشته", placeholder="مثال: کامپیوتر"),
72
+ gr.Textbox(label="گرایش", placeholder="مثال: هوش مصنوعی"),
73
+ gr.Textbox(label="کلیدواژه‌ها", placeholder="مثال: یادگیری عمیق، بینایی ماشین"),
74
+ gr.Textbox(label="جامعه هدف", placeholder="مثال: دانشجویان دکتری"),
75
  gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع")
76
  ],
77
+ outputs=gr.HTML(label="موضوعات پیشنهادی", elem_id="output_box"),
 
 
 
78
  title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین",
79
  theme="default",
80
  css="""
 
82
  min-height: 350px !important;
83
  max-height: 600px !important;
84
  overflow-y: auto !important;
85
+ background-color: #1e1e1e !important;
86
+ color: white !important;
87
  padding: 20px;
88
  border: 2px solid #ccc;
89
  font-family: 'Tahoma', sans-serif;
 
95
  """
96
  )
97
 
98
+ if __name__ == "__main__":
99
+ iface.launch()