Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"""
|
| 93 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
|
|
|
|
| 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()
|