caspianthesis / app.py
diginoron's picture
Update app.py
1bd73ac verified
raw
history blame
4.09 kB
import gradio as gr
import os
from deep_translator import GoogleTranslator
from huggingface_hub import InferenceClient
# دریافت توکن از محیط (در Hugging Face Secrets تنظیم شود)
HF_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN")
if not HF_TOKEN:
raise RuntimeError("Missing HUGGINGFACE_API_TOKEN secret")
# ساخت کلاینت جدید
hf_client = InferenceClient(token=HF_TOKEN)
def generate_topics(field, major, keywords, audience, level):
if not all([field.strip(), major.strip(), keywords.strip(), audience.strip()]):
return "<div style='color: red;'>❌ لطفاً همه فیلدها را پر کنید.</div>"
base_prompt = (
f"Suggest 3 academic thesis topics based on the following:\n"
f"Field: {field}\n"
f"Specialization: {major}\n"
f"Keywords: {keywords}\n"
f"Target Audience: {audience}\n"
f"Level: {level}\n"
)
extra = (
"Since this is a doctoral-level project, focus on proposing theoretical frameworks, "
"advanced modeling approaches, and in-depth methodological contributions."
if level == "دکتری"
else
"Focus on practical and applied thesis topics suitable for a master's level student."
)
prompt = base_prompt + extra
try:
response = hf_client.chat.completions.create(
model="deepseek-ai/DeepSeek-Prover-V2-671B",
messages=[
{"role": "system", "content": "You are an academic advisor assistant."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=512
)
english_output = response.choices[0].message.content.strip()
try:
translated = GoogleTranslator(source='en', target='fa').translate(english_output)
except:
translated = english_output
items = [f"<li>{line}</li>" for line in translated.split("\n") if line.strip()]
translated_html = "<ol>" + "".join(items) + "</ol>"
return (
"<div>"
f"{translated_html}"
"<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
"<strong>021-88252497</strong>"
"</div>"
)
except Exception as e:
return f"<div style='color: red;'>❌ خطا در تماس با مدل DeepSeek: {e}</div>"
# CSS برای زیبایی خروجی
css = """
#output_box {
min-height: 350px !important;
max-height: 600px !important;
overflow-y: auto !important;
background-color: #1e1e1e !important;
color: white !important;
padding: 20px;
border: 2px solid #ccc;
font-family: 'Tahoma', sans-serif;
font-size: 16px;
text-align: right;
direction: rtl;
line-height: 1.8;
}
"""
with gr.Blocks(css=css, title="🎓 پیشنهادگر موضوع پایان‌نامه کاسپین") as app:
# لوگو بالای صفحه
gr.HTML(
"<div style='text-align:center;margin-bottom:10px;'>"
"<img src='/file=logo.png' width='200'/>"
"</div>"
)
with gr.Row():
with gr.Column():
field = gr.Textbox(label="رشته", placeholder="مثال: کامپیوتر")
major = gr.Textbox(label="گرایش", placeholder="مثال: هوش مصنوعی")
keywords = gr.Textbox(label="کلیدواژه‌ها", placeholder="مثال: یادگیری عمیق، بینایی ماشین")
audience = gr.Textbox(label="جامعه هدف", placeholder="مثال: دانشجویان دکتری")
level = gr.Dropdown(["کارشناسی ارشد","دکتری"], label="مقطع")
submit = gr.Button("🎯 پیشنهاد موضوع")
with gr.Column():
output = gr.HTML(elem_id="output_box")
submit.click(
fn=generate_topics,
inputs=[field, major, keywords, audience, level],
outputs=output
)
if __name__ == "__main__":
app.launch()