Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,10 @@ import os
|
|
3 |
from openai import OpenAI
|
4 |
from deep_translator import GoogleTranslator
|
5 |
|
6 |
-
# دریافت کلید از
|
7 |
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
8 |
|
|
|
9 |
def generate_topics(field, major, keywords, audience, level):
|
10 |
prompt = (
|
11 |
f"Suggest 3 academic thesis topics based on the following:\n"
|
@@ -18,7 +19,7 @@ def generate_topics(field, major, keywords, audience, level):
|
|
18 |
|
19 |
try:
|
20 |
completion = client.chat.completions.create(
|
21 |
-
model="gpt-4", #
|
22 |
messages=[
|
23 |
{"role": "system", "content": "You are an academic advisor assistant."},
|
24 |
{"role": "user", "content": prompt}
|
@@ -30,19 +31,19 @@ def generate_topics(field, major, keywords, audience, level):
|
|
30 |
translated_output_html = translated_output.replace("\n", "<br>")
|
31 |
|
32 |
html_output = (
|
33 |
-
"<div
|
34 |
-
"line-height: 1.8; min-height: 250px; max-height: 400px; overflow-y: auto; "
|
35 |
-
"border: 1px solid #ccc; padding: 10px; background-color: #fdfdfd;'>"
|
36 |
f"{translated_output_html}"
|
37 |
"<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
|
38 |
-
"<strong>021-88252497</strong
|
|
|
39 |
)
|
40 |
|
41 |
return html_output
|
42 |
|
43 |
except Exception as e:
|
44 |
-
return f"<div
|
45 |
|
|
|
46 |
iface = gr.Interface(
|
47 |
fn=generate_topics,
|
48 |
inputs=[
|
@@ -52,8 +53,27 @@ iface = gr.Interface(
|
|
52 |
gr.Textbox(label="جامعه هدف"),
|
53 |
gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع")
|
54 |
],
|
55 |
-
outputs=gr.HTML(
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
)
|
58 |
|
59 |
iface.launch()
|
|
|
3 |
from openai import OpenAI
|
4 |
from deep_translator import GoogleTranslator
|
5 |
|
6 |
+
# دریافت کلید از محیط (در Hugging Face از Secrets استفاده کن)
|
7 |
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
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"
|
|
|
19 |
|
20 |
try:
|
21 |
completion = client.chat.completions.create(
|
22 |
+
model="gpt-4", # یا gpt-3.5-turbo
|
23 |
messages=[
|
24 |
{"role": "system", "content": "You are an academic advisor assistant."},
|
25 |
{"role": "user", "content": prompt}
|
|
|
31 |
translated_output_html = translated_output.replace("\n", "<br>")
|
32 |
|
33 |
html_output = (
|
34 |
+
"<div>"
|
|
|
|
|
35 |
f"{translated_output_html}"
|
36 |
"<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
|
37 |
+
"<strong>021-88252497</strong>"
|
38 |
+
"</div>"
|
39 |
)
|
40 |
|
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=[
|
|
|
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="""
|
63 |
+
#output_box {
|
64 |
+
min-height: 350px !important;
|
65 |
+
max-height: 600px !important;
|
66 |
+
overflow-y: auto !important;
|
67 |
+
background-color: #fefefe !important;
|
68 |
+
padding: 20px;
|
69 |
+
border: 2px solid #ccc;
|
70 |
+
font-family: 'Tahoma', sans-serif;
|
71 |
+
font-size: 16px;
|
72 |
+
text-align: right;
|
73 |
+
direction: rtl;
|
74 |
+
line-height: 1.8;
|
75 |
+
}
|
76 |
+
"""
|
77 |
)
|
78 |
|
79 |
iface.launch()
|